From abf67a1c6a11b3f4a0054914a83f3d3ad379f4df Mon Sep 17 00:00:00 2001 From: Artem Vasilev Date: Wed, 13 Mar 2024 23:43:47 +0300 Subject: [PATCH] implement AbstractImmutableEvent --- .../src/Event/AbstractImmutableEvent.php | 114 ++++++++++++++++++ 1 file changed, 114 insertions(+) create mode 100644 com_oauthserver/administrator/src/Event/AbstractImmutableEvent.php diff --git a/com_oauthserver/administrator/src/Event/AbstractImmutableEvent.php b/com_oauthserver/administrator/src/Event/AbstractImmutableEvent.php new file mode 100644 index 0000000..169d29c --- /dev/null +++ b/com_oauthserver/administrator/src/Event/AbstractImmutableEvent.php @@ -0,0 +1,114 @@ + + * @license MIT; see LICENSE.txt + **/ + +namespace Webmasterskaya\Component\OauthServer\Administrator\Event; + +abstract class AbstractImmutableEvent extends \Joomla\CMS\Event\AbstractImmutableEvent +{ + /** + * A flag to see if the constructor has been already called. + * + * @var boolean + * @since version + */ + private bool $constructed = false; + + /** + * Constructor. + * + * @param string $name The event name. + * @param array $arguments The event arguments. + * + * @throws \BadMethodCallException + * @since version + * @noinspection PhpMissingParentConstructorInspection + */ + public function __construct(string $name, array $arguments = []) + { + if ($this->constructed) + { + throw new \BadMethodCallException( + sprintf('Cannot reconstruct the AbstractImmutableEvent %s.', $this->name) + ); + } + + $this->name = $name; + $this->arguments = []; + + foreach ($arguments as $argumentName => $value) + { + $this->setArgument($argumentName, $value); + } + + $this->constructed = true; + } + + public function setArgument($name, $value): AbstractImmutableEvent + { + if (!$this->constructed) + { + return parent::setArgument($name, $value); + } + + throw new \BadMethodCallException( + sprintf( + 'Cannot set the argument %s of the immutable event %s.', + $name, + $this->name + ) + ); + } + + public function addArgument($name, $value): AbstractImmutableEvent + { + if (!$this->constructed) + { + return parent::addArgument($name, $value); + } + + throw new \BadMethodCallException( + sprintf( + 'Cannot add the argument %s of the immutable event %s.', + $name, + $this->name + ) + ); + } + + public function removeArgument($name) + { + if (!$this->constructed) + { + return parent::removeArgument($name); + } + + throw new \BadMethodCallException( + sprintf( + 'Cannot remove the argument %s of the immutable event %s.', + $name, + $this->name + ) + ); + } + + public function clearArguments(): array + { + if (!$this->constructed) + { + return parent::clearArguments(); + } + + throw new \BadMethodCallException( + sprintf( + 'Cannot clear arguments of the immutable event %s.', + $this->name + ) + ); + } +}