Completed
Push — master ( ccaea6...c0c6b3 )
by
unknown
02:43
created

IzbergClient::setNameSpace()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace judicaelpaquet\IzbergBundle\Service;
4
5
use Guzzle\Http\Client;
6
use Guzzle\Http\Exception\BadResponseException;
7
8
/**
9
 * Class IzbergClient
10
 * @package judicaelpaquet\IzbergBundle\Service
11
 */
12
abstract class IzbergClient
13
{
14
    /**
15
     * @var IzbergConnector
16
     */
17
    protected $izbergConnector;
18
    /**
19
     * @var Client
20
     */
21
    protected $httpClient;
22
    /**
23
     * @var string
24
     */
25
    protected static $baseUri = 'https://api.sandbox.iceberg.technology';
26
    /**
27
     * @var string
28
     */
29
    protected $nameSpace;
30
31
    /**
32
     * @param IzbergConnector $izbergConnector
33
     * @param string $nameSpace
0 ignored issues
show
Bug introduced by
There is no parameter named $nameSpace. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
34
     */
35
    public function setIzbergConnector(IzbergConnector $izbergConnector)
36
    {
37
        $this->izbergConnector = $izbergConnector;
38
    }
39
40
    /**
41
     * @param Client $httpClient
42
     */
43
    public function setHttpClient(Client $httpClient)
44
    {
45
        $this->httpClient = $httpClient;
46
        $this->httpClient->setBaseUrl(self::$baseUri);
47
    }
48
49
    /**
50
     * @param string $namespace
0 ignored issues
show
Documentation introduced by
There is no parameter named $namespace. Did you maybe mean $nameSpace?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.

Consider the following example. The parameter $ireland is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $ireland
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was changed, but the annotation was not.

Loading history...
51
     */
52
    public function setNameSpace(string $nameSpace)
53
    {
54
       $this->nameSpace = $nameSpace;
55
    }
56
57
    /**
58
     * @return array
59
     */
60
    public function generateHeader()
61
    {
62
        $namespace = '';
63
        if ($this->izbergConnector->getAuthentication()['username'] == 'Anonymous') {
64
            $namespace = $this->nameSpace;
65
            $namespace = ':'.$namespace;
66
        }
67
        return [
68
            'Content-Type' => 'application/json',
69
            'Authorization' => vsprintf("Bearer %s%s:%s", [$this->izbergConnector->getAuthentication()['username'], $namespace, $this->izbergConnector->getAuthentication()['api_key']]),
70
        ];
71
    }
72
73
    /**
74
     * @param string $url
75
     * @return array
76
     */
77
    protected function getObjects(string $url)
78
    {
79
        $request = $this->httpClient->get($url);
80
        $request->setHeaders($this->generateHeader());
81
        return $request->send()->json()['objects'];
82
    }
83
84
    /**
85
     * @param string $url
86
     * @param int $id
87
     * @return string
88
     */
89
    protected function getObject(string $url, int $id)
90
    {
91
        $request = $this->httpClient->get($url.'/'.$id);
92
        $request->setHeaders($this->generateHeader());
93
94
        try {
95
            return $request->send()->json();
96
        } catch(BadResponseException $e) {
97
            echo $e->getMessage();
98
            echo 'There is no object with this id:'.$id;
99
        }
100
    }
101
102
    /**
103
     * @param string $url
104
     * @return string
105
     */
106
    protected function getObjectSchema(string $url)
107
    {
108
        $request = $this->httpClient->get($url.'/schema');
109
        $request->setHeaders($this->generateHeader());
110
        return $request->send()->json();
111
    }
112
113
    /**
114
     * @param $functionName
115
     * @param $params
116
     * @return array|string
117
     */
118
    public function __call($functionName, $params)
119
    {
120
        $className = (new \ReflectionClass($this))->getShortName();
121
        $apiNameCalled = str_replace('Izberg', '', $className);
122
123
        switch($functionName) {
124
            case 'get'.$apiNameCalled.'s':
125
                return $this->getObjects(static::$url);
126
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
127
            case 'get'.$apiNameCalled:
128
                return $this->getObject(static::$url, $params[0]);
129
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
130
            case 'get'.$apiNameCalled.'Schema':
131
                return $this->getObjectSchema(static::$url);
132
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
133
        }
134
    }
135
}
136