Completed
Push — master ( 1212b3...010c30 )
by Stéphane
05:43
created

AbstractRequest::getUrl()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
/**
3
 * This file is part of the bee4/transport package.
4
 * For the full copyright and license information, please view the LICENSE
5
 * file that was distributed with this source code.
6
 *
7
 * @copyright Bee4 2015
8
 * @author  Stephane HULARD <[email protected]>
9
 * @package Bee4\Transport\Message\Request
10
 */
11
12
namespace Bee4\Transport\Message\Request;
13
14
use Bee4\Transport\Message\AbstractMessage;
15
use Bee4\Transport\Client;
16
use Bee4\Transport\Url;
17
use Bee4\Transport\Exception\RuntimeException;
18
19
/**
20
 * HTTP Request object
21
 * @package Bee4\Transport\Message\Request
22
 */
23
abstract class AbstractRequest extends AbstractMessage
24
{
25
    //Compatible SCHEME
26
    const HTTP = '/^http/';
27
    const FTP = '/^ftp/';
28
    const SSH = '/sftp|scp/';
29
30
    /**
31
     * Request UserAgent
32
     * Allow to identify the request initiator
33
     * @var string
34
     */
35
    protected $ua = "Bee4/Transport";
36
37
    /**
38
     * Current client instance
39
     * @var Client
40
     */
41
    protected $client;
42
43
    /**
44
     * specific cURL options for the current request
45
     * @var array
46
     */
47
    protected $options = [];
48
49
    /**
50
     * @var Url
51
     */
52
    protected $url;
53
54
    /**
55
     * Construct a new request object
56
     * @param Url $url
57
     * @param array $headers
58
     */
59 16
    public function __construct(Url $url, array $headers = [])
60
    {
61 16
        parent::__construct();
62 16
        $this->url = $url;
63 16
        $this->options = [];
64 16
        $this->addHeaders($headers);
65 16
    }
66
67
    /**
68
     * Set the linked client
69
     * @param Client $client
70
     * @return AbstractRequest
71
     */
72 11
    public function setClient(Client $client)
73
    {
74 11
        $this->client = $client;
75 11
        return $this;
76
    }
77
78
    /**
79
     * URL accessor
80
     * @return Url
81
     */
82 12
    public function getUrl()
83
    {
84 12
        return $this->url;
85
    }
86
87
    /**
88
     * cURL option collection accessor
89
     * @return array
90
     */
91 12
    public function getOptions()
92
    {
93 12
        return $this->options;
94 1
    }
95
96
    /**
97
     * Add specifically curl option list to current request
98
     * @param array $options
99
     * @return AbstractRequest
100
     */
101 1
    public function addOptions(array $options)
102
    {
103 1
        foreach ($options as $name => $value) {
104 1
            $this->addOption($name, $value);
105 1
        }
106
107 1
        return $this;
108 1
    }
109
110
    /**
111
     * Add an option for current request
112
     * @param int $name
113
     * @param mixed $value
114
     * @return AbstractRequest
115
     */
116 12
    public function addOption($name, $value)
117
    {
118 12
        if ($name === CURLOPT_USERAGENT) {
119 9
            $this->ua = $value;
120 9
        }
121
122 12
        $this->options[$name] = $value;
123
124 12
        return $this;
125
    }
126
127
    /**
128
     * Check if an option exists
129
     * @param mixed $name
130
     * @return boolean
131
     */
132 11
    public function hasOption($name)
133
    {
134 11
        return isset($this->options[$name]);
135
    }
136
137
    /**
138
     * Prepare the request execution by adding specific cURL parameters
139
     */
140
    abstract protected function prepare();
141
142
    /**
143
     * Send method.
144
     * To send a request, a client must be linked
145
     * @return \Bee4\Transport\Message\Response
146
     * @throws RuntimeException
147
     */
148 12
    public function send()
149
    {
150 12
        if (!$this->client) {
151 1
            throw new RuntimeException('A client must be set on the request');
152
        }
153
154 11
        if (!$this->hasOption(CURLOPT_URL)) {
155 11
            $this->addOption(CURLOPT_URL, $this->getUrl()->toString());
156 11
        }
157 11
        $this->prepare();
158
159 11
        return $this->client->send($this);
160
    }
161
162
    /**
163
     * Retrieve the status message for the current request based on STATUS_XXX constants
164
     * @param string $status
165
     * @return string
166
     */
167
    public function getStatusMessage($status)
168
    {
169
        $name = get_called_class().'::STATUS_'.$status;
170
        return defined($name)?constant($name):'';
171
    }
172
173
    /**
174
     * Get the client UA for all requests
175
     * @return string
176
     */
177 9
    public function getUserAgent()
178
    {
179 9
        return $this->ua;
180
    }
181
182
    /**
183
     * Set the client UA for current request
184
     * @param string $ua
185
     * @return AbstractRequest
186
     */
187 1
    public function setUserAgent($ua)
188
    {
189 1
        $this->addOption(CURLOPT_USERAGENT, $ua);
190
191 1
        return $this;
192
    }
193
}
194