Alternative for handling requests using switch statement - php

I have to set up multiple cron jobs. Each cron will be a separate request to the server. So, I started by the following, where each request will be handled by a case inside the switch, but the cases are bound to increase and thus doesn't seem to me a very good idea.
require_once './invoice_cron.php';
$checkRequest = isset($_REQUEST['request']);
if($checkRequest) {
$request_name = $_REQUEST['request'];
switch($request_name) {
case 'send_invoice':
break;
default:
break;
}
}
What could be a better approach here?

Let there be a request handler interface:
<?php
// CronRequests.php
require_once __DIR__.'./autoload.php';
$request_name = isset($_REQUEST['request']) ?
(new _cron)->handler($_REQUEST['request']) :
null ;
Make a class that could handle each request:
class _cron {
/**
* List of possible requests
* #var array
*/
private static $REQUESTS = ['send_invoice','start_user_subscription'];
/**
* HTTP request handler for all cron jobs
* #param string $request_name Name of the request
*/
public function handler($request_name) {
$status = false;
if(isset($request_name)) {
$request_map = array_flip(self::$REQUESTS);
if(isset($request_map[$request_name])) {
$status = $this->$request_name();
}
}
return $status;
}
}
The list of requests are bound to increase, so it is necessary to search the list efficiently. So, here we do an array flip and check a key for existence.

for each request type must match a function
all the types of requests must be defined in the system before start the processing
if a request is not in the list => some logical response must happen
i will give you just simple example, you can develop it
class Request{
private $REQUEST_TYPES = ['get_invoice', 'set_invoice', 'print_invoice'];
public function handler($requestKey){
foreach($this->REQUEST_TYPES as $type){
if($requestKey == $type) $this->$type();
}
$this->handlerNotFound();
}
private function get_invoice(){
//do some thing here
}
private function set_invoice(){
//do some thing here
}
private function print_invoice(){
//do some thing here
}
private function handlerNotFound(){
//do some thing here
}
}

Related

How to properly unpack Protobuf Any type in PHP

I'm desperately trying to check whether an "unknown" protobuf payload wrapped within an Any is some specific type (Heartbeat) and unpack it to a Heartbeat object using the code below. Both Heartbeat and StatusChanged are valid generated Protobuf classes.
/* <external service> */
$event = (new StatusChanged)->setId("testId");
$any = new Any;
$any->pack($event);
$protobufBinaryAny = $any->serializeToString();
/* </external service> */
/* $protobufBinaryAny is the incoming binary data */
$anyEvent = new Any();
$anyEvent->mergeFromString($protobufBinaryAny);
if ($anyEvent->is(Heartbeat::class)) {
$this->processHeartbeat($anyEvent->unpack());
} else {
throw new UnknownMessageTypeException($anyEvent->getTypeUrl());
}
When running the following code, I'd expect it to throw a UnknownMessageTypeException, however, I get this:
Call to a member function getFullName() on null.
This error happens at $data->is(Heartbeat::class), since the Heartbeat class obviously couldn't be found in the DescriptorPool.
/**
* Google\Protobuf\Any
* https://github.com/protocolbuffers/protobuf/blob/440a156e1cd5c783d8d64eef81a93b1df3d78b60/php/src/Google/Protobuf/Any.php#L315-L323
*/
class Any {
public function is($klass)
{
$pool = \Google\Protobuf\Internal\DescriptorPool::getGeneratedPool();
$desc = $pool->getDescriptorByClassName($klass);
$fully_qualifed_name = $desc->getFullName();
$type_url = GPBUtil::TYPE_URL_PREFIX.substr(
$fully_qualifed_name, 1, strlen($fully_qualifed_name));
return $this->type_url === $type_url;
}
}
However, when explicitly initializing the Heartbeat before, it's working, since Heartbeat is now in the DescriptorPool.
foreach ([
\GPBMetadata\Heartbeat::class,
\GPBMetadata\StatusChanged::class,
] as $klass) {
$klass::initOnce();
}
But this manual initialization thing just feels wrong. Am I supposed to do it like that, or did I just miss a fancy autoloading-feature in the non-existing PHP docs?
For the time being, I wrote an autoloader for this case, but it still just feels not right:
trait HandlesProtobufMessages
{
function initializeProtobufMessages()
{
/** #var \Composer\Autoload\ClassLoader */
$loader = require(__DIR__.'/../../vendor/autoload.php');
$classes = array_filter(array_keys($loader->getClassMap()), function ($className) {
$namespace = "GPBMetadata\\";
return substr($className, 0, strlen($namespace)) === $namespace;
});
foreach ($classes as $class) {
$class::initOnce();
}
}
}
Maybe you have more insights into this protobuf jungle and can help me out here, would be glad!
Thank you in advance

