throw exception if present does not exists

This commit is contained in:
KHOUBZA Younes
2022-05-28 17:02:04 +01:00
parent 6839affa41
commit f89cadc6a3
2 changed files with 28 additions and 2 deletions
@@ -8,6 +8,7 @@
namespace Flasher\Prime\EventDispatcher\EventListener;
use Flasher\Prime\EventDispatcher\Event\PersistEvent;
use Flasher\Prime\Exception\PresetNotFoundException;
use Flasher\Prime\Notification\Envelope;
use Flasher\Prime\Stamp\PresetStamp;
@@ -21,7 +22,7 @@ use Flasher\Prime\Stamp\PresetStamp;
* }
* }
*/
class PresetListener implements EventSubscriberInterface
final class PresetListener implements EventSubscriberInterface
{
/**
* @phpstan-var PresetType[]
@@ -65,7 +66,7 @@ class PresetListener implements EventSubscriberInterface
}
if (!isset($this->presets[$preset->getPreset()])) {
return;
throw new PresetNotFoundException($preset->getPreset(), array_keys($this->presets));
}
$preset = $this->presets[$preset->getPreset()];
@@ -0,0 +1,25 @@
<?php
/*
* This file is part of the PHPFlasher package.
* (c) Younes KHOUBZA <younes.khoubza@gmail.com>
*/
namespace Flasher\Prime\Exception;
final class PresetNotFoundException extends \Exception
{
/**
* @param string $preset
* @param string[] $availablePresets
*/
public function __construct($preset, array $availablePresets = array())
{
$message = sprintf('Preset "%s" not found, did you forget to register it?', $preset);
if (array() !== $availablePresets) {
$message .= sprintf(' Available presets: %s', implode(', ', $availablePresets));
}
parent::__construct($message);
}
}