Test Failed
Push — master ( e2e181...baee16 )
by Fabian
02:01
created

Request::handleError()   A

Complexity

Conditions 3
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 2
nc 1
nop 1
dl 0
loc 4
ccs 0
cts 0
cp 0
crap 12
rs 10
c 0
b 0
f 0
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\Header;
14
use HanischIt\KrakenApi\Model\RequestInterface;
15
use HanischIt\KrakenApi\Model\RequestOptions;
16
use HanischIt\KrakenApi\Model\ResponseInterface;
17
18
/**
19
 * Class Request
20
 *
21
 * @package HanischIt\KrakenApi\Service\RequestService
22
 */
23
class Request implements RequestServiceInterface
24
{
25
    /**
26
     * @var PostRequest
27
     */
28
    private $postRequest;
29
    /**
30
     * @var GetRequest
31
     */
32
    private $getRequest;
33
34
    /**
35
     * Request constructor.
36
     *
37
     * @param PostRequest $postRequest
38
     * @param GetRequest $getRequest
39 15
     */
40
    public function __construct(PostRequest $postRequest, GetRequest $getRequest)
41 15
    {
42 15
        $this->postRequest = $postRequest;
43 15
        $this->getRequest = $getRequest;
44
    }
45
46
    /**
47
     * @param RequestInterface $request
48
     * @param RequestOptions $requestOptions
49
     * @param Header $header
50
     *
51
     * @return ResponseInterface
52 4
     */
53
    public function execute(RequestInterface $request, RequestOptions $requestOptions, Header $header)
54 4
    {
55
        $method = $request->getVisibility() == VisibilityEnum::VISIBILITY_PRIVATE ? RequestMethodEnum::REQUEST_METHOD_POST : RequestMethodEnum::REQUEST_METHOD_GET;
56 4
57 2
        if ($method === RequestMethodEnum::REQUEST_METHOD_POST) {
58 2
            $response = $this->postRequest->execute($request, $requestOptions, $header);
59 2
        } else {
60
            $response = $this->getRequest->execute($request, $requestOptions);
61
        }
62 4
63 4
        $responseClass = $request->getResponseClassName();
64 4
        $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

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