1<?php
2
3namespace Facebook\WebDriver\Support\Events;
4
5use Facebook\WebDriver\Exception\WebDriverException;
6use Facebook\WebDriver\WebDriverDispatcher;
7use Facebook\WebDriver\WebDriverNavigationInterface;
8
9class EventFiringWebDriverNavigation implements WebDriverNavigationInterface
10{
11    /**
12     * @var WebDriverNavigationInterface
13     */
14    protected $navigator;
15    /**
16     * @var WebDriverDispatcher
17     */
18    protected $dispatcher;
19
20    /**
21     * @param WebDriverNavigationInterface $navigator
22     * @param WebDriverDispatcher $dispatcher
23     */
24    public function __construct(WebDriverNavigationInterface $navigator, WebDriverDispatcher $dispatcher)
25    {
26        $this->navigator = $navigator;
27        $this->dispatcher = $dispatcher;
28    }
29
30    /**
31     * @return WebDriverDispatcher
32     */
33    public function getDispatcher()
34    {
35        return $this->dispatcher;
36    }
37
38    /**
39     * @return WebDriverNavigationInterface
40     */
41    public function getNavigator()
42    {
43        return $this->navigator;
44    }
45
46    public function back()
47    {
48        $this->dispatch(
49            'beforeNavigateBack',
50            $this->getDispatcher()->getDefaultDriver()
51        );
52
53        try {
54            $this->navigator->back();
55        } catch (WebDriverException $exception) {
56            $this->dispatchOnException($exception);
57        }
58        $this->dispatch(
59            'afterNavigateBack',
60            $this->getDispatcher()->getDefaultDriver()
61        );
62
63        return $this;
64    }
65
66    public function forward()
67    {
68        $this->dispatch(
69            'beforeNavigateForward',
70            $this->getDispatcher()->getDefaultDriver()
71        );
72
73        try {
74            $this->navigator->forward();
75        } catch (WebDriverException $exception) {
76            $this->dispatchOnException($exception);
77        }
78        $this->dispatch(
79            'afterNavigateForward',
80            $this->getDispatcher()->getDefaultDriver()
81        );
82
83        return $this;
84    }
85
86    public function refresh()
87    {
88        try {
89            $this->navigator->refresh();
90
91            return $this;
92        } catch (WebDriverException $exception) {
93            $this->dispatchOnException($exception);
94            throw $exception;
95        }
96    }
97
98    public function to($url)
99    {
100        $this->dispatch(
101            'beforeNavigateTo',
102            $url,
103            $this->getDispatcher()->getDefaultDriver()
104        );
105
106        try {
107            $this->navigator->to($url);
108        } catch (WebDriverException $exception) {
109            $this->dispatchOnException($exception);
110            throw $exception;
111        }
112
113        $this->dispatch(
114            'afterNavigateTo',
115            $url,
116            $this->getDispatcher()->getDefaultDriver()
117        );
118
119        return $this;
120    }
121
122    /**
123     * @param mixed $method
124     * @param mixed ...$arguments
125     */
126    protected function dispatch($method, ...$arguments)
127    {
128        if (!$this->dispatcher) {
129            return;
130        }
131
132        $this->dispatcher->dispatch($method, $arguments);
133    }
134
135    /**
136     * @param WebDriverException $exception
137     */
138    protected function dispatchOnException(WebDriverException $exception)
139    {
140        $this->dispatch('onException', $exception);
141    }
142}
143