Skip to content

Commit

Permalink
Fixes some issues introduced by PHPStan (#14)
Browse files Browse the repository at this point in the history
* Fixes phpstan issues

* Apply fixes from StyleCI

* Update middleware's documentation
  • Loading branch information
richan-fongdasen authored Jun 14, 2019
1 parent 1c4beb8 commit da5a365
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 14 deletions.
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -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().#'

4 changes: 2 additions & 2 deletions src/Events/ModelHasUpdated.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class ModelHasUpdated
/**
* Eloquent model object.
*
* @var \Illuminate\Database\Eloquent\Model
* @var mixed
*/
protected $model;

Expand Down Expand Up @@ -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;
}
}
30 changes: 23 additions & 7 deletions src/Middleware/CacheableByVarnish.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
20 changes: 18 additions & 2 deletions src/Middleware/UncacheableByVarnish.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
20 changes: 19 additions & 1 deletion src/VarnishableObserver.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -25,14 +41,16 @@ public function deleted(Model $model) :void
*
* @param \Illuminate\Database\Eloquent\Model $model
*
* @throws Exception
*
* @return void
*/
protected function handleModelInitialization(Model $model) :void
{
$updatedAt = $model->getAttribute('updated_at');

if ($updatedAt !== null) {
\Varnishable::setLastModifiedHeader($updatedAt);
$this->varnishable->setLastModifiedHeader($updatedAt);
}
}

Expand Down

0 comments on commit da5a365

Please sign in to comment.