Completed
Push — feature/configuration ( 48ff2d...765854 )
by Stéphane
53:12 queued 25:50
created

Client   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 11

Test Coverage

Coverage 93.55%

Importance

Changes 10
Bugs 1 Features 1
Metric Value
wmc 9
c 10
b 1
f 1
lcom 1
cbo 11
dl 0
loc 89
ccs 29
cts 31
cp 0.9355
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 2
A setConfiguration() 0 5 1
A createRequest() 0 13 3
A send() 0 21 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\Events\DispatcherAwareTrait;
15
use Bee4\Transport\Events\ErrorEvent;
16
use Bee4\Transport\Events\MessageEvent;
17
use Bee4\Transport\Exception\CurlException;
18
use Bee4\Transport\Message\Request\AbstractRequest;
19
use Bee4\Transport\Message\Request\RequestFactory;
20
use Bee4\Transport\Message\ResponseFactory;
21
use Bee4\Transport\Handle\HandleFactory;
22
use Bee4\Transport\Exception\Exception;
23
use Bee4\Transport\Exception\InvalidArgumentException;
24
use Bee4\Transport\Configuration\Configuration;
25
26
/**
27
 * Transport client, generate a request and return the linked response
28
 * @package Bee4\Transport
29
 */
30
class Client implements ClientInterface
31
{
32
    use DispatcherAwareTrait;
33
34
    /**
35
     * Base URL for calls
36
     * @var Url
37
     */
38
    protected $baseUrl;
39
40
    /**
41
     * The factory to build request messages
42
     * @var RequestFactory
43
     */
44
    protected $requestFactory;
45
46
    /**
47
     * HTTP Client which use cURL extension
48
     * @param string $baseUrl Base URL of the web service
49
     */
50 14
    public function __construct($baseUrl = '', Configuration $config = null)
51
    {
52 14
        if ($baseUrl != '') {
53 13
            $this->baseUrl = new Url($baseUrl);
54 13
        }
55
56 14
        $this->requestFactory = new RequestFactory($config);
57 14
    }
58
59
    /**
60
     * Set configuration in the Request factory
61
     * @param Configuration $config
62
     * @return Client
63
     */
64 2
    public function setConfiguration(Configuration $config)
65
    {
66
        $this->requestFactory->setConfiguration($config);
0 ignored issues
show
Bug introduced by
The method setConfiguration() does not seem to exist on object<Bee4\Transport\Me...Request\RequestFactory>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
67
        return $this;
68 2
    }
69
70
    /**
71
     * Create the request object
72
     * @param string $method
73
     * @param string $url
74
     * @param array $headers
75
     * @return AbstractRequest
76
     */
77 13
    public function createRequest($method, $url = '', array $headers = [])
78
    {
79 13
        if (!is_string($url)) {
80 1
            throw new InvalidArgumentException('Url parameter must be a valid string!!');
81
        }
82
83 12
        $url = new Url((isset($this->baseUrl)?$this->baseUrl->toString():'').$url);
84
85 11
        $request = $this->requestFactory->build($method, $url, $headers);
86 10
        $request->setClient($this);
87
88 10
        return $request;
89
    }
90
91
    /**
92
     * Send the request
93
     * @param AbstractRequest $request The request to be send
94
     * @return Message\Response
95
     * @throws Exception
96
     */
97 11
    public function send(AbstractRequest $request)
98
    {
99 11
        $handle = HandleFactory::build($request);
100 11
        $this->dispatch(MessageEvent::REQUEST, new MessageEvent($request));
101
102
        try {
103 10
            $result = $handle->execute();
104 10
        } catch (\Exception $error) {
105 1
            if ($error instanceof CurlException) {
106 1
                $response = ResponseFactory::build('', $handle->infos(), $request);
107 1
                $error->setResponse($response);
108 1
            }
109 1
            $this->dispatch(ErrorEvent::ERROR, new ErrorEvent($error));
110 1
            throw $error;
111
        }
112
113 9
        $response = ResponseFactory::build($result, $handle->infos(), $request);
114 9
        $this->dispatch(MessageEvent::RESPONSE, new MessageEvent($response));
115
116 9
        return $response;
117
    }
118
}
119