MagicHandler   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 84.62%

Importance

Changes 3
Bugs 1 Features 1
Metric Value
wmc 7
c 3
b 1
f 1
lcom 1
cbo 1
dl 0
loc 54
ccs 11
cts 13
cp 0.8462
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 2
A send() 0 4 1
A createRequest() 0 4 1
A __call() 0 12 3
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
10
 */
11
12
namespace Bee4\Transport;
13
14
use Bee4\Transport\Message\Request\AbstractRequest;
15
16
/**
17
 * Add some magic method handling to the default client
18
 * @package Bee4\Transport
19
 *
20
 * @method AbstractRequest get(string $url = "", array $headers = [])
21
 * @method AbstractRequest post(string $url = "", array $headers = [])
22
 * @method AbstractRequest head(string $url = "", array $headers = [])
23
 * @method AbstractRequest delete(string $url = "", array $headers = [])
24
 * @method AbstractRequest put(string $url = "", array $headers = [])
25
 */
26
class MagicHandler implements ClientInterface
27
{
28
    protected $client;
29
30
    /**
31
     * @param Client $c
32
     */
33 13
    public function __construct(Client $c = null)
34
    {
35 13
        $this->client = is_null($c)?new Client():$c;
36 13
    }
37
38
    /**
39
     * Send decoration, rely to given client
40
     * @param  AbstractRequest $request
41
     * @return Message\Response
42
     */
43
    public function send(AbstractRequest $request)
44
    {
45
        return $this->client->send($request);
46
    }
47
48
    /**
49
     * Create decoration, rely to given client
50
     * @param string $method
51
     * @param string $url
52
     * @param array $headers
53
     * @return AbstractRequest
54
     */
55 9
    public function createRequest($method, $url = '', array $headers = [])
56
    {
57 9
        return $this->client->createRequest($method, $url, $headers);
58
    }
59
60
    /**
61
     * Magic method to implement dynamically all request types that are defined.
62
     * The request factory can't build a valid object, an exception is thrown
63
     * @param string $name The method name
64
     * @param array $arguments Argument collection to be used to build request
65
     * @return AbstractRequest
66
     */
67 9
    public function __call($name, array $arguments = [])
68
    {
69
        //Send all others method directly to the client
70 9
        if (method_exists($this->client, $name)) {
71 1
            return call_user_func_array([$this->client, $name], $arguments);
72
        }
73
74
        //Then handle magic HTTP name calls
75 9
        $arguments[0] = isset($arguments[0])?$arguments[0]:'';
76 9
        array_unshift($arguments, $name);
77 9
        return call_user_func_array([$this, 'createRequest'], $arguments);
78
    }
79
}
80