Issues (51)

src/Service/RequestService/Request.php (2 issues)

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
     */
40 26
    public function __construct(PostRequest $postRequest, GetRequest $getRequest)
41
    {
42 26
        $this->postRequest = $postRequest;
43 26
        $this->getRequest = $getRequest;
44 26
    }
45
46
    /**
47
     * @param RequestInterface $request
48
     * @param RequestOptions $requestOptions
49
     * @param Header $header
50
     *
51
     * @return ResponseInterface
52
     */
53 5
    public function execute(RequestInterface $request, RequestOptions $requestOptions, Header $header)
54
    {
55 5
        $method = $request->getVisibility() == VisibilityEnum::VISIBILITY_PRIVATE ? RequestMethodEnum::REQUEST_METHOD_POST : RequestMethodEnum::REQUEST_METHOD_GET;
56
57 5
        if ($method === RequestMethodEnum::REQUEST_METHOD_POST) {
58 2
            $response = $this->postRequest->execute($request, $requestOptions, $header);
59 2
        } else {
60 3
            $response = $this->getRequest->execute($request, $requestOptions);
61
        }
62
63 5
        $responseClass = $request->getResponseClassName();
64 5
        $result = json_decode($response->getBody(), true);
0 ignored issues
show
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 5
        $this->handleError($result);
66 4
        $responseObject = new $responseClass();
67 4
        if (isset($result["result"])) {
68 4
            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 2
                    $responseObject->$methodName($val);
74 2
                }
75
            }
76 4
        }
77
78 4
        return $responseObject;
79
    }
80
81
    /**
82
     * @param array $result
83
     * @throws ApiException
84
     */
85 5
    private function handleError($result)
86
    {
87 5
        if (isset($result["error"]) && !empty($result["error"])) {
88 1
            throw new ApiException(implode($result["error"], ","));
0 ignored issues
show
',' of type string is incompatible with the type array expected by parameter $pieces of implode(). ( Ignorable by Annotation )

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

88
            throw new ApiException(implode($result["error"], /** @scrutinizer ignore-type */ ","));
Loading history...
89
        }
90 4
    }
91
}
92