Completed
Push — master ( 092247...6dd5e4 )
by Stéphane
06:20 queued 03:28
created

AbstractRequest::hasOption()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
179
    }
180
181
    /**
182
     * Set the client UA for current request
183
     * @param string $ua
184
     * @return AbstractRequest
185
     */
186 1
    public function setUserAgent($ua)
187
    {
188 1
        $this->configuration->user_agent = $ua;
0 ignored issues
show
Documentation introduced by
The property user_agent does not exist on object<Bee4\Transport\Co...guration\Configuration>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
189 1
        $this->addHeader('User-Agent', $ua);
190
191 1
        return $this;
192
    }
193
}
194