1 <?php
 2 /**
 3  * Part of the Joomla Framework Event Package
 4  *
 5  * @copyright  Copyright (C) 2005 - 2016 Open Source Matters, Inc. All rights reserved.
 6  * @license    GNU General Public License version 2 or later; see LICENSE
 7  */
 8 
 9 namespace Joomla\Event;
10 
11 /**
12  * Defines the trait for a Dispatcher Aware Class.
13  *
14  * @since  1.2.0
15  */
16 trait DispatcherAwareTrait
17 {
18     /**
19      * Event Dispatcher
20      *
21      * @var    DispatcherInterface
22      * @since  1.2.0
23      */
24     private $dispatcher;
25 
26     /**
27      * Get the event dispatcher.
28      *
29      * @return  DispatcherInterface
30      *
31      * @since   1.2.0
32      * @throws  \UnexpectedValueException May be thrown if the dispatcher has not been set.
33      */
34     public function getDispatcher()
35     {
36         if ($this->dispatcher)
37         {
38             return $this->dispatcher;
39         }
40 
41         throw new \UnexpectedValueException('Dispatcher not set in ' . __CLASS__);
42     }
43 
44     /**
45      * Set the dispatcher to use.
46      *
47      * @param   DispatcherInterface  $dispatcher  The dispatcher to use.
48      *
49      * @return  $this
50      *
51      * @since   1.2.0
52      */
53     public function setDispatcher(DispatcherInterface $dispatcher)
54     {
55         $this->dispatcher = $dispatcher;
56 
57         return $this;
58     }
59 }
60