Completed
Push — master ( 3ad417...37b5d9 )
by Fabian
02:10
created

Request   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Test Coverage

Coverage 85.19%

Importance

Changes 0
Metric Value
dl 0
loc 66
ccs 23
cts 27
cp 0.8519
rs 10
c 0
b 0
f 0
wmc 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B execute() 0 26 6
A handleError() 0 4 3
1
<?php
2
/**
3
 * @author  Fabian Hanisch
4
 * @since   14.07.2017 23:08
5
 * @version 1.0
6
 */
7
8
namespace HanischIt\KrakenApi\Service\RequestService;
9
10
use HanischIt\KrakenApi\Enum\RequestMethodEnum;
11
use HanischIt\KrakenApi\Enum\VisibilityEnum;
12
use HanischIt\KrakenApi\Exception\ApiException;
13
use HanischIt\KrakenApi\Model\AbstractRequest;
14
use HanischIt\KrakenApi\Model\Header;
15
use HanischIt\KrakenApi\Model\RequestInterface;
0 ignored issues
show
Bug introduced by
The type HanischIt\KrakenApi\Model\RequestInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
16
use HanischIt\KrakenApi\Model\RequestOptions;
17
use HanischIt\KrakenApi\Model\Response;
18
19
/**
20
 * Class AbstractRequest
21
 *
22
 * @package HanischIt\KrakenApi\Service\RequestService
23
 */
24
class Request implements RequestServiceInterface
25
{
26
    /**
27
     * @var PostRequest
28
     */
29
    private $postRequest;
30
    /**
31
     * @var GetRequest
32
     */
33
    private $getRequest;
34
35
    /**
36
     * AbstractRequest constructor.
37
     *
38
     * @param PostRequest $postRequest
39
     * @param GetRequest $getRequest
40
     */
41 19
    public function __construct(PostRequest $postRequest, GetRequest $getRequest)
42
    {
43 19
        $this->postRequest = $postRequest;
44 19
        $this->getRequest = $getRequest;
45 19
    }
46
47
    /**
48
     * @param AbstractRequest $request
49
     * @param RequestOptions $requestOptions
50
     * @param Header $header
51
     *
52
     * @return Response
53
     */
54 5
    public function execute(AbstractRequest $request, RequestOptions $requestOptions, Header $header)
55
    {
56 5
        $method = $request->getVisibility() == VisibilityEnum::VISIBILITY_PRIVATE ? RequestMethodEnum::REQUEST_METHOD_POST : RequestMethodEnum::REQUEST_METHOD_GET;
57
58 5
        if ($method === RequestMethodEnum::REQUEST_METHOD_POST) {
59 2
            $response = $this->postRequest->execute($request, $requestOptions, $header);
60 2
        } else {
61 3
            $response = $this->getRequest->execute($request, $requestOptions);
62
        }
63
64 5
        $responseClass = $request->getResponseClassName();
65 5
        $result = json_decode($response->getBody(), true);
0 ignored issues
show
Bug introduced by
The method getBody() does not exist on HanischIt\KrakenApi\Model\ResponseInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

65
        $result = json_decode($response->/** @scrutinizer ignore-call */ getBody(), true);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
66 5
        $this->handleError($result);
67 4
        $responseObject = new $responseClass();
68 4
        if (isset($result["result"])) {
69 4
            if (method_exists($responseObject, "manualMapping")) {
70 4
                $responseObject->manualMapping($result["result"]);
71 4
            } else {
72
                foreach ($result["result"] as $key => $val) {
73
                    $methodName = "set" . ucfirst($key);
74
                    $responseObject->$methodName($val);
75
                }
76
            }
77 4
        }
78
79 4
        return $responseObject;
80
    }
81
82
    /**
83
     * @param array $result
84
     * @throws ApiException
85
     */
86 5
    private function handleError($result)
87
    {
88 5
        if (isset($result["error"]) && !empty($result["error"])) {
89 1
            throw new ApiException($result["error"]);
90
        }
91 4
    }
92
}
93