PHPunit method expected to be called 1 time, actually called 0 times

I've been stuck on this for a while and I'm not sure why PHPunit can't see that the function is being called.
This is the code I'm trying to test:
public function handle()
{
$path = $this->request->getPath();
$requestMethod = $this->request->getMethod();
if (!$path) {
$this->redirect('home');
} else if (!$this->isMethodPathFound($path, $requestMethod)) {
$this->redirect('404');
} else {
$handler = $this->getControllerFullName($this->routes[$path]['handler']);
if (is_callable($handler)) {
call_user_func($handler);
} else {
$this->redirect('404');
}
}
}
/**
* #param string $path
* #param int $statusCode
*/
public function redirect($path, $statusCode = 303)
{
if (defined('TESTING_ENVIRONMENT') && TESTING_ENVIRONMENT) {
return;
}
header(
'Location: ' . $this->request->getProtocol() .
$this->request->getHost() . '/' . $path,
true,
$statusCode
);
die();
}
The TESTING_ENVIRONMENT variable is set for the header function so it does not trigger on running PHPunit (I don't want to create another class to have that redirect function just to be able to mock it for one test) and this is the testing code:
public function testHandlePathIsEmpty()
{
$requestMock = $this->getMockBuilder('\services\Request')->getMock();
$requestMock->expects($this->once())->method('getPath')->willReturn('');
$requestMock->expects($this->once())->method('getMethod')->willReturn('GET');
$routerMock = $this->getMockBuilder('\services\Router')
->setConstructorArgs([$this->routes, $requestMock])
->enableProxyingToOriginalMethods()
->getMock();
$routerMock->expects($this->once())->method('redirect')
->with('asdasd')->willReturn(true);
$routerMock->handle();
}
The $routerMock object should definitely invoke the "redirect" function, and it says that it does not get invoked..even though when I var_dump/die inside the function, it does go inside of it.
Thanks for the help!
Though you hesitated to show the complete output of phpunit's error, your problem is very likely not that your method is not called, but that it is not called with all the expectations you defined.
Your code
$routerMock->expects($this->once())->method('redirect')
->with('asdasd')->willReturn(true);
translates to the following expectations: The method redirect must be called exactly once with an argument 'asdasd' and will return true.
From your testcode I do not see that there is asdasd passed to the redirect method. Your test will most likely succeed when you remove the with expectation.
Just to make this clear. If you have to mock the class u want to test, your code is way to complex and you should think about implementing your logic in another way.
How about not mocking the class you are actually testing, create the new instance by passing the Request and a Router Mock (Router mock might not have any logic since you are not going to use it) and then do the following in your code:
public function handle()
{
$request = $this->request;
$path = $request->getPath();
if (!$path) {
$this->redirect('home');
} else if (!$this->isMethodPathFound($path, $request->getMethod())) {
$this->redirect('404');
} else {
$handler = $this->getControllerFullName($this->routes[$path]['handler']);
if (is_callable($handler)) {
call_user_func($handler);
} else {
$this->redirect('404');
}
}
}
In your Unit-Test, you now can just test for
$requestMock
->expects($this->never())
->method('getMethod');
I see that this would only cover the second case to not being executed but the third one could happen aswell. Thats always a point why your code is not clean enough.
You should read something about KISS and SOLID to make your code more testable. This method is just too complex as you could test it correctly.

PHP include Page.php, if Page=404 { PHP include API { WRITE API as Page.php (Storing APIs on Servers)

I'm scraping data from an API. The problem is that I'm querying the data too often for each pageload, so I'd like to store the data on the server after the first query. This should be fine according to the TOS.
On example.com/page:
<?php include 'example.com/data'; ?>
On example.com/data:
<?php include 'api.com/include'; ?>
So I am including a page from my server, and that page is including the api data from the external server.
Question 1: How can I tell example.com/data to WRITE or save the information from api.com/include as a file on the server such as example.com/data1.php ?
Question 2: How can I tell example.com/page to php include example.com/data1.php, and if it's a 404 (doesn't exist) to include example.com/data instead?
With both questions 1 and 2 answered I can query the api once, store the data as a file, and if that file exists use the data from that page rather than have to query the api each time the page is loaded.
If you know of a better way of doing this I'd be grateful to learn it. Though it is important that I include from example.com/data from example.com/page rather than directly from api.com/include because example.com/data has the correct code handlers to properly interpret and filter the information from api/include.
If you can answer either of the two questions it would be a great starting ground for me solving the other problem.
Thank you!
You should use a class to decouple this behaviour. Something like this:
<?php
class ReadApiFromDomainDotCom
{
const TTL = 3600; // File is fresh for 3600 sec
const FILE_NAME = 'whatever.txt';
const API_ADDRESS = "http://api.whatever.com";
/**
* #return string
*/
public function get()
{
if ($this->isCacheValid()) {
return file_get_contents(self::FILE_NAME);
}
return $this->readApi();
}
/**
* #return bool
*/
private function isCacheValid()
{
if (!file_exists(self::FILE_NAME)) {
return false;
}
if ($this->isExpired()) {
return false;
}
return true;
}
/**
* #return bool
*/
private function isExpired()
{
return time() - filemtime(self::FILE_NAME) > self::TTL;
}
/**
* #return string
*/
private function readApi()
{
$data = file_get_contents(self::API_ADDRESS);
file_put_contents(self::FILE_NAME, $data);
return $data;
}
}
Then it will be easy as:
$apiReader = new ReadApiFromDomainDotCom();
$data $apiReader->get();
Haven't tested this code so you will have to fiddle with it a bit. Add namespace, paths and so on.

PHP - Multiple instances of script accessing same resources

I have to analyze a lot of information.
To speed things up I'll be running multiple instances of same script at the same moment.
However there is a big chance scripts would analyze same piece of information(duplicate) which I do not like as it would slow down the process.
If running only 1 instance I solve this problem with array(I save what has been already analyzed).
So I have a question how could I somehow sync that array with other "threads" ?
MySQL is an option but I guess it would be overkill?
I read also about memory sharing but not sure if this is solution I am looking for.
So if anyone has some suggestions let me know.
Regards
This is a trivial task using real multi-threading:
<?php
/* we want logs to be readable so we are creating a mutex for output */
define ("LOG", Mutex::create());
/* basically a thread safe printf */
function slog($message, $format = null) {
$format = func_get_args();
if ($format) {
$message = array_shift($format);
if ($message) {
Mutex::lock(LOG);
echo vsprintf(
$message, $format);
Mutex::unlock(LOG);
}
}
}
/* any pthreads descendant would do */
class S extends Stackable {
public function run(){}
}
/* a thread that manipulates the shared data until it's all gone */
class T extends Thread {
public function __construct($shared) {
$this->shared = $shared;
}
public function run() {
/* you could also use ::chunk if you wanted to bite off a bit more work */
while (($next = $this->shared->shift())) {
slog(
"%lu working with item #%d\n", $this->getThreadId(), $next);
}
}
}
$shared = new S();
/* fill with dummy data */
while (#$o++ < 10000) {
$shared[]=$o;
}
/* start some threads */
$threads = array();
while (#$thread++ < 5) {
$threads[$thread] = new T($shared);
$threads[$thread]->start();
}
/* join all threads */
foreach ($threads as $thread)
$thread->join();
/* important; ::destroy what you ::create */
Mutex::destroy(LOG);
?>
The slog() function isn't necessarily required for your use case, but thought it useful to show an executable example with readable output.
The main gist of it is that multiple threads need only a reference to a common set of data to manipulate that data ...

filter methods in a controller

I want to create a filter for my add, update, and delete actions in my controllers to automatically check if they
were called in a POST, as opposed to GET or some other method
and have the pageInstanceIDs which I set in the forms on my views
protects against xss
protects against double submission of a form
from submit button double click
from back button pressed after a submision
from a url being saved or bookmarked
Currently I extended \lithium\action\Controller using an AppController and have my add, update, and delete actions defined in there.
I also have a boolean function in my AppController that checks if the appropriate pageInstanceIDs are in session or not.
Below is my code:
public function isNotPostBack() {
// pull in the session
$pageInstanceIDs = Session::read('pageInstanceIDs');
$pageInstanceID = uniqid('', true);
$this->set(compact('pageInstanceID'));
$pageInstanceIDs[] = $pageInstanceID;
Session::write('pageInstanceIDs', $pageInstanceIDs);
// checks if this is a save operation
if ($this->request->data){
$pageInstanceIDs = Session::read('pageInstanceIDs');
$pageIDIndex = array_search($this->request->data['pageInstanceID'], $pageInstanceIDs);
if ($pageIDIndex !== false) {
// remove the key
unset($pageInstanceIDs[$pageIDIndex]);
Session::write('pageInstanceIDs', $pageInstanceIDs);
return true;
}
else
return false;
} else {
return true;
}
}
public function add() {
if (!$this->request->is('post') && exist($this->request->data())) {
$msg = "Add can only be called with http:post.";
throw new DispatchException($msg);
}
}
Then in my controllers I inherit from AppController and implement the action like so:
public function add() {
parent::add();
if (parent::isNotPostBack()){
//do work
}
return $this->render(array('layout' => false));
}
which will ensure that the form used a POST and was not double submitted (back button or click happy users). This also helps protect against XSS.
I'm aware there is a plugin for this, but I want to implement this as a filter so that my controller methods are cleaner. Implented this way, the only code in my actions are the //do work portion and the return statement.
You should probably start with a filter on lithium\action\Dispatcher::run() here is some pseudo code. Can't help too much without seeing your parent::isNotPostBack() method but this should get you on the right track.
<?php
use lithium\action\Dispatcher;
Dispatcher::applyFilter('run', function($self, $params, $chain) {
$request = $params['request'];
// Request method is in $request->method
// Post data is in $request->data
if($not_your_conditions) {
return new Response(); // set up your custom response
}
return $chain->next($self, $params, $chain); // to continue on the path of execution
});
First of all, use the integrated CSRF (XSRF) protection.
The RequestToken class creates cryptographically-secure tokens and keys that can be used to validate the authenticity of client requests.
— http://li3.me/docs/lithium/security/validation/RequestToken
Check the CSRF token this way:
if ($this->request->data && !RequestToken::check($this->request)) {
/* do your stuff */
}
You can even check the HTTP method used via is()
$this->request->is('post');
The problem of filters (for that use case) is that they are very generic. So if you don't want to write all your actions as filterable code (which might be painful and overkill), you'll have to find a way to define which method blocks what and filter the Dispatcher::_call.
For CSRF protection, I use something similar to greut's suggestion.
I have this in my extensions/action/Controller.php
protected function _init() {
parent::_init();
if ($this->request->is('post') ||
$this->request->is('put') ||
$this->request->is('delete')) {
//on add, update and delete, if the security token exists, we will verify the token
if ('' != Session::read('security.token') && !RequestToken::check($this->request)) {
RequestToken::get(array('regenerate' => true));
throw new DispatchException('There was an error submitting the form.');
}
}
}
Of course, this means you'd have to also add the following to the top of your file:
use \lithium\storage\Session;
use lithium\security\validation\RequestToken;
use lithium\action\DispatchException;
With this, I don't have to repeatedly check for CSRF.
I implemented something similar in a recent project by subclassing \lithium\action\Controller as app\controllers\ApplicationController (abstract) and applying filters to invokeMethod(), as that's how the dispatcher invokes the action methods. Here's the pertinent chunk:
namespace app\controllers;
class ApplicationController extends \lithium\action\Controller {
/**
* Essential because you cannot invoke `parent::invokeMethod()` from within the closure passed to `_filter()`... But it makes me sad.
*
* #see \lithium\action\Controller::invokeMethod()
*
* #param string $method to be invoked with $arguments
* #param array $arguments to pass to $method
*/
public function _invokeMethod($method, array $arguments = array()) {
return parent::invokeMethod($method, $arguments);
}
/**
* Overridden to make action methods filterable with `applyFilter()`
*
* #see \lithium\action\Controller::invokeMethod()
* #see \lithium\core\Object::applyFilter()
*
* #param string $method to be invoked with $arguments
* #param array $arguments to pass to $method
*/
public function invokeMethod($method, array $arguments = array()) {
return $this->_filter(__METHOD__, compact('method', 'arguments'), function($self, $params){
return $self->_invokeMethod($params['method'], $params['arguments']);
});
}
}
Then you can use applyFilter() inside of _init() to run filters on your method. Instead of checking $method in every filter, you can opt to change _filter(__METHOD__, . . .) to _filter($method, . . .), but we chose to keep the more generic filter.

Categories