AbstractRequest   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 92
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 0
dl 0
loc 92
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A getURI() 0 8 3
B setParameters() 0 24 6
1
<?php
2
/**
3
 * This file is part of :project_name
4
 *
5
 * PHP version 7
6
 *
7
 * @category  PHP
8
 * @package   WilliamEspindola\AbstractHTTPClient
9
 * @author    William Espindola <[email protected]>
10
 * @copyright Free
11
 * @license   MIT
12
 * @link      https://github.com/williamespindola/abstract-http-client
13
 */
14
declare(strict_types=1);
15
16
namespace WilliamEspindola\AbstractHTTPClient;
17
18
use Exception;
19
use InvalidArgumentException;
20
use WilliamEspindola\AbstractHTTPClient\HTTPClient;
21
22
/**
23
 * Abstracts request resources
24
 *
25
 * @category  PHP
26
 * @package   WilliamEspindola\AbstractHTTPClient
27
 * @author    William Espindola <[email protected]>
28
 * @copyright Free
29
 * @license   MIT
30
 * @version   Release: 1.0.0
31
 * @link      https://github.com/williamespindola/abstract-http-client
32
 */
33
abstract class AbstractRequest
34
{
35
    /**
36
     * @var array
37
     */
38
    protected $config;
39
40
    /**
41
     * @var Client $client Cliente implementation
42
     */
43
    protected $client;
44
45
    /**
46
     * @var string $baseUrl Base API URL
47
     */
48
    protected $baseUrl;
49
50
    /**
51
     * @var string $uri String that identifies resource
52
     */
53
    private $uri;
54
55
    /**
56
     * Initializes new AbstractNuxeoIntegration
57
     *
58
     * @param HTTPClient $client HTTPClient implementation
59
     * @param string     $baseUrl    Base API URL
60
     */
61
    public function __construct(HTTPClient $client, string $baseUrl)
62
    {
63
        $this->baseUrl = $baseUrl;
64
65
        $this->client = $client;
0 ignored issues
show
Documentation Bug introduced by
It seems like $client of type object<WilliamEspindola\...tHTTPClient\HTTPClient> is incompatible with the declared type object<WilliamEspindola\...tractHTTPClient\Client> of property $client.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
66
    }
67
68
    /**
69
     * Get URI concatenating base url api with resource end point
70
     *
71
     * @return string URI end point
72
     *
73
     * @throws Exception If AbstractRequest::baseUrl or AbstractRequest::uri was empty
74
     */
75
    protected function getURI(): string
76
    {
77
        if (empty($this->baseUrl) || empty($this->uri)) {
78
            throw new Exception('Url or end point can not be null');
79
        }
80
81
        return $this->baseUrl . $this->uri;
82
    }
83
84
    /**
85
     * Set url parameters
86
     *
87
     * @param array $parameters array with parameters that must be setted
88
     *
89
     * <code>
90
     *  $this->setParameters([
91
     *      ':paramkey' => 'my param value'
92
     *  ]);
93
     * </code>
94
     *
95
     * @throws InvalidArgumentException If parameters param was empty
96
     * @throws Exception                If parametes not match with request endPoint
97
     *
98
     * @return void
99
     */
100
    protected function setParameters(array $parameters)
101
    {
102
        if (empty($parameters)) {
103
            throw new InvalidArgumentException('Parameters can not be null');
104
        }
105
106
        $this->uri = null;
107
108
        foreach ($parameters as $key => $param) {
109
            if (preg_match("/{$key}/i", $this->endPoint)) {
110
                if (is_null($this->uri)) {
111
                    $this->uri = $this->endPoint;
0 ignored issues
show
Bug introduced by
The property endPoint does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
112
                }
113
114
                $this->uri = str_replace($key, $param, $this->uri);
115
            }
116
        }
117
118
        if (empty($this->uri)) {
119
            throw new Exception(
120
                'Parameters definition not matches with request end point'
121
            );
122
        }
123
    }
124
}
125