Passed
Branch simplistic (3d6a47)
by Ali
02:39
created

ShortAddress::coordinates()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
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    2.0
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 ShortAddress 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/' . 'short_address_';
45
46
    /**
47
     * The short address.
48
     *
49
     * @var  string|null
50
     */
51
    protected $short = null;
52
53
54
    /**
55
     * Constructor.
56
     *
57
     * @param  \AliAlharthi\SaudiAddress\ConfigInterface  $config
58
     * @param  string  $short
59
     */
60
    function __construct($config, $short)
61
    {
62
        parent::__construct($config);
63
        $this->checkShortAddressFormat($short);
64
        $this->short = $short;
65
    }
66
67
    /**
68
     * Returns the full address from a short address.
69
     *
70
     * @return  ShortAddress
71
     */
72
    public function fullAddress()
73
    {
74
        $cache = $this->file . preg_replace('/[^a-zA-Z0-9_]/', '_', $this->short) . '_' . strtolower($this->config->getLocale()) . '.data';
75
76
        $this->response = $this->cacheValue($cache);
77
78
        if ($this->response == null) {
79
            $response = $this->_get(
80
                'NationalAddressByShortAddress/NationalAddressByShortAddress',
81
                [
82
                    'shortaddress'      => $this->short,
83
                ],
84
                false
85
            );
86
87
            if ($response['success'] == false) {
88
                return;
89
            }
90
91
            $addresses = $response;
92
93
            if($this->config->getCache()){
94
                (!file_exists($this->cacheDir)) ?
0 ignored issues
show
Bug introduced by
It seems like $this->cacheDir can also be of type null; however, parameter $filename of file_exists() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

94
                (!file_exists(/** @scrutinizer ignore-type */ $this->cacheDir)) ?
Loading history...
95
                    mkdir($this->cacheDir, 0777, false) : ((file_exists($cache)) ? unlink($cache) : touch($cache));
0 ignored issues
show
Bug introduced by
It seems like $this->cacheDir can also be of type null; however, parameter $directory of mkdir() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

95
                    mkdir(/** @scrutinizer ignore-type */ $this->cacheDir, 0777, false) : ((file_exists($cache)) ? unlink($cache) : touch($cache));
Loading history...
96
                file_put_contents($cache, serialize($addresses));
97
            }
98
            $this->response = $addresses;
99
100
        }
101
102
        return $this->response['Addresses'][0] ?? [];
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->response['Addresses'][0] ?? array() also could return the type array which is incompatible with the documented return type AliAlharthi\SaudiAddress\Api\ShortAddress.
Loading history...
103
    }
104
105
    /**
106
     * Verify an address.
107
     *
108
     * @return  bool
109
     */
110
    public function verify()
111
    {
112
        if($this->response !== null){
113
            return (bool) ((int) $this->response['totalSearchResults'] > 0);
114
        }
115
116
        $skipReturn = $this->fullAddress();
0 ignored issues
show
Unused Code introduced by
The assignment to $skipReturn is dead and can be removed.
Loading history...
117
118
        return (bool) ((int) $this->response['totalSearchResults'] > 0);
119
    }
120
121
    /**
122
     * Returns the geo location from a short address.
123
     *
124
     * @return  array
125
     */
126
    public function geo()
127
    {
128
        $cache = $this->file . preg_replace('/[^a-zA-Z0-9_]/', '_', $this->short) . '_geo_' . strtolower($this->config->getLocale()) . '.data';
129
130
        $response = $this->cacheValue($cache);
131
132
        if ($response == null) {
133
            $response = $this->_get(
134
                'GeoCodeByShortAddress/GeoCodeByShortlAddress',
135
                [
136
                    'shortaddress'      => $this->short,
137
                ],
138
                false
139
            );
140
141
            if ($response['success'] == false) {
142
                return;
143
            }
144
145
            $addresses = $response['Addresses'];
146
147
            if($this->config->getCache()){
148
                (!file_exists($this->cacheDir)) ?
0 ignored issues
show
Bug introduced by
It seems like $this->cacheDir can also be of type null; however, parameter $filename of file_exists() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

148
                (!file_exists(/** @scrutinizer ignore-type */ $this->cacheDir)) ?
Loading history...
149
                    mkdir($this->cacheDir, 0777, false) : ((file_exists($cache)) ? unlink($cache) : touch($cache));
0 ignored issues
show
Bug introduced by
It seems like $this->cacheDir can also be of type null; however, parameter $directory of mkdir() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

149
                    mkdir(/** @scrutinizer ignore-type */ $this->cacheDir, 0777, false) : ((file_exists($cache)) ? unlink($cache) : touch($cache));
Loading history...
150
                file_put_contents($cache, serialize($addresses));
151
            }
152
            $response = $addresses;
0 ignored issues
show
Unused Code introduced by
The assignment to $response is dead and can be removed.
Loading history...
153
154
        }
155
156
        return $this->response['Addresses'][0] ?? [];
157
    }
158
159
    /**
160
     * Returns the geo location from a short address.
161
     *
162
     * @return  array
163
     */
164
    public function coordinates()
165
    {
166
        $this->geo();
167
    }
168
169
    /**
170
     * Check if find() method was called first.
171
     *
172
     * @param   string  $shortAddress
173
     * @return  void
174
     * @throws  \AliAlharthi\SaudiAddress\Exception\SaudiAddressException
175
     */
176
    protected function checkShortAddressFormat($shortAddress)
177
    {
178
        if (!preg_match('/^[A-Z]{4}\d{4}$/', $shortAddress) > 0) {
179
            throw new \AliAlharthi\SaudiAddress\Exception\SaudiAddressException("Incorrect short address format: should consists of 4 letters followed by 4 numbers.");
180
        }
181
    }
182
}
183