Completed
Branch master (148e2f)
by Ali
01:30
created

Services::categories()   B

Complexity

Conditions 6
Paths 7

Size

Total Lines 25

Duplication

Lines 15
Ratio 60 %

Importance

Changes 0
Metric Value
dl 15
loc 25
rs 8.8977
c 0
b 0
f 0
cc 6
nc 7
nop 1
1
<?php
2
3
/*
4
 * Part of the Saudi Address API PHP package.
5
 *
6
 * NOTICE OF LICENSE
7
 *
8
 * Licensed under the MIT.
9
 *
10
 * This source file is subject to the MIT License that is
11
 * bundled with this package in the LICENSE file.
12
 *
13
 * @package    Saudi Address
14
 * @version    1.2
15
 * @author     Ali Alharthi
16
 * @license    MIT
17
 * @copyright  (c) 2020, Ali Alharthi
18
 * @link       https://aalharthi.sa
19
 */
20
21
namespace AliAlharthi\SaudiAddress\Api;
22
23
class Services extends Api
24
{
25
    /**
26
     * The response array.
27
     *
28
     * @var array|null
29
     */
30
    protected $response = null;
31
32
    /**
33
     * The cache directory.
34
     *
35
     * @var string|null
36
     */
37
    protected $cacheDir = __DIR__ . '/cache/';
38
39
    /**
40
     * The cache file name.
41
     *
42
     * @var string|null
43
     */
44
    protected $file = __DIR__ . '/cache/' . 'services_';
45
46
    /**
47
     * Returns a list of all the services categories.
48
     *
49
     * @param   string  $lang
50
     * @return  Services
51
     */
52
    public function categories($lang = 'A')
53
    {
54
55
        $cache = $this->file . 'categories_' . strtolower($lang) . '.data';
56
57
        $this->response = $this->cacheValue($cache);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->cacheValue($cache) can also be of type string. However, the property $response is declared as type array|null. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
58
59 View Code Duplication
        if ($this->response == null) {
0 ignored issues
show
Bug introduced by
It seems like you are loosely comparing $this->response of type string|null against null; this is ambiguous if the string can be empty. Consider using a strict comparison === instead.
Loading history...
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
60
            $response = $this->_get(
61
                'v3.1/lookup/service-categories',
62
                $lang
63
            );
64
            if($response['success'] == false){
65
                return ;
66
            }
67
            if($this->config->getCache()){
68
                (!file_exists($this->cacheDir)) ?
69
                    mkdir($this->cacheDir, 0777, false) : ((file_exists($cache)) ? unlink($cache) : touch($cache));
70
                file_put_contents($cache, serialize($response['ServiceCategories']));
71
            }
72
            $this->response = $response['ServiceCategories'];
73
        }
74
75
        return $this;
76
    }
77
78
    /**
79
     * Returns a list of all the services categories.
80
     *
81
     * @param   string  $lang
82
     * @return  array
83
     */
84
    public function main($lang = 'A')
85
    {
86
        return $this->categories($lang);
87
    }
88
89
    /**
90
     * Returns a list of all the services categories.
91
     *
92
     * @param   string  $lang
93
     * @return  array
94
     */
95
    public function cat($lang = 'A')
96
    {
97
        return $this->categories($lang);
98
    }
99
100
    /**
101
     * Returns a list of all the sub services of a service.
102
     *
103
     * @param   int     $serviceId
104
     * @param   string  $lang
105
     * @return  Services
106
     */
107
    public function sub(int $serviceId = 1, $lang = 'A')
108
    {
109
        $cache = $this->file . 'sub_' . $serviceId . '_' . strtolower($lang) . '.data';
110
111
        $this->response = $this->cacheValue($cache);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->cacheValue($cache) can also be of type string. However, the property $response is declared as type array|null. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
112
113 View Code Duplication
        if ($this->config->getCache() && $this->response == null) {
0 ignored issues
show
Bug introduced by
It seems like you are loosely comparing $this->response of type string|null against null; this is ambiguous if the string can be empty. Consider using a strict comparison === instead.
Loading history...
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
114
            $response = $this->_get(
115
                'v3.1/lookup/services-sub-categories',
116
                $lang,
117
                [
118
                    'servicecategoryid' => $serviceId
119
                ]
120
            );
121
            if ($response['success'] == false) {
122
                return;
123
            }
124
            if($this->config->getCache()){
125
                (!file_exists($this->cacheDir)) ?
126
                    mkdir($this->cacheDir, 0777, false) : ((file_exists($cache)) ? unlink($cache) : touch($cache));
127
                file_put_contents($cache, serialize($response['ServiceSubCategories']));
128
            }
129
            $this->response = $response['ServiceSubCategories'];
130
        }
131
132
        return $this;
133
    }
134
135
    /**
136
     * Returns a list of all the sub services of a service.
137
     *
138
     * @param   int     $serviceId
139
     * @param   string  $lang
140
     * @return  array
141
     */
142
    public function subCategories(int $serviceId = 1, $lang = 'A')
143
    {
144
        $this->sub($serviceId, $lang);
145
    }
146
147
    /**
148
     * Returns a list of all the sub services of a service.
149
     *
150
     * @param   int     $serviceId
151
     * @param   string  $lang
152
     * @return  array
153
     */
154
    public function subServices(int $serviceId = 1, $lang = 'A')
155
    {
156
        $this->sub($serviceId, $lang);
157
    }
158
159
    /**
160
     * Returns a the response.
161
     *
162
     * @return  array
163
     */
164
    public function get()
165
    {
166
        $this->check();
167
168
        return $this->response;
169
    }
170
171
    /**
172
     * Returns a specific service by id.
173
     *
174
     * @param   int     $serviceId
175
     * @return  array
176
     */
177 View Code Duplication
    public function getId(int $serviceId)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
178
    {
179
        $this->check();
180
181
        $key = array_search($serviceId, array_column($this->response, 'Id'));
182
183
        return $this->response[$key];
184
    }
185
186
    /**
187
     * Returns a specific service by id.
188
     *
189
     * @param   int     $serviceId
190
     * @return  array
191
     */
192
    public function byId(int $serviceId)
193
    {
194
        return $this->getId($serviceId);
195
    }
196
197
    /**
198
     * Returns a specific service by id.
199
     *
200
     * @param   int     $serviceId
201
     * @return  array
202
     */
203
    public function id(int $serviceId)
204
    {
205
        return $this->getId($serviceId);
206
    }
207
208
    /**
209
     * Returns a specific service by name.
210
     *
211
     * @param   string  $serviceName
212
     * @return  array
213
     */
214 View Code Duplication
    public function getName($serviceName)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
215
    {
216
        $this->check();
217
218
        $key = array_search($serviceName, array_column($this->response, 'Name'));
219
220
        return $this->response[$key];
221
    }
222
223
    /**
224
     * Returns a specific service by name.
225
     *
226
     * @param   string  $serviceName
227
     * @return  array
228
     */
229
    public function byName($serviceName)
230
    {
231
        return $this->getName($serviceName);
232
    }
233
234
    /**
235
     * Returns a specific service by name.
236
     *
237
     * @param   string  $serviceName
238
     * @return  array
239
     */
240
    public function named($serviceName)
241
    {
242
        return $this->getName($serviceName);
243
    }
244
245
    /**
246
     * Returns a specific service by name.
247
     *
248
     * @param   string  $serviceName
249
     * @return  array
250
     */
251
    public function serviceName($serviceName)
252
    {
253
        return $this->getName($serviceName);
254
    }
255
256
    /**
257
     * Check if all() method was called first.
258
     *
259
     * @return  void
260
     * @throws  \BadMethodCallException
261
     */
262
    protected function check()
263
    {
264
        if ($this->response == null) {
265
            throw new \BadMethodCallException("You need to call categories() or sub() methods first.");
266
        }
267
    }
268
}
269