Skip to content

Commit

Permalink
Fix all PHPStan level MAX errors
Browse files Browse the repository at this point in the history
  • Loading branch information
antonioribeiro committed Aug 16, 2024
1 parent fc4fd60 commit 6e153e6
Show file tree
Hide file tree
Showing 22 changed files with 169 additions and 110 deletions.
2 changes: 1 addition & 1 deletion src/Behaviours/CachedOnCDN.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public function edgeFlushIsEnabled(): bool
return Helpers::configBool('edge-flush.enabled.package', false);
}

public function invalidationsAreEnabled()
public function invalidationsAreEnabled(): bool
{
return Helpers::configBool('edge-flush.enabled.services.invalidation', false);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Behaviours/CastObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ trait CastObject
{
public function getInternalModel(Model $model): Model
{
$internal = config('edge-flush.tags.external-models.'.get_class($model));
$internal = Helpers::configString('edge-flush.tags.external-models.'.get_class($model));

if (blank($internal)) {
return $model;
Expand Down
23 changes: 16 additions & 7 deletions src/Behaviours/MakeTag.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace A17\EdgeFlush\Behaviours;

use Illuminate\Support\Str;
use A17\EdgeFlush\EdgeFlush;
use A17\EdgeFlush\Services\Entity;
use A17\EdgeFlush\Support\Helpers;
Expand Down Expand Up @@ -52,6 +53,7 @@ public function getCDNCacheTagFromModel(mixed $model, string $key = null, string
return null;
}

/** @phpstan-ignore-next-line */
return $model->getCDNCacheTag($key, $type);
}

Expand All @@ -77,18 +79,25 @@ public function encodeValueForComparison(mixed $value, string|null $type = null)
}

if ($type === 'boolean' || is_bool($value)) {
return !!$value ? 'true' : 'false';
return (bool) $value ? 'true' : 'false';
}

if ($type === 'string' || is_string($value)) {
return "'$value'";
}
if ($type === 'string' || $type === 'array' || $type === 'object') {
if (is_string($value)) {
return "'$value'";
}

if (is_array($value) || is_object($value)) {
$value = json_encode($value);

if ($type === 'array' || is_array($value)) {
return json_encode($value);
if (is_string($value)) {
return $value;
}
}
}

return (string) $value;
/// FIXME: we cannot cast this value to a string, so we are generating a random string that will not match anything else, fow now
return '--- cannot cast to string --- ' . Str::random(16);
}

public function granularPropertyIsAllowed(string $name, Model|string $model): bool
Expand Down
2 changes: 1 addition & 1 deletion src/Console/Commands/ConfigMergeSection.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ public function extractInternalArray(string|false|null $content): string|null
return null;
}

$lines = is_string($content) ? explode("\n", $content) : [];
$lines = explode("\n", $content);

$result = [];

Expand Down
2 changes: 2 additions & 0 deletions src/Contracts/CDNService.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,6 @@ public function maxUrls(): int;
public function invalidationIsCompleted(string $invalidationId): bool;

public function enabled(): bool;

public function canInvalidateAll(): bool;
}
5 changes: 4 additions & 1 deletion src/EdgeFlush.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,17 @@
* @method static bool storeTagsServiceIsEnabled()
* @method static bool warmerServiceIsEnabled()
* @method static string packageName()
* @method static bool match(string $patten, string $string)
**/
class EdgeFlush extends Facade
{
protected static function getFacadeAccessor(): string
{
$accessor = \A17\EdgeFlush\Services\EdgeFlush::class;

static::$app->afterResolving($accessor, fn() => app($accessor)->boot());
if (filled(static::$app)) {
static::$app->afterResolving($accessor, fn() => app($accessor)->boot());
}

return $accessor;
}
Expand Down
6 changes: 4 additions & 2 deletions src/Jobs/InvalidateModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,10 @@ public function __construct(Entity $entity)
*
* @return void
*/
public function handle()
public function handle(): void
{
EdgeFlush::tags()->dispatchInvalidationsForModel($this->entity);
if (filled($this->entity)) {
EdgeFlush::tags()->dispatchInvalidationsForModel($this->entity);
}
}
}
12 changes: 9 additions & 3 deletions src/Listeners/EloquentObserver.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public function forceDeleted(Model $model): void
$this->invalidate($model, 'deleted');
}

