Completed
Pull Request — master (#3)
by Sergey
02:17
created

RequestsContainer   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 104
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 10
c 4
b 0
f 0
lcom 1
cbo 3
dl 0
loc 104
rs 10

6 Methods

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