Completed
Push — master ( 15e9d9...1109c2 )
by Anton
03:03
created

WithCustomHost   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 52
Duplicated Lines 9.62 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 3
dl 5
loc 52
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 5 16 4
A send() 0 8 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Covery\Client\Transport;
4
5
use Covery\Client\TransportInterface;
6
use Psr\Http\Message\RequestInterface;
7
8
/**
9
 * Class WithCustomHost
10
 *
11
 * Special transport implementation, that allows replacement
12
 * of Covery hostname
13
 *
14
 * @package Covery\Client\Transport
15
 */
16
class WithCustomHost implements TransportInterface
17
{
18
    /**
19
     * @var string
20
     */
21
    private $host;
22
    /**
23
     * @var string
24
     */
25
    private $scheme;
26
27
    /**
28
     * @var TransportInterface
29
     */
30
    private $transport;
31
32
    /**
33
     * WithCustomUrl constructor.
34
     *
35
     * @param TransportInterface $transport
36
     * @param string $host
37
     * @param string $scheme
38
     */
39
    public function __construct(TransportInterface $transport, $host, $scheme = 'https')
40
    {
41 View Code Duplication
        if (!is_string($host)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
42
            throw new \InvalidArgumentException('Host must be string');
43
        } elseif (empty($host)) {
44
            throw new \InvalidArgumentException('Host must be not empty');
45
        }
46
47
        if (!is_string($scheme)) {
48
            throw new \InvalidArgumentException('Scheme must be string');
49
        }
50
51
        $this->transport = $transport;
52
        $this->host = $host;
53
        $this->scheme = $scheme;
54
    }
55
56
    /**
57
     * @inheritDoc
58
     */
59
    public function send(RequestInterface $request)
60
    {
61
        $request = $request->withUri(
62
            $request->getUri()->withHost($this->host)->withScheme($this->scheme)
63
        );
64
65
        return $this->transport->send($request);
66
    }
67
}
68