TheCelavi

decorate

May 18th, 2017
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.91 KB | None | 0 0
  1. class RepositoryDecorator
  2. {
  3.     /**
  4.      * @var RepositoryInterface
  5.      */
  6.     private $repository;
  7.  
  8.     /**
  9.      * @var \SplQueue
  10.      */
  11.     private $queue;
  12.  
  13.     public function __construct(RepositoryInterface $repository)
  14.     {
  15.         $this->repository = $repository;
  16.         $this->queue = new \SplQueue();
  17.     }
  18.  
  19.     public function on($name, $will)
  20.     {
  21.         $this->queue->enqueue(['name' => $name, 'value' => $will]);
  22.         return $this;
  23.     }
  24.  
  25.     public function __call($name, $arguments)
  26.     {
  27.         if (count($this->queue) > 0 && $name === $this->queue->bottom()['name']) {
  28.             $result = $this->queue->dequeue();
  29.  
  30.             if ($result['value'] instanceof \Exception) {
  31.                 throw $result['value'];
  32.             }
  33.  
  34.             return $result['value'];
  35.         }
  36.  
  37.         return call_user_func_array(array($this->repository, $name), $arguments);
  38.     }
  39. }
Advertisement
Add Comment
Please, Sign In to add comment