Passed
Push — master ( 01e76a...f881c5 )
by Leo
01:36
created

Api::downloadFile()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 1
1
<?php
2
3
namespace leocata\M1;
4
5
use leocata\M1\Abstracts\Methods;
6
use leocata\M1\InternalFunctionality\BodyConstructor;
7
use leocata\M1\InternalFunctionality\DummyLogger;
8
use Psr\Log\LoggerInterface;
9
use React\Promise\PromiseInterface;
10
11
/**
12
 * Class Api
13
 * @package leocata\M1
14
 */
15
class Api
16
{
17
18
    /**
19
     * @var RequestHandlerInterface
20
     */
21
    protected $requestHandler;
22
23
    /**
24
     * @var BodyConstructor
25
     */
26
    protected $bodyConstructor;
27
28
    /**
29
     * Contains an instance to a PSR-3 compatible logger
30
     * @var LoggerInterface
31
     */
32
    protected $logger;
33
34
    /**
35
     * @var string
36
     */
37
    protected $methodName = '';
38
39
    /**
40
     * Stores the API URL
41
     * @var string
42
     */
43
    private $host;
44
45
    /**
46
     * Api constructor.
47
     *
48
     * @param string $host
49
     * @param array $clientAuthorization
50
     * @param RequestHandlerInterface $handler
51
     * @param LoggerInterface $logger
52
     */
53
    public function __construct(
54
        string $host,
55
        array $clientAuthorization,
56
        RequestHandlerInterface $handler,
57
        LoggerInterface $logger = null
58
    ) {
59
60
        if ($logger === null) {
61
            $logger = new DummyLogger();
62
        }
63
        $this->logger = $logger;
64
65
        $this->requestHandler = $handler;
66
        $this->bodyConstructor = new BodyConstructor($logger, $clientAuthorization);
67
        $this->host = $host;
68
    }
69
70
    /**
71
     * Performs the request to server
72
     * @param Methods $method
73
     * @return PromiseInterface
74
     * @throws \leocata\M1\Exceptions\MissingMandatoryField
75
     */
76
    public function performApiRequest(Methods $method): PromiseInterface
77
    {
78
        $this->logger->debug('Request for async API call, resetting internal values', [get_class($method)]);
79
        $option = $this->bodyConstructor->constructOptions($method);
80
81
        return $this->sendRequestToTelegram($method, $option)
82
            ->then(function (Response $response) use ($method) {
83
                return $method::bindToObject($response, $this->logger);
84
            }, function ($error) {
85
                $this->logger->error($error);
86
                throw $error;
87
            });
88
    }
89
90
    /**
91
     * Resets everything to the default values
92
     * @return Api
93
     */
94
    final protected function resetObjectValues(): Api
95
    {
96
        $this->bodyConstructor->formType = 'application/x-www-form-urlencoded';
0 ignored issues
show
Bug introduced by
The property formType does not seem to exist on leocata\M1\InternalFunctionality\BodyConstructor.
Loading history...
97
98
        return $this;
99
    }
100
101
    /**
102
     * @param Methods $method
103
     * @param array $formData
104
     * @return PromiseInterface
105
     */
106
    protected function sendRequestToTelegram(Methods $method, array $formData): PromiseInterface
107
    {
108
        $this->logger->debug('About to perform async HTTP call to M1\'s API');
109
110
        return $this->requestHandler->post($this->composeApiMethodUrl($method), $formData);
111
    }
112
113
    /**
114
     * @param Methods $call
115
     * @return string
116
     */
117
    protected function composeApiMethodUrl(Methods $call): string
118
    {
119
        $completeClassName = get_class($call);
120
        $this->methodName = substr($completeClassName, strrpos($completeClassName, '\\') + 1);
121
        $this->logger->info('About to perform API request', ['method' => $this->methodName]);
122
123
        return $this->host . $this->methodName;
124
    }
125
}
126