Completed
Push — master ( 7b23a1...148e2f )
by Ali
01:22
created

Regions::check()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
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 Regions 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/' . 'regions_';
45
46
    /**
47
     * Returns a list of all the regions.
48
     *
49
     * @param   string  $lang
50
     * @return  Regions
51
     */
52
    public function all($lang = 'A')
53
    {
54
        $cache = $this->file . strtolower($lang) . '.data';
55
56
        $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...
57
58 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...
59
            $response = $this->_get(
60
                'v3.1/lookup/regions',
61
                $lang
62
            );
63
            if($this->config->getCache()){
64
                (!file_exists($this->cacheDir)) ?
65
                mkdir($this->cacheDir, 0777, false) : ((file_exists($cache)) ? unlink($cache) : touch($cache));
66
                file_put_contents($cache, serialize($response));
67
            }
68
            $this->response = $response;
69
        }
70
71
        return $this;
72
    }
73
74
    /**
75
     * Returns a the response.
76
     *
77
     * @return  array
78
     */
79
    public function get()
80
    {
81
        $this->check();
82
83
        return $this->response['Regions'];
84
    }
85
86
    /**
87
     * Returns a certian region info by id.
88
     *
89
     * @param   int     $regionId
90
     * @return  array
91
     */
92 View Code Duplication
    public function getId(int $regionId)
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...
93
    {
94
        $this->check();
95
96
        $key = array_search($regionId, array_column($this->response['Regions'], 'Id'));
97
98
        return $this->response['Regions'][$key];
99
    }
100
101
    /**
102
     * Returns a certian region info by id.
103
     *
104
     * @param   int     $regionId
105
     * @return  array
106
     */
107
    public function byId(int $regionId)
108
    {
109
        return $this->getId($regionId);
110
    }
111
112
    /**
113
     * Returns a certian region info by id.
114
     *
115
     * @param   int     $regionId
116
     * @return  array
117
     */
118
    public function id(int $regionId)
119
    {
120
        return $this->getId($regionId);
121
    }
122
123
    /**
124
     * Returns a certian region info by name.
125
     *
126
     * @param   string  $name
127
     * @return  array
128
     */
129 View Code Duplication
    public function getName($name)
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...
130
    {
131
        $this->check();
132
133
        $key = array_search(ucwords($name), array_column($this->response['Regions'], 'Name'));
134
135
        return $this->response['Regions'][$key];
136
    }
137
138
    /**
139
     * Returns a certian region info by name.
140
     *
141
     * @param   string  $name
142
     * @return  array
143
     */
144
    public function name($name)
145
    {
146
        return $this->getName($name);
147
    }
148
149
    /**
150
     * Returns a certian region info by name.
151
     *
152
     * @param   string  $name
153
     * @return  array
154
     */
155
    public function byName($name)
156
    {
157
        return $this->getName($name);
158
    }
159
160
    /**
161
     * Returns a certian region info by name.
162
     *
163
     * @param   string  $name
164
     * @return  array
165
     */
166
    public function named($name)
167
    {
168
        return $this->getName($name);
169
    }
170
171
    /**
172
     * Check if all() method was called first.
173
     *
174
     * @return  void
175
     * @throws  \BadMethodCallException
176
     */
177
    protected function check()
178
    {
179
        if ($this->response == null) {
180
            throw new \BadMethodCallException("You need to call all() method first.");
181
        }
182
    }
183
}
184