Executor   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 9
Bugs 5 Features 0
Metric Value
wmc 4
c 9
b 5
f 0
lcom 1
cbo 5
dl 0
loc 61
ccs 21
cts 21
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A run() 0 12 1
A makeBody() 0 15 2
1
<?php
2
3
namespace Raidros\Storer;
4
5
use GuzzleHttp\Client;
6
7
class Executor
8
{
9
    protected $api;
10
    protected $endpoint;
11
12
    /**
13
     * Setup a api endpoint execution.
14
     *
15
     * @param Raidros\Storer\Api $api
16
     */
17 15
    public function __construct(Api $api)
18
    {
19 15
        $this->api = $api;
20 15
    }
21
22
    /**
23
     * call a api endpoint.
24
     *
25
     * @param string $pointName
26
     * @param array  $data
27
     * @param array  $headers
28
     *
29
     * @return Raidros\Storer\Response
30
     */
31 15
    public function run($pointName, array $data = [], array $headers = [])
32
    {
33 15
        $this->endpoint = $this->api->getEndpoints()->findOrFail($pointName);
34
35 15
        $client = new Client($this->api->getHandler());
36 15
        $method = $this->endpoint->getMethod();
37 15
        $uri = $this->endpoint->getUri($data);
38 15
        $body = $this->makeBody($headers, $data);
39 15
        $response = $client->request($method, $uri, $body);
40
41 15
        return new Response($response, $this->endpoint->getTransformer());
0 ignored issues
show
Compatibility introduced by
$response of type object<Psr\Http\Message\ResponseInterface> is not a sub-type of object<GuzzleHttp\Psr7\Response>. It seems like you assume a concrete implementation of the interface Psr\Http\Message\ResponseInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
42
    }
43
44
    /**
45
     * make the request body.
46
     *
47
     * @param array $pointHeaders
48
     * @param array $data
49
     *
50
     * @return array
51
     */
52 15
    protected function makeBody($pointHeaders, $data)
53
    {
54 15
        unset($data['uriData']);
55 15
        $apiHeaders = array_map(function ($value) {
56 12
            return $value->get();
57 15
        }, $this->api->getHeaders()->get());
58
59 15
        $dataChain = $this->endpoint->getMethod() == 'GET' ? 'query' : 'json';
60 15
        $body = array_merge($apiHeaders, $pointHeaders);
61 15
        $body[$dataChain] = $data;
62
63 15
        $request = new Request($body, $this->endpoint->getTransformer('request'), $dataChain);
0 ignored issues
show
Unused Code introduced by
The call to Request::__construct() has too many arguments starting with $dataChain.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
64
65 15
        return $request->getBody($dataChain);
66
    }
67
}
68