Passed
Push — master ( b808b9...919917 )
by Ehsan
04:54
created

AbstractAccessList::getSubAccessControlList()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 1
crap 2
1
<?php
2
3
namespace Botonomous;
4
5
use Botonomous\client\ApiClient;
6
use Botonomous\utility\ClassUtility;
7
8
abstract class AbstractAccessList
9
{
10
    /**
11
     * Dependencies.
12
     */
13
    private $request;
14
    private $dictionary;
15
    private $apiClient;
16
    private $classUtility;
17
18
    /**
19
     * @return mixed
20
     */
21 18
    protected function getAccessControlList()
22
    {
23 18
        return $this->getDictionary()->get('access-control');
24
    }
25
26
    /**
27
     * @param $sublistKey
28
     *
29
     * @return mixed
30
     */
31 18
    protected function getSubAccessControlList($sublistKey)
32
    {
33 18
        $list = $this->getAccessControlList();
34
35 18
        if (!isset($list[$sublistKey])) {
36
            /* @noinspection PhpInconsistentReturnPointsInspection */
37 1
            return;
38
        }
39
40 18
        return $list[$sublistKey];
41
    }
42
43
    /**
44
     * @param array $list
45
     *
46
     * @return bool
47
     */
48 9
    protected function isEmailInList(array $list)
49
    {
50
        // get user info
51 9
        $userInfo = $this->getSlackUserInfo();
52
53 9
        return !empty($userInfo) && in_array($userInfo['profile']['email'], $list['userEmail']);
54
    }
55
56
    /**
57
     * @param string $requestKey
58
     * @param $listKey
59
     * @param string $subListKey
60
     *
61
     * @return null|bool
62
     */
63 12
    protected function findInListByRequestKey($requestKey, $listKey, $subListKey)
64
    {
65
        // get request
66 12
        $request = $this->getRequest();
67
68
        /**
69
         * load the relevant list to start checking
70
         * The list name is the called class name e.g. WhiteList in lowercase.
71
         */
72 12
        $list = $this->getSubAccessControlList($listKey);
73
74 12
        if ($list === null) {
75
            /* @noinspection PhpInconsistentReturnPointsInspection */
76 1
            return;
77
        }
78
79
        // currently if list key is not set we do not check it
80 12
        if (!isset($list[$subListKey])) {
81
            /* @noinspection PhpInconsistentReturnPointsInspection */
82 8
            return;
83
        }
84
85 11
        return in_array($request[$requestKey], $list[$subListKey]);
86
    }
87
88
    /**
89
     * @return mixed
90
     */
91 18
    protected function getShortClassName()
92
    {
93 18
        return $this->getClassUtility()->extractClassNameFromFullName(strtolower(get_called_class()));
94
    }
95
96
    /**
97
     * @return mixed
98
     */
99 19
    public function getRequest()
100
    {
101 19
        return $this->request;
102
    }
103
104
    /**
105
     * @param mixed $request
106
     */
107 22
    public function setRequest($request)
108
    {
109 22
        $this->request = $request;
110 22
    }
111
112
    /**
113
     * @return Dictionary
114
     */
115 18
    public function getDictionary()
116
    {
117 18
        if (!isset($this->dictionary)) {
118 5
            $this->setDictionary(new Dictionary());
119
        }
120
121 18
        return $this->dictionary;
122
    }
123
124
    /**
125
     * @param Dictionary $dictionary
126
     */
127 18
    public function setDictionary(Dictionary $dictionary)
128
    {
129 18
        $this->dictionary = $dictionary;
130 18
    }
131
132
    /**
133
     * @return ApiClient
134
     */
135 11
    public function getApiClient()
136
    {
137 11
        if (!isset($this->apiClient)) {
138 5
            $this->setApiClient(new ApiClient());
139
        }
140
141 11
        return $this->apiClient;
142
    }
143
144
    /**
145
     * @param ApiClient $apiClient
146
     */
147 14
    public function setApiClient(ApiClient $apiClient)
148
    {
149 14
        $this->apiClient = $apiClient;
150 14
    }
151
152
    /**
153
     * @return array|bool
154
     */
155 11
    public function getSlackUserInfo()
156
    {
157
        // get user id in the request
158 11
        $request = $this->getRequest();
159
160
        // currently if user_id is not set we do not check it
161 11
        if (!isset($request['user_id'])) {
162 1
            return false;
163
        }
164
165
        /**
166
         * email normally does not exist in the request.
167
         * Get it by user_id. For this users:read and users:read.email are needed.
168
         */
169 10
        $userInfo = $this->getApiClient()->userInfo(['user' => $request['user_id']]);
170
171 10
        if (empty($userInfo)) {
172
            /*
173
             * Could not find the user in the team
174
             * Probably there might be some issue with Access token and reading user info but block the access
175
             */
176 4
            return false;
177
        }
178
179 6
        return $userInfo;
180
    }
181
182
    /**
183
     * @return ClassUtility
184
     */
185 18
    public function getClassUtility()
186
    {
187 18
        if (!isset($this->classUtility)) {
188 18
            $this->setClassUtility(new ClassUtility());
189
        }
190
191 18
        return $this->classUtility;
192
    }
193
194
    /**
195
     * @param ClassUtility $classUtility
196
     */
197 18
    public function setClassUtility(ClassUtility $classUtility)
198
    {
199 18
        $this->classUtility = $classUtility;
200 18
    }
201
}
202