Skip to content

Commit

Permalink
Allow an array of checks
Browse files Browse the repository at this point in the history
Signed-off-by: Lloric Mayuga Garcia <[email protected]>
  • Loading branch information
lloricode committed Mar 9, 2024
1 parent 7d556d7 commit e438816
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 16 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

All notable changes to `spatie-image-optimizer-health-check` will be documented in this file.

## 2.0.3 - 2023-03-09

- Allow an array of checks

## 2.0.2 - 2023-03-09

- Optimize code for php 8.2
Expand Down
39 changes: 24 additions & 15 deletions src/ImageOptimizerCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,16 @@

namespace Lloricode\SpatieImageOptimizerHealthCheck;

use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Process;
use Spatie\Health\Checks\Check;
use Spatie\Health\Checks\Result;

class ImageOptimizerCheck extends Check
{
/**
* @var array<int, \Lloricode\SpatieImageOptimizerHealthCheck\Optimizer>|null
*/
private ?array $checks = null;

private int $timeout = 60;
Expand All @@ -21,10 +25,15 @@ public function timeout(int $timeout): self
return $this;
}

public function addCheck(Optimizer $optimizer): self
/**
* @param \Lloricode\SpatieImageOptimizerHealthCheck\Optimizer|array<int, \Lloricode\SpatieImageOptimizerHealthCheck\Optimizer> $optimizers
*/
public function addChecks(array|Optimizer $optimizers): self
{
if (! in_array($optimizer, $this->checks ?? [])) {
$this->checks[] = $optimizer;
foreach (Arr::wrap($optimizers) as $optimizer) {
if (! in_array($optimizer, $this->checks ?? [])) {
$this->checks[] = $optimizer;
}
}

return $this;
Expand Down Expand Up @@ -78,39 +87,39 @@ private function shouldPerformCheck(Optimizer $optimizer): bool
return in_array($optimizer, $this->checks);
}

/** @deprecated use addCheck() will be removed on v3 */
/** @deprecated use addChecks() will be removed on v3 */
public function checkJPEGOPTIM(): self
{
return $this->addCheck(Optimizer::JPEGOPTIM);
return $this->addChecks(Optimizer::JPEGOPTIM);
}

/** @deprecated use addCheck() will be removed on v3 */
/** @deprecated use addChecks() will be removed on v3 */
public function checkOPTIPNG(): self
{
return $this->addCheck(Optimizer::OPTIPNG);
return $this->addChecks(Optimizer::OPTIPNG);
}

/** @deprecated use addCheck() will be removed on v3 */
/** @deprecated use addChecks() will be removed on v3 */
public function checkPNGQUANT(): self
{
return $this->addCheck(Optimizer::PNGQUANT);
return $this->addChecks(Optimizer::PNGQUANT);
}

/** @deprecated use addCheck() will be removed on v3 */
/** @deprecated use addChecks() will be removed on v3 */
public function checkSVGO(): self
{
return $this->addCheck(Optimizer::SVGO);
return $this->addChecks(Optimizer::SVGO);
}

/** @deprecated use addCheck() will be removed on v3 */
/** @deprecated use addChecks() will be removed on v3 */
public function checkGIFSICLE(): self
{
return $this->addCheck(Optimizer::GIFSICLE);
return $this->addChecks(Optimizer::GIFSICLE);
}

/** @deprecated use addCheck() will be removed on v3 */
/** @deprecated use addChecks() will be removed on v3 */
public function checkWEBP(): self
{
return $this->addCheck(Optimizer::WEBP);
return $this->addChecks(Optimizer::WEBP);
}
}
2 changes: 1 addition & 1 deletion tests/CheckTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
]);

$result = (new ImageOptimizerCheck())
->addCheck($optimizer)
->addChecks($optimizer)
->run();

expect($result->status)
Expand Down

0 comments on commit e438816

Please sign in to comment.