public function pivotSynced($observed, $model, $relationName, $changes)
public function pivotSynced(mixed $observed, Model $model, string $relationName, array $changes): void
{
$this->invalidate($model, 'pivot-synced', ['name' => $relationName, 'changes' => $changes]);
}
Expand All @@ -58,7 +58,13 @@ public function invalidate(Model $model, string $event, array $relation = []): v
return;
}

if ($this->dispatchedEvents->alreadyDispatched($event.'-'.$this->makeModelName($model))) {
if (blank($this->dispatchedEvents)) {
return;
}

$dispatched = $this->dispatchedEvents->alreadyDispatched($event.'-'.$this->makeModelName($model));

if (filled($dispatched) && $dispatched !== false) {
return;
}

Expand All @@ -76,7 +82,7 @@ public function invalidate(Model $model, string $event, array $relation = []): v
}
}

public function boot()
public function boot(): void
{
$this->dispatchedEvents = app('a17.edgeflush.dispatchedEvents');
}
Expand Down
3 changes: 2 additions & 1 deletion src/Middleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace A17\EdgeFlush;

use Closure;
use A17\EdgeFlush\Support\Helpers;

class Middleware
{
Expand All @@ -17,7 +18,7 @@ public function handle($request, Closure $next)
{
$response = $next($request);

if (config('edge-flush.enabled.package')) {
if (Helpers::configBool('edge-flush.enabled.package')) {
return EdgeFlush::setRequest($request)->makeResponse($response);
}

Expand Down
3 changes: 2 additions & 1 deletion src/Models/Url.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@
* @property string $url
* @property string $url_hash
* @property int $hits
* @property bool $was_purged_at
* @property \Illuminate\Support\Carbon $was_purged_at
* @property string $invalidation_id
* @property bool $canBeSaved
*/
class Url extends Model
{
Expand Down
35 changes: 22 additions & 13 deletions src/Services/Akamai/Service.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,30 +54,25 @@ public function getAuthHeaders(mixed $body): string
$auth->setHttpMethod('POST');

$auth->setAuth(
$this->getClientToken(),
$this->getClientSecret(),
$this->getAccessToken(),
(string) $this->getClientToken(),
(string) $this->getClientSecret(),
(string) $this->getAccessToken(),
);

$auth->setPath($this->getApiPath());

return $auth->createAuthHeader();
}

public function maxUrls(): int
{
return Helpers::configInt('edge-flush.services.akamai.max_urls') ?? 300;
}

public function invalidationIsCompleted(string $invalidationId): bool
{
/**
* Fast purge is supposed to be completed in seconds, so no need to do a request
* to check if it's completed.
*/
$url = Url::where('invalidation_id', $invalidationId)->take(1)->get()->first();
$url = Url::where('invalidation_id', $invalidationId)->first();

if (empty($url)) {
if (blank($url)) {
return true;
}

Expand All @@ -86,6 +81,14 @@ public function invalidationIsCompleted(string $invalidationId): bool

public function createInvalidationRequest(Invalidation|array $invalidation = null): Invalidation
{
if (blank($invalidation)) {
return new Invalidation();
}

if (is_array($invalidation)) {
$invalidation = (new Invalidation())->setUrls($invalidation);
}

$urls = $invalidation->urls()
->map(function ($item) {
return $item instanceof Url ? $item->url_hash : $item;
Expand Down Expand Up @@ -116,7 +119,7 @@ public function createInvalidationRequest(Invalidation|array $invalidation = nul
Helpers::debug('[AKAMAI] Invalidation completed in ' . $duration . ' seconds');

if ($response->failed()) {
Helpers::error('Error invalidating akamai tags: ' . $response->getBody());
Helpers::error('Error invalidating akamai tags: ' . $response->body());

$invalidation->setSuccess(false);

Expand All @@ -125,9 +128,15 @@ public function createInvalidationRequest(Invalidation|array $invalidation = nul

$invalidation->setSuccess(true);

$invalidation->setId($response->json('purgeId'));
$json = $response->json();

if (!is_array($json)) {
$json = [];
}

$invalidation->setId($json['purgeId'] ?? null);

$invalidation->setInvalidationResponse($response->json());
$invalidation->setInvalidationResponse($json);

return $invalidation;
}
Expand Down
9 changes: 9 additions & 0 deletions src/Services/BaseService.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ abstract class BaseService implements ServiceContract

protected bool|null $enabled = null;

protected function instantiate(): void
{
}

public function addHeadersToResponse(Response $response, string $service, string $tag): Response
{
if (!$this->enabled()) {
Expand Down Expand Up @@ -135,4 +139,9 @@ public function createInvalidation(Invalidation|array $invalidation = null): Inv

return $invalidation;
}

public function canInvalidateAll(): bool
{
return true;
}
}
6 changes: 3 additions & 3 deletions src/Services/CacheControl.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public function isCachable(Response $response): bool
}

if (filled($this->_isCachable)) {
return (bool) $this->_isCachable;
return $this->_isCachable;
}

return $this->_isCachable = !$this->getCachableMatrix($response)->contains(false);
Expand Down Expand Up @@ -199,7 +199,7 @@ protected function middlewaresAllowCaching(): bool
? ['no-middleware']
: $route->action['middleware'] ?? null;

if (blank($middleware) || is_null($middleware)) {
if (blank($middleware)) {
return false;
}

Expand Down Expand Up @@ -452,7 +452,7 @@ public function willBeCached(Response $response, string|null $strategy = null):

public function strategyDoesntContainsNoStoreDirectives(string $strategy): bool
{
return !!Helpers::collect(explode(',', $strategy))->reduce(function ($willCache, $element) {
return Helpers::collect(explode(',', $strategy))->reduce(function (bool $willCache, mixed $element) {
if (!is_string($element)) {
return $willCache;
}
Expand Down
38 changes: 13 additions & 25 deletions src/Services/CdnBaseService.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

abstract class CdnBaseService extends BaseService implements CDNService
{
protected static string $serviceName = 'not-assigned';

public function __construct()
{
if ($this->enabled()) {
Expand Down Expand Up @@ -80,7 +82,7 @@ public function invalidatePaths(Invalidation $invalidation): Invalidation
{
return $this->createInvalidationRequest($invalidation);
}

public function maxUrls(): int
{
return Helpers::configInt('edge-flush.services.'.static::$serviceName.'.max_urls') ?? 300;
Expand All @@ -93,39 +95,25 @@ public function enabled(): bool
$this->isProperlyConfigured();
}

public function invalidationIsCompleted(string $invalidationId): bool
{
$response = $this->getInvalidation($invalidationId);

if (blank($response)) {
return false;
}

return Invalidation::factory($response)->isCompleted();
}

public function getInvalidation(string $invalidationId): AwsResult
{
return $this->client->getInvalidation([
'DistributionId' => $this->getDistributionId(),
'Id' => $invalidationId,
]);
}

public function serviceIsEnabled(): bool
{
return Helpers::configBool('edge-flush.services.'.static::$serviceName.'.enabled', true);
}

public function getMaxUrls(): int
{
return Helpers::configInt('edge-flush.services.'.static::$serviceName.'.max_urls', 300);
}

public function canInvalidateAll(): bool
{
return collect(
Helpers::configString('edge-flush.services.'.static::$serviceName.'.invalidate_all_paths') ?? []
)->filter()->isNotEmpty();
}

public function createInvalidationRequest(Invalidation|array $invalidation = null): Invalidation
{
return new Invalidation();
}

public function isProperlyConfigured(): bool
{
return true;
}
}
Loading

0 comments on commit 6e153e6

Please sign in to comment.