Completed
Push — master ( c6d0a7...e2c92c )
by Sergey
06:14 queued 04:15
created

RequestsContainer::buildRequest()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 1
1
<?php
2
3
namespace seregazhuk\SmsIntel\Api\Requests;
4
5
use ReflectionClass;
6
use seregazhuk\SmsIntel\Contracts\HttpClient;
7
use seregazhuk\SmsIntel\Exceptions\WrongRequest;
8
9
class RequestsContainer
10
{
11
12
    /**
13
     * @var HttpClient
14
     */
15
    protected $http;
16
17
    /**
18
     * @var string
19
     */
20
    protected $login;
21
22
    /**
23
     * @var string
24
     */
25
    protected $password;
26
27
    /**
28
     * @var Request[]
29
     */
30
    protected $requests = [];
31
32
    public function __construct(HttpClient $http, $login, $password)
33
    {
34
        $this->http = $http;
35
        $this->login = $login;
36
        $this->password = $password;
37
    }
38
39
    /**
40
     * @return array
41
     */
42
    protected function getRequestsActionsMap()
43
    {
44
        return [
45
            XMLRequest::class  => XMLRequest::$allowedMethods,
46
            JSONRequest::class => JSONRequest::$allowedMethods,
47
        ];
48
    }
49
50
    /**
51
     * Proxies all methods to the appropriate Request object
52
     *
53
     * @param string $method
54
     * @param array $arguments
55
     * @return array
56
     */
57
    public function __call($method, $arguments)
58
    {
59
        $request = $this->resolveRequestByAction($method);
60
61
        return $request->$method(...$arguments);
62
    }
63
64
    /**
65
     * Gets request object by name. If there is no such request
66
     * in requests array, it will try to create it, then save
67
     * it, and then return.
68
     *
69
     * @param string $requestClass
70
     *
71
     * @throws WrongRequest
72
     *
73
     * @return Request
74
     */
75
    public function getRequest($requestClass)
76
    {
77
        // Check if an instance has already been initiated
78
        if (!isset($this->requests[$requestClass])) {
79
            $this->addRequest($requestClass);
80
        }
81
        return $this->requests[$requestClass];
82
    }
83
84
    /**
85
     * @param $action
86
     * @return string
87
     * @throws WrongRequest
88
     */
89
    public function resolveRequestByAction($action)
90
    {
91
        foreach ($this->getRequestsActionsMap() as $requestClass => $actions) {
92
            if(in_array($action, $actions)) {
93
                return $this->getRequest($requestClass);
94
            }
95
        }
96
97
        throw new WrongRequest("Action $action doesn't exist!");
98
    }
99
100
    /**
101
     * Creates request by class name, and if success saves
102
     * it to requests array.
103
     *
104
     * @param string $requestClass
105
     *
106
     * @throws WrongRequest
107
     */
108
    protected function addRequest($requestClass)
109
    {
110
        if (!class_exists($requestClass)) {
111
            throw new WrongRequest("Request $requestClass not found.");
112
        }
113
        $this->requests[$requestClass] = $this->buildRequest($requestClass);
114
    }
115
116
    /**
117
     * Build RequestInterface object with reflection API.
118
     *
119
     * @param string $className
120
     *
121
     * @return object
122
     */
123
    protected function buildRequest($className)
124
    {
125
        return (new ReflectionClass($className))
126
            ->newInstanceArgs([$this->http])
127
            ->setCredentials($this->login, $this->password);
128
    }
129
}