diff --git a/.travis.yml b/.travis.yml index 215e073..7c0d000 100644 --- a/.travis.yml +++ b/.travis.yml @@ -32,7 +32,8 @@ before_script: - set +H script: - - vendor/bin/phpunit + - composer analyse + - if [[ $COVERAGE != '1' ]]; then vendor/bin/phpunit; fi - if [[ $COVERAGE = '1' ]]; then phpdbg -qrr vendor/bin/phpunit --coverage-clover=clover.xml; fi after_script: diff --git a/phpstan.neon b/phpstan.neon index 6914bb8..b8481cf 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -5,5 +5,5 @@ includes: parameters: level: 7 ignoreErrors: - - '#Call to static method [a-zA-Z0-9\\_]+\(\) on an unknown class Varnishable.#' + - '#Call to an undefined method Illuminate\\Database\\Eloquent\\Builder::withTrashed().#' diff --git a/src/Events/ModelHasUpdated.php b/src/Events/ModelHasUpdated.php index e0edbcb..ee97689 100644 --- a/src/Events/ModelHasUpdated.php +++ b/src/Events/ModelHasUpdated.php @@ -22,7 +22,7 @@ class ModelHasUpdated /** * Eloquent model object. * - * @var \Illuminate\Database\Eloquent\Model + * @var mixed */ protected $model; @@ -108,6 +108,6 @@ protected function retrieveModel() :?Model $this->model = $this->getQuery($model)->find(data_get($this->data, $model->getKeyName())); - return $this->model; + return ($this->model instanceof Model) ? $this->model : null; } } diff --git a/src/Middleware/CacheableByVarnish.php b/src/Middleware/CacheableByVarnish.php index ebae34d..2e7c6ac 100644 --- a/src/Middleware/CacheableByVarnish.php +++ b/src/Middleware/CacheableByVarnish.php @@ -3,29 +3,45 @@ namespace RichanFongdasen\Varnishable\Middleware; use Closure; +use RichanFongdasen\Varnishable\VarnishableService; use Symfony\Component\HttpFoundation\Request; class CacheableByVarnish { + /** + * Varnishable Service Object. + * + * @var \RichanFongdasen\Varnishable\VarnishableService + */ + protected $varnishable; + + /** + * CacheableByVarnish Middleware constructor. + */ + public function __construct() + { + $this->varnishable = app(VarnishableService::class); + } + /** * Handle an incoming request. * * @param \Symfony\Component\HttpFoundation\Request $request * @param \Closure $next - * @param int|null $cacheDuration + * @param int $cacheDuration * - * @return mixed + * @return \Symfony\Component\HttpFoundation\Response */ - public function handle(Request $request, Closure $next, $cacheDuration = null) + public function handle(Request $request, Closure $next, int $cacheDuration = 0) { - \Varnishable::setRequestHeaders($request->headers); + $this->varnishable->setRequestHeaders($request->headers); - if ((int) $cacheDuration > 0) { - \Varnishable::setCacheDuration($cacheDuration); + if ($cacheDuration > 0) { + $this->varnishable->setCacheDuration($cacheDuration); } $response = $next($request); - return \Varnishable::manipulate($response); + return $this->varnishable->manipulate($response); } } diff --git a/src/Middleware/UncacheableByVarnish.php b/src/Middleware/UncacheableByVarnish.php index 2dd7395..cdf1734 100644 --- a/src/Middleware/UncacheableByVarnish.php +++ b/src/Middleware/UncacheableByVarnish.php @@ -3,23 +3,39 @@ namespace RichanFongdasen\Varnishable\Middleware; use Closure; +use RichanFongdasen\Varnishable\VarnishableService; use Symfony\Component\HttpFoundation\Request; class UncacheableByVarnish { + /** + * Varnishable Service Object. + * + * @var \RichanFongdasen\Varnishable\VarnishableService + */ + protected $varnishable; + + /** + * UncacheableByVarnish Middleware constructor. + */ + public function __construct() + { + $this->varnishable = app(VarnishableService::class); + } + /** * Handle an incoming request. * * @param \Symfony\Component\HttpFoundation\Request $request * @param \Closure $next * - * @return mixed + * @return \Symfony\Component\HttpFoundation\Response */ public function handle(Request $request, Closure $next) { $response = $next($request); - \Varnishable::addUncacheableHeader($response); + $this->varnishable->addUncacheableHeader($response); return $response; } diff --git a/src/VarnishableObserver.php b/src/VarnishableObserver.php index e014760..dd500ff 100644 --- a/src/VarnishableObserver.php +++ b/src/VarnishableObserver.php @@ -2,11 +2,27 @@ namespace RichanFongdasen\Varnishable; +use Exception; use Illuminate\Database\Eloquent\Model; use RichanFongdasen\Varnishable\Events\ModelHasUpdated; class VarnishableObserver { + /** + * Varnishable Service Object. + * + * @var \RichanFongdasen\Varnishable\VarnishableService + */ + protected $varnishable; + + /** + * Varnishable Observer constructor. + */ + public function __construct() + { + $this->varnishable = app(VarnishableService::class); + } + /** * Listening to any saved events. * @@ -25,6 +41,8 @@ public function deleted(Model $model) :void * * @param \Illuminate\Database\Eloquent\Model $model * + * @throws Exception + * * @return void */ protected function handleModelInitialization(Model $model) :void @@ -32,7 +50,7 @@ protected function handleModelInitialization(Model $model) :void $updatedAt = $model->getAttribute('updated_at'); if ($updatedAt !== null) { - \Varnishable::setLastModifiedHeader($updatedAt); + $this->varnishable->setLastModifiedHeader($updatedAt); } }