From 6adb6d5d42581180a6dab63da6f0111a0e28d3c6 Mon Sep 17 00:00:00 2001 From: Matthew Baggett Date: Fri, 19 Jun 2020 09:53:59 +0200 Subject: [PATCH] Fixing PHPStan issues. --- src/App.php | 48 +---- src/Controllers/Controller.php | 8 +- src/Controllers/CrudController.php | 4 +- src/Controllers/Filters/Filter.php | 177 ++++++++++++++++++ src/Controllers/Filters/FilterCondition.php | 17 ++ src/Controllers/HtmlController.php | 4 +- .../BenzineConfigurationException.php | 2 +- src/Exceptions/BenzineException.php | 2 +- src/Exceptions/DbConfigException.php | 2 +- src/Exceptions/FilterDecodeException.php | 7 + src/Middlewares/AccessRequirements.php | 6 +- src/Middlewares/CORSHeadersOnResponse.php | 4 +- .../EnvironmentHeadersOnResponse.php | 24 ++- src/Middlewares/ForceSSLMiddleware.php | 4 +- src/Middlewares/JSONResponseLinter.php | 4 +- src/Services/ConfigurationService.php | 1 + src/Services/QueueService.php | 2 +- src/Workers/AbstractQueueWorker.php | 7 +- src/Workers/AbstractWorker.php | 2 +- src/Workers/WorkerWorkItem.php | 2 +- 20 files changed, 242 insertions(+), 85 deletions(-) create mode 100644 src/Controllers/Filters/Filter.php create mode 100644 src/Controllers/Filters/FilterCondition.php create mode 100644 src/Exceptions/FilterDecodeException.php diff --git a/src/App.php b/src/App.php index b044036..3f2a4fc 100644 --- a/src/App.php +++ b/src/App.php @@ -325,19 +325,11 @@ class App return $this; } - public function makeClean(): self - { - $this->setup(); - $this->loadAllRoutes(); - - return $this; - } - public static function Log(int $level = Logger::DEBUG, $message) { return self::Instance() ->getContainer() - ->get(Log\Logger::class) + ->get(Logger::class) ->log($level, ($message instanceof \Exception) ? $message->__toString() : $message) ; } @@ -355,44 +347,6 @@ class App return $this; } - public static function waitForMySQLToBeReady($connection = null) - { - if (!$connection) { - /** @var DbConfig $configs */ - $dbConfig = self::Instance()->getContainer()->get(DatabaseConfig::class); - $configs = $dbConfig->__toArray(); - - if (isset($configs['Default'])) { - $connection = $configs['Default']; - } else { - foreach ($configs as $option => $connection) { - self::waitForMySQLToBeReady($connection); - } - - return; - } - } - - $ready = false; - echo "Waiting for MySQL ({$connection['hostname']}:{$connection['port']}) to come up..."; - while (false == $ready) { - $conn = @fsockopen($connection['hostname'], $connection['port']); - if (is_resource($conn)) { - fclose($conn); - $ready = true; - } else { - echo '.'; - usleep(500000); - } - } - echo " [DONE]\n"; - - /** @var Services\EnvironmentService $environmentService */ - $environmentService = self::Container()->get(Services\EnvironmentService::class); - - $environmentService->rebuildEnvironmentVariables(); - } - public function runHttp(): void { $this->app->run(); diff --git a/src/Controllers/Controller.php b/src/Controllers/Controller.php index 14b9593..3d71ffc 100644 --- a/src/Controllers/Controller.php +++ b/src/Controllers/Controller.php @@ -2,10 +2,10 @@ namespace Benzine\Controllers; -use Slim\Http\Request; -use Slim\Http\Response; -use ⌬\Filters\Exceptions\FilterDecodeException; -use ⌬\Filters\Filter; +use Benzine\Exceptions\FilterDecodeException; +use Benzine\Controllers\Filters\Filter; +use Slim\Psr7\Request; +use Slim\Psr7\Response; abstract class Controller { diff --git a/src/Controllers/CrudController.php b/src/Controllers/CrudController.php index efd6a95..3d46e69 100644 --- a/src/Controllers/CrudController.php +++ b/src/Controllers/CrudController.php @@ -4,8 +4,8 @@ namespace Benzine\Controllers; use Benzine\ORM\Interfaces\ModelInterface; use Laminas\Db\Adapter\Exception\InvalidQueryException; -use Slim\Http\Request; -use Slim\Http\Response; +use Slim\Psr7\Request; +use Slim\Psr7\Response; abstract class CrudController extends Controller { diff --git a/src/Controllers/Filters/Filter.php b/src/Controllers/Filters/Filter.php new file mode 100644 index 0000000..5f669f4 --- /dev/null +++ b/src/Controllers/Filters/Filter.php @@ -0,0 +1,177 @@ +orderDirection; + } + + /** + * @param mixed $orderDirection + * + * @throws FilterDecodeException + * + * @return Filter + */ + public function setOrderDirection($orderDirection): self + { + if (!in_array(strtoupper($orderDirection), ['ASC', 'DESC', 'RAND'], true)) { + throw new FilterDecodeException("Failed to decode Filter Order, Direction unknown: {$orderDirection} must be ASC|DESC|RAND"); + } + $this->orderDirection = strtoupper($orderDirection); + + return $this; + } + + /** + * @param $header + * + * @throws FilterDecodeException + */ + public function parseFromHeader($header): self + { + foreach ($header as $key => $value) { + switch ($key) { + case 'limit': + $this->setLimit($value); + + break; + case 'offset': + $this->setOffset($value); + + break; + case 'wheres': + $this->setWheres($value); + + break; + case 'order': + $this->parseOrder($value); + + break; + default: + throw new FilterDecodeException("Failed to decode Filter, unknown key: {$key}"); + } + } + + return $this; + } + + /** + * @return mixed + */ + public function getLimit() + { + return $this->limit; + } + + /** + * @param mixed $limit + * + * @return Filter + */ + public function setLimit($limit): self + { + $this->limit = $limit; + + return $this; + } + + /** + * @return mixed + */ + public function getOffset() + { + return $this->offset; + } + + /** + * @param mixed $offset + * + * @return Filter + */ + public function setOffset($offset): self + { + $this->offset = $offset; + + return $this; + } + + /** + * @return mixed + */ + public function getWheres() + { + return $this->wheres; + } + + /** + * @param mixed $wheres + * + * @return Filter + */ + public function setWheres($wheres): self + { + $this->wheres = $wheres; + + return $this; + } + + /** + * @return mixed + */ + public function getOrder() + { + return $this->order; + } + + /** + * @param mixed $order + * + * @return Filter + */ + public function setOrder($order): self + { + $this->order = $order; + + return $this; + } + + public function setOrderRandom(): self + { + $this->setOrder(new Expression('RAND()')); + + return $this; + } + + public function parseOrder($orderArray): self + { + if (in_array(strtolower($orderArray['column']), ['rand', 'random', 'rand()'], true)) { + $this->setOrderRandom(); + } elseif (isset($orderArray['column'], $orderArray['direction'])) { + $this->setOrder($orderArray['column']); + + if (isset($orderArray['direction'])) { + $this->setOrderDirection($orderArray['direction']); + } + } else { + throw new FilterDecodeException("Could not find properties 'column' or 'direction' of the order array given."); + } + + return $this; + } +} diff --git a/src/Controllers/Filters/FilterCondition.php b/src/Controllers/Filters/FilterCondition.php new file mode 100644 index 0000000..a65ebf1 --- /dev/null +++ b/src/Controllers/Filters/FilterCondition.php @@ -0,0 +1,17 @@ +'; + public const CONDITION_LESS_THAN = '<'; + public const CONDITION_GREATER_THAN_OR_EQUAL = '>='; + public const CONDITION_LESS_THAN_OR_EQUAL = '<='; + public const CONDITION_LIKE = 'LIKE'; + public const CONDITION_NOT_LIKE = 'NOT LIKE'; + public const CONDITION_IN = 'IN'; + public const CONDITION_NOT_IN = 'NOT IN'; +} diff --git a/src/Controllers/HtmlController.php b/src/Controllers/HtmlController.php index ac53962..ba2e448 100644 --- a/src/Controllers/HtmlController.php +++ b/src/Controllers/HtmlController.php @@ -2,8 +2,8 @@ namespace Benzine\Controllers; -use Slim\Http\Request; -use Slim\Http\Response; +use Slim\Psr7\Request; +use Slim\Psr7\Response; use Slim\Views\Twig; abstract class HtmlController extends Controller diff --git a/src/Exceptions/BenzineConfigurationException.php b/src/Exceptions/BenzineConfigurationException.php index 3cfff5f..9e6fca0 100644 --- a/src/Exceptions/BenzineConfigurationException.php +++ b/src/Exceptions/BenzineConfigurationException.php @@ -2,6 +2,6 @@ namespace Benzine\Exceptions; -class BenzineConfigurationException extends Exception +class BenzineConfigurationException extends BenzineException { } diff --git a/src/Exceptions/BenzineException.php b/src/Exceptions/BenzineException.php index 093739a..38c6940 100644 --- a/src/Exceptions/BenzineException.php +++ b/src/Exceptions/BenzineException.php @@ -2,6 +2,6 @@ namespace Benzine\Exceptions; -class Exception extends \Exception +class BenzineException extends \Exception { } diff --git a/src/Exceptions/DbConfigException.php b/src/Exceptions/DbConfigException.php index 3a6d3b3..b1e6db6 100644 --- a/src/Exceptions/DbConfigException.php +++ b/src/Exceptions/DbConfigException.php @@ -2,6 +2,6 @@ namespace Benzine\Exceptions; -class DbConfigException extends Exception +class DbConfigException extends BenzineException { } diff --git a/src/Exceptions/FilterDecodeException.php b/src/Exceptions/FilterDecodeException.php new file mode 100644 index 0000000..7dc8106 --- /dev/null +++ b/src/Exceptions/FilterDecodeException.php @@ -0,0 +1,7 @@ +configuration = $configuration; @@ -38,8 +37,8 @@ class EnvironmentHeadersOnResponse $json = json_decode($body->getContents(), true); $gitVersion = null; - if (file_exists($this->configuration->get(Configuration\Configuration::KEY_APP_ROOT).'/version.txt')) { - $gitVersion = trim(file_get_contents($this->configuration->get(Configuration\Configuration::KEY_APP_ROOT).'/version.txt')); + if (file_exists(APP_ROOT.'/version.txt')) { + $gitVersion = trim(file_get_contents(APP_ROOT.'/version.txt')); $gitVersion = explode(' ', $gitVersion, 2); $gitVersion = reset($gitVersion); } @@ -55,7 +54,7 @@ class EnvironmentHeadersOnResponse 'Human' => date('Y-m-d H:i:s'), 'Epoch' => time(), ], - 'Exec' => number_format(microtime(true) - $this->configuration->get(Configuration\Configuration::KEY_APP_START), 4).' sec', + 'Exec' => number_format(microtime(true) - APP_START, 4).' sec', ] : null, 'Memory' => defined('DEBUG') && DEBUG ? [ 'Used' => number_format(memory_get_usage(false) / 1024 / 1024, 2).'MB', @@ -63,7 +62,6 @@ class EnvironmentHeadersOnResponse 'Limit' => ini_get('memory_limit'), ] : null, 'SQL' => defined('DEBUG') && DEBUG ? $this->profiler->getQueriesArray() : null, - 'API' => defined('DEBUG') && DEBUG && class_exists('\Gone\SDK\Common\Profiler') ? \Gone\SDK\Common\Profiler::debugArray() : null, ]); if (isset($json['Status'])) { @@ -88,8 +86,8 @@ class EnvironmentHeadersOnResponse 'json' => $json, 'json_pretty_printed_rows' => explode("\n", json_encode($json, JSON_PRETTY_PRINT)), 'inline_css' => $this->renderInlineCss([ - $this->configuration->get(Configuration\Configuration::KEY_APP_ROOT).'/vendor/benzine/benzine-http-assets/css/reset.css', - $this->configuration->get(Configuration\Configuration::KEY_APP_ROOT).'/vendor/benzine/benzine-http-assets/css/api-explorer.css', + APP_ROOT.'/vendor/benzine/benzine-http-assets/css/reset.css', + APP_ROOT.'/vendor/benzine/benzine-http-assets/css/api-explorer.css', ]), ]); $response = $response->withHeader('Content-type', 'text/html'); diff --git a/src/Middlewares/ForceSSLMiddleware.php b/src/Middlewares/ForceSSLMiddleware.php index 15bfbf2..1b0fe48 100644 --- a/src/Middlewares/ForceSSLMiddleware.php +++ b/src/Middlewares/ForceSSLMiddleware.php @@ -2,8 +2,8 @@ namespace Benzine\Middleware; -use Slim\Http\Request; -use Slim\Http\Response; +use Slim\Psr7\Request; +use Slim\Psr7\Response; class ForceSSLMiddleware { diff --git a/src/Middlewares/JSONResponseLinter.php b/src/Middlewares/JSONResponseLinter.php index d6b4d4f..4a10995 100644 --- a/src/Middlewares/JSONResponseLinter.php +++ b/src/Middlewares/JSONResponseLinter.php @@ -2,8 +2,8 @@ namespace Benzine\Middleware; -use Slim\Http\Request; -use Slim\Http\Response; +use Slim\Psr7\Request; +use Slim\Psr7\Response; class JSONResponseLinter { diff --git a/src/Services/ConfigurationService.php b/src/Services/ConfigurationService.php index 3edad38..4320abf 100644 --- a/src/Services/ConfigurationService.php +++ b/src/Services/ConfigurationService.php @@ -88,6 +88,7 @@ class ConfigurationService { define('APP_ROOT', $this->appRoot); define('APP_NAME', $this->get('application/name')); + define('APP_START', microtime(true)); } /** diff --git a/src/Services/QueueService.php b/src/Services/QueueService.php index 4a9b2ba..d23e9d3 100644 --- a/src/Services/QueueService.php +++ b/src/Services/QueueService.php @@ -2,7 +2,7 @@ namespace Benzine\Services; -use ⌬\UUID\UUID; +use Gone\UUID\UUID; class QueueService { diff --git a/src/Workers/AbstractQueueWorker.php b/src/Workers/AbstractQueueWorker.php index fbd52e7..ad3907a 100644 --- a/src/Workers/AbstractQueueWorker.php +++ b/src/Workers/AbstractQueueWorker.php @@ -3,6 +3,7 @@ namespace Benzine\Workers; use Benzine\Services\EnvironmentService; +use Benzine\Services\QueueService; use Monolog\Logger; abstract class AbstractQueueWorker extends AbstractWorker @@ -35,7 +36,7 @@ abstract class AbstractQueueWorker extends AbstractWorker } $this->logger->debug( sprintf( - 'Listening to "%s" and outputting on %d channel(s)', + 'Worker %s: Listening to "%s" and outputting on %d channel(s)', $this->getClassWithoutNamespace(), $this->inputQueue, count($this->outputQueues) @@ -128,7 +129,9 @@ abstract class AbstractQueueWorker extends AbstractWorker foreach ($items as $item) { $processResults = $this->process($item); if (is_array($processResults)) { - $resultItems[] += $processResults; + foreach($processResults as $processResult) { + $resultItems[] = $processResult; + } } else { $resultItems[] = $processResults; } diff --git a/src/Workers/AbstractWorker.php b/src/Workers/AbstractWorker.php index 66647f4..90e4595 100644 --- a/src/Workers/AbstractWorker.php +++ b/src/Workers/AbstractWorker.php @@ -5,7 +5,7 @@ namespace Benzine\Workers; use Benzine\Services\EnvironmentService; use Monolog\Logger; -abstract class AbstractWorker +abstract class AbstractWorker implements WorkerInterface { protected Logger $logger; protected EnvironmentService $environmentService; diff --git a/src/Workers/WorkerWorkItem.php b/src/Workers/WorkerWorkItem.php index e6b5297..17f0452 100644 --- a/src/Workers/WorkerWorkItem.php +++ b/src/Workers/WorkerWorkItem.php @@ -2,7 +2,7 @@ namespace Benzine\Workers; -use ⌬\Controllers\Abstracts\Model; +use Benzine\ORM\Abstracts\Model; class WorkerWorkItem {