1<?php
2
3namespace Facebook\WebDriver\Remote;
4
5use Facebook\WebDriver\Exception\WebDriverException;
6
7class CustomWebDriverCommand extends WebDriverCommand
8{
9    const METHOD_GET = 'GET';
10    const METHOD_POST = 'POST';
11
12    /** @var string */
13    private $customUrl;
14    /** @var string */
15    private $customMethod;
16
17    /**
18     * @param string $session_id
19     * @param string $url
20     * @param string $method
21     * @param array $parameters
22     */
23    public function __construct($session_id, $url, $method, array $parameters)
24    {
25        $this->setCustomRequestParameters($url, $method);
26
27        parent::__construct($session_id, DriverCommand::CUSTOM_COMMAND, $parameters);
28    }
29
30    /**
31     * @throws WebDriverException
32     * @return string
33     */
34    public function getCustomUrl()
35    {
36        if ($this->customUrl === null) {
37            throw new WebDriverException('URL of custom command is not set');
38        }
39
40        return $this->customUrl;
41    }
42
43    /**
44     * @throws WebDriverException
45     * @return string
46     */
47    public function getCustomMethod()
48    {
49        if ($this->customMethod === null) {
50            throw new WebDriverException('Method of custom command is not set');
51        }
52
53        return $this->customMethod;
54    }
55
56    /**
57     * @param string $custom_url
58     * @param string $custom_method
59     * @throws WebDriverException
60     */
61    protected function setCustomRequestParameters($custom_url, $custom_method)
62    {
63        $allowedMethods = [static::METHOD_GET, static::METHOD_POST];
64        if (!in_array($custom_method, $allowedMethods, true)) {
65            throw new WebDriverException(
66                sprintf(
67                    'Invalid custom method "%s", must be one of [%s]',
68                    $custom_method,
69                    implode(', ', $allowedMethods)
70                )
71            );
72        }
73        $this->customMethod = $custom_method;
74
75        if (mb_strpos($custom_url, '/') !== 0) {
76            throw new WebDriverException(
77                sprintf('URL of custom command has to start with / but is "%s"', $custom_url)
78            );
79        }
80        $this->customUrl = $custom_url;
81    }
82}
83