Completed
Push — master ( cbe1fc...39f0d7 )
by Anton
02:31
created

WithCustomUrl   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
lcom 1
cbo 3
dl 0
loc 51
rs 10
c 1
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 15 4
A send() 0 14 3
1
<?php
2
3
namespace Covery\Client\Transport;
4
5
use Covery\Client\TransportInterface;
6
use GuzzleHttp\Psr7\Uri;
7
use Psr\Http\Message\RequestInterface;
8
9
/**
10
 * Class WithCustomUrl
11
 *
12
 * Special transport implementation, that allows replacement
13
 * of Covery URL
14
 *
15
 * @package Covery\Client\Transport
16
 */
17
class WithCustomUrl implements TransportInterface
18
{
19
    /**
20
     * @var string
21
     */
22
    private $url;
23
    /**
24
     * @var TransportInterface
25
     */
26
    private $transport;
27
28
    /**
29
     * WithCustomUrl constructor.
30
     *
31
     * @param string $url
32
     * @param TransportInterface $transport
33
     */
34
    public function __construct($url, TransportInterface $transport)
35
    {
36
        if (!is_string($url)) {
37
            throw new \InvalidArgumentException('URL must be string');
38
        }
39
        if ($url !== null) {
40
            if (substr($url, -1) != '/') {
41
                $url .= '/';
42
            }
43
44
            $this->url = $url;
45
        }
46
        $this->url = $url;
47
        $this->transport = $transport;
48
    }
49
50
    /**
51
     * @inheritDoc
52
     */
53
    public function send(RequestInterface $request)
54
    {
55
        $requestUrl = strval($request->getUri());
56
        if ($this->url !== null
57
            && substr($requestUrl, 0, strlen(TransportInterface::DEFAULT_URL)) === TransportInterface::DEFAULT_URL
58
        ) {
59
            // Replacing
60
            $request = $request->withUri(
61
                new Uri(str_replace(TransportInterface::DEFAULT_URL, $this->url, $requestUrl))
62
            );
63
        }
64
65
        return $this->transport->send($request);
66
    }
67
}
68