Passed
Branch master (34dd1b)
by Leo
01:41
created

Api::getApiResponse()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 0
cts 6
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 1
crap 2
1
<?php
2
3
namespace leocata\M1;
4
5
use leocata\M1\Abstracts\CallbackMethods;
6
use leocata\M1\Abstracts\Methods;
0 ignored issues
show
Bug introduced by
The type leocata\M1\Abstracts\Methods 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...
7
use leocata\M1\Abstracts\RequestMethods;
8
use leocata\M1\Exceptions\MethodNotFound;
9
use leocata\M1\Exceptions\MissingMandatoryField;
10
use leocata\M1\InternalFunctionality\DummyLogger;
11
use Psr\Log\LoggerInterface;
12
13
/**
14
 * Class Api
15
 * @package leocata\M1
16
 */
17
class Api
18
{
19
20
    protected $logger;
21
    private static $callbacks = [];
22
    private $client;
23
    private $auth;
24
25
    /**
26
     * @param LoggerInterface $logger
27
     * @param HttpClientAuthorization $auth
28
     */
29
    public function __construct(HttpClientAuthorization $auth, LoggerInterface $logger = null)
30
    {
31
        if ($logger === null) {
32
            $logger = new DummyLogger();
33
        }
34
        $this->logger = $logger;
35
36
        $this->client = new HttpClient($auth);
37
        $this->auth = $auth;
38
    }
39
40
    /**
41
     * @param $name
42
     * @param \Closure $func
43
     * @throws \BadFunctionCallException
44
     */
45
    public static function doCallback($name, \Closure $func)
46
    {
47
        if (array_key_exists($name, self::$callbacks)) {
48
            if (!is_callable($func)) {
49
                throw new \BadFunctionCallException();
50
            }
51
            call_user_func($func);
52
        }
53
    }
54
55
    /**
56
     * @param string $data
57
     * @return bool|CallbackMethods
58
     * @throws MethodNotFound | MissingMandatoryField
59
     */
60
    public function getApiCallbackMethod(string $data)
61
    {
62
        $data = \GuzzleHttp\json_decode($data);
63
        if (empty($data->method)) {
64
            return false;
65
        }
66
67
        $class = '\leocata\M1\Methods\Callback\\' . ucfirst($data->method);
68
69
        if (!class_exists($class)) {
70
            throw new MethodNotFound(sprintf(
71
                'The method "%s" not found, please correct',
72
                $data->method
73
            ));
74
        }
75
76
        class_alias($class, $data->method);
77
        /** @var \leocata\M1\Abstracts\CallbackMethods $method */
78
        $method = new $data->method();
79
        $method->import($data->params ?? new \stdClass());
80
        self::$callbacks += ['after' . $method->getMethodName() => $method];
81
82
        return $method;
83
    }
84
85
    /**
86
     * Performs the request to the Api servers
87
     *
88
     * @param RequestMethods $method
89
     * @return RequestMethods
90
     */
91
    public function sendApiRequest(RequestMethods $method)
92
    {
93
        $response = $this->client->getResponseContent($method->getRequestString());
94
        if(!empty($response)) {
95
            $method->setResult($response);
96
        }
97
98
        return $method;
99
    }
100
}
101