3ef14d0d7ca7a82bc6738c367442e6b674bd3100
[yaffs-website] / vendor / jcalderonzumba / gastonjs / src / Browser / BrowserBase.php
1 <?php
2
3 namespace Zumba\GastonJS\Browser;
4
5 use Zumba\GastonJS\Exception\BrowserError;
6 use Zumba\GastonJS\Exception\DeadClient;
7 use GuzzleHttp\Client;
8 use GuzzleHttp\Exception\ConnectException;
9 use GuzzleHttp\Exception\ServerException;
10
11 /**
12  * Class BrowserBase
13  * @package Zumba\GastonJS\Browser
14  */
15 class BrowserBase {
16   /** @var mixed */
17   protected $logger;
18   /** @var  bool */
19   protected $debug;
20   /** @var  string */
21   protected $phantomJSHost;
22   /** @var  Client */
23   protected $apiClient;
24
25   /**
26    *  Creates an http client to consume the phantomjs API
27    */
28   protected function createApiClient() {
29     // Provide a BC switch between guzzle 5 and guzzle 6.
30     if (class_exists('GuzzleHttp\Psr7\Response')) {
31       $this->apiClient = new Client(array("base_uri" => $this->getPhantomJSHost()));
32     }
33     else {
34       $this->apiClient = new Client(array("base_url" => $this->getPhantomJSHost()));
35     }
36   }
37
38   /**
39    * TODO: not sure how to do the normalizeKeys stuff fix when needed
40    * @param $keys
41    * @return mixed
42    */
43   protected function normalizeKeys($keys) {
44     return $keys;
45   }
46
47   /**
48    * @return Client
49    */
50   public function getApiClient() {
51     return $this->apiClient;
52   }
53
54   /**
55    * @return string
56    */
57   public function getPhantomJSHost() {
58     return $this->phantomJSHost;
59   }
60
61   /**
62    * @return mixed
63    */
64   public function getLogger() {
65     return $this->logger;
66   }
67
68   /**
69    * Restarts the browser
70    */
71   public function restart() {
72     //TODO: Do we really need to do this?, we are just a client
73   }
74
75   /**
76    * Sends a command to the browser
77    * @throws BrowserError
78    * @throws \Exception
79    * @return mixed
80    */
81   public function command() {
82     try {
83       $args = func_get_args();
84       $commandName = $args[0];
85       array_shift($args);
86       $messageToSend = json_encode(array('name' => $commandName, 'args' => $args));
87       /** @var $commandResponse \GuzzleHttp\Psr7\Response|\GuzzleHttp\Message\Response */
88       $commandResponse = $this->getApiClient()->post("/api", array("body" => $messageToSend));
89       $jsonResponse = json_decode($commandResponse->getBody(), TRUE);
90     } catch (ServerException $e) {
91       $jsonResponse = json_decode($e->getResponse()->getBody()->getContents(), true);
92     } catch (ConnectException $e) {
93       throw new DeadClient($e->getMessage(), $e->getCode(), $e);
94     } catch (\Exception $e) {
95       throw $e;
96     }
97
98     if (isset($jsonResponse['error'])) {
99       throw $this->getErrorClass($jsonResponse);
100     }
101
102     return $jsonResponse['response'];
103   }
104
105   /**
106    * @param $error
107    * @return BrowserError
108    */
109   protected function getErrorClass($error) {
110     $errorClassMap = array(
111       'Poltergeist.JavascriptError'   => "Zumba\\GastonJS\\Exception\\JavascriptError",
112       'Poltergeist.FrameNotFound'     => "Zumba\\GastonJS\\Exception\\FrameNotFound",
113       'Poltergeist.InvalidSelector'   => "Zumba\\GastonJS\\Exception\\InvalidSelector",
114       'Poltergeist.StatusFailError'   => "Zumba\\GastonJS\\Exception\\StatusFailError",
115       'Poltergeist.NoSuchWindowError' => "Zumba\\GastonJS\\Exception\\NoSuchWindowError",
116       'Poltergeist.ObsoleteNode'      => "Zumba\\GastonJS\\Exception\\ObsoleteNode"
117     );
118     if (isset($error['error']['name']) && isset($errorClassMap[$error["error"]["name"]])) {
119       return new $errorClassMap[$error["error"]["name"]]($error);
120     }
121
122     return new BrowserError($error);
123   }
124 }