1<?php
2
3namespace Facebook\WebDriver;
4
5use Facebook\WebDriver\Support\Events\EventFiringWebDriver;
6
7class WebDriverDispatcher
8{
9    /**
10     * @var array
11     */
12    protected $listeners = [];
13    /**
14     * @var EventFiringWebDriver
15     */
16    protected $driver;
17
18    /**
19     * this is needed so that EventFiringWebElement can pass the driver to the
20     * exception handling
21     *
22     * @param EventFiringWebDriver $driver
23     * @return $this
24     */
25    public function setDefaultDriver(EventFiringWebDriver $driver)
26    {
27        $this->driver = $driver;
28
29        return $this;
30    }
31
32    /**
33     * @return null|EventFiringWebDriver
34     */
35    public function getDefaultDriver()
36    {
37        return $this->driver;
38    }
39
40    /**
41     * @param WebDriverEventListener $listener
42     * @return $this
43     */
44    public function register(WebDriverEventListener $listener)
45    {
46        $this->listeners[] = $listener;
47
48        return $this;
49    }
50
51    /**
52     * @param WebDriverEventListener $listener
53     * @return $this
54     */
55    public function unregister(WebDriverEventListener $listener)
56    {
57        $key = array_search($listener, $this->listeners, true);
58        if ($key !== false) {
59            unset($this->listeners[$key]);
60        }
61
62        return $this;
63    }
64
65    /**
66     * @param mixed $method
67     * @param mixed $arguments
68     * @return $this
69     */
70    public function dispatch($method, $arguments)
71    {
72        foreach ($this->listeners as $listener) {
73            call_user_func_array([$listener, $method], $arguments);
74        }
75
76        return $this;
77    }
78}
79