AbstractRequest::getStatusMessage()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

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