* @copyright Since 2007 PrestaShop SA and Contributors * @license https://opensource.org/licenses/AFL-3.0 Academic Free License version 3.0 */ declare(strict_types=1); namespace PrestaShop\Module\Mbo\Service; use Exception; /** * Because the Core's HookManager makes the exceptions from hooks silent, * this service will hold exceptions from hooks in case we want to reuse/log/throw them */ class HookExceptionHolder { private $listenedHooks = []; public function reset(): void { $this->listenedHooks = []; } public function listenFor(string $hookName): void { $this->listenedHooks[$hookName] = [ 'exception' => null, ]; } public function holdException(string $hookName, Exception $exception): void { if (!array_key_exists($hookName, $this->listenedHooks)) { // Hook exceptions not listened. Throw Exception ? log ? return; } $this->listenedHooks[$hookName]['exception'] = $exception; } public function getLastException(string $hookName): ?Exception { if (!array_key_exists($hookName, $this->listenedHooks)) { // Hook exceptions not listened. Throw Exception ? log ? return null; } return $this->listenedHooks[$hookName]['exception']; } }