vendor/php-flasher/flasher/Notification/Envelope.php line 13

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the PHPFlasher package.
  4.  * (c) Younes KHOUBZA <younes.khoubza@gmail.com>
  5.  */
  6. namespace Flasher\Prime\Notification;
  7. use Flasher\Prime\Stamp\PresentableStampInterface;
  8. use Flasher\Prime\Stamp\StampInterface;
  9. final class Envelope implements NotificationInterface
  10. {
  11.     /**
  12.      * @var NotificationInterface
  13.      */
  14.     private $notification;
  15.     /**
  16.      * @var array<class-string<StampInterface>, StampInterface>
  17.      */
  18.     private $stamps = array();
  19.     /**
  20.      * @param StampInterface|StampInterface[] $stamps
  21.      */
  22.     public function __construct(NotificationInterface $notification$stamps = array())
  23.     {
  24.         $this->notification $notification;
  25.         $this->with(\is_array($stamps) ? $stamps : \array_slice(\func_get_args(), 1));
  26.     }
  27.     /**
  28.      * Dynamically call methods on the notification.
  29.      *
  30.      * @param string  $method
  31.      * @param mixed[] $parameters
  32.      *
  33.      * @return mixed
  34.      */
  35.     public function __call($method, array $parameters)
  36.     {
  37.         /** @var callable $callback */
  38.         $callback = array($this->getNotification(), $method);
  39.         return \call_user_func_array($callback$parameters);
  40.     }
  41.     /**
  42.      * Makes sure the notification is in an Envelope and adds the given stamps.
  43.      *
  44.      * @param StampInterface|StampInterface[] $stamps
  45.      *
  46.      * @return static
  47.      */
  48.     public static function wrap(NotificationInterface $notification$stamps = array())
  49.     {
  50.         $envelope $notification instanceof self $notification : new self($notification);
  51.         return $envelope->with(\is_array($stamps) ? $stamps : \array_slice(\func_get_args(), 1));
  52.     }
  53.     /**
  54.      * @param StampInterface|StampInterface[] $stamps
  55.      *
  56.      * @return static
  57.      */
  58.     public function with($stamps)
  59.     {
  60.         $stamps = \is_array($stamps) ? $stamps : \func_get_args();
  61.         foreach ($stamps as $stamp) {
  62.             $this->withStamp($stamp);
  63.         }
  64.         return $this;
  65.     }
  66.     /**
  67.      * @return static
  68.      */
  69.     public function withStamp(StampInterface $stamp)
  70.     {
  71.         $this->stamps[\get_class($stamp)] = $stamp;
  72.         return $this;
  73.     }
  74.     /**
  75.      * @param StampInterface|StampInterface[] $stamps
  76.      *
  77.      * @return static
  78.      */
  79.     public function without($stamps)
  80.     {
  81.         $stamps = \is_array($stamps) ? $stamps : \func_get_args();
  82.         foreach ($stamps as $stamp) {
  83.             $this->withoutStamp($stamp);
  84.         }
  85.         return $this;
  86.     }
  87.     /**
  88.      * @param class-string<StampInterface>|StampInterface $type
  89.      *
  90.      * @return static
  91.      */
  92.     public function withoutStamp($type)
  93.     {
  94.         $type $type instanceof StampInterface ? \get_class($type) : $type;
  95.         unset($this->stamps[$type]);
  96.         return $this;
  97.     }
  98.     /**
  99.      * @param class-string<StampInterface> $stampFqcn
  100.      *
  101.      * @return StampInterface|null
  102.      */
  103.     public function get($stampFqcn)
  104.     {
  105.         if (!isset($this->stamps[$stampFqcn])) {
  106.             return null;
  107.         }
  108.         return $this->stamps[$stampFqcn];
  109.     }
  110.     /**
  111.      * All stamps by their class name.
  112.      *
  113.      * @return array<class-string<StampInterface>, StampInterface>
  114.      */
  115.     public function all()
  116.     {
  117.         return $this->stamps;
  118.     }
  119.     /**
  120.      * The original notification contained in the envelope.
  121.      *
  122.      * @return NotificationInterface
  123.      */
  124.     public function getNotification()
  125.     {
  126.         return $this->notification;
  127.     }
  128.     /**
  129.      * {@inheritdoc}
  130.      */
  131.     public function getType()
  132.     {
  133.         return $this->notification->getType();
  134.     }
  135.     /**
  136.      * {@inheritdoc}
  137.      */
  138.     public function setType($type)
  139.     {
  140.         return $this->notification->setType($type); // @phpstan-ignore-line
  141.     }
  142.     /**
  143.      * {@inheritdoc}
  144.      */
  145.     public function getMessage()
  146.     {
  147.         return $this->notification->getMessage();
  148.     }
  149.     /**
  150.      * {@inheritdoc}
  151.      */
  152.     public function setMessage($message)
  153.     {
  154.         return $this->notification->setMessage($message); // @phpstan-ignore-line
  155.     }
  156.     /**
  157.      * {@inheritdoc}
  158.      */
  159.     public function getTitle()
  160.     {
  161.         return $this->notification->getTitle();
  162.     }
  163.     /**
  164.      * {@inheritdoc}
  165.      */
  166.     public function setTitle($title)
  167.     {
  168.         return $this->notification->setMessage($title); // @phpstan-ignore-line
  169.     }
  170.     /**
  171.      * {@inheritdoc}
  172.      */
  173.     public function getOptions()
  174.     {
  175.         return $this->notification->getOptions();
  176.     }
  177.     /**
  178.      * {@inheritdoc}
  179.      */
  180.     public function setOptions(array $options)
  181.     {
  182.         return $this->notification->setOptions($options); // @phpstan-ignore-line
  183.     }
  184.     /**
  185.      * {@inheritdoc}
  186.      */
  187.     public function getOption($name$default null)
  188.     {
  189.         return $this->notification->getOption($name$default);
  190.     }
  191.     /**
  192.      * {@inheritdoc}
  193.      */
  194.     public function setOption($name$value)
  195.     {
  196.         return $this->notification->setOption($name$value); // @phpstan-ignore-line
  197.     }
  198.     /**
  199.      * {@inheritdoc}
  200.      */
  201.     public function unsetOption($name)
  202.     {
  203.         return $this->notification->unsetOption($name); // @phpstan-ignore-line
  204.     }
  205.     /**
  206.      * {@inheritdoc}
  207.      */
  208.     public function toArray()
  209.     {
  210.         $array = array(
  211.             'notification' => $this->notification->toArray(),
  212.         );
  213.         foreach ($this->all() as $stamp) {
  214.             if ($stamp instanceof PresentableStampInterface) {
  215.                 $array array_merge($array$stamp->toArray());
  216.             }
  217.         }
  218.         return $array;
  219.     }
  220. }