Completed
Push — develop ( f8095c...0ca917 )
by Alireza
01:53
created

Generator::generateMeliCode()   D

Complexity

Conditions 10
Paths 5

Size

Total Lines 43
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 10
eloc 25
c 1
b 0
f 0
nc 5
nop 0
dl 0
loc 43
rs 4.8196

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Josh\Faker;
4
5
class Generator
6
{
7
8
    /**
9
     * List of first names
10
     *
11
     * @var array
12
     */
13
    protected $objects = [];
14
15
    /**
16
     * Set objects, Generator constructor.
17
     *
18
     * @author Alireza Josheghani <[email protected]>
19
     * @since 3 Feb 2017
20
     */
21
    public function __construct()
22
    {
23
        $this->objects = require __DIR__ . '/../objects.php';
24
    }
25
26
    /**
27
     * Get firtname
28
     *
29
     * @author Alireza Josheghani <[email protected]>
30
     * @since 13 Dec 2016
31
     * @return mixed
32
     */
33
    public function firstname()
34
    {
35
        return $this->getRandomKey('names');
36
    }
37
38
    /**
39
     * Get last name
40
     *
41
     * @author Alireza Josheghani <[email protected]>
42
     * @since 13 Dec 2016
43
     * @return mixed
44
     */
45
    public function lastname()
46
    {
47
        return $this->getRandomKey('families');
48
    }
49
    
50
    
51
    /**
52
     * Get company name
53
     *
54
     * @author Vahid Almasi <[email protected]>
55
     * @since 29 Jan 2017
56
     * @return mixed
57
     */
58
    public function company()
59
    {
60
        return $this->getRandomKey('companies');
61
    }
62
63
    /**
64
     * Get random firstname and lastname
65
     *
66
     * @author Alireza Josheghani <[email protected]>
67
     * @since 13 Dec 2016
68
     * @return string
69
     */
70
    public function fullname()
71
    {
72
        return $this->firstname() . ' ' . $this->lastname();
73
    }
74
75
    /**
76
     * Get telephone
77
     *
78
     * @author Alireza Josheghani <[email protected]>
79
     * @since 13 Dec 2016
80
     * @return string
81
     */
82
    public function mobile()
83
    {
84
        $prefix = $this->getRandomKey('prefixTelePhones');
85
86
        $phone = string('0' . $prefix . randomNumber(7));
87
88
        return ( strlen($phone) !== 11 ? $phone . rand(1,10) : $phone );
89
    }
90
91
    /**
92
     * Get random phone
93
     *
94
     * @author Alireza Josheghani <[email protected]>
95
     * @since 13 Dec 2016
96
     * @return string
97
     */
98
    public function telephone()
99
    {
100
        $prefix = $this->getRandomKey('prefixPhones');
101
102
        return string('0' . $prefix . randomNumber(7));
103
    }
104
105
    /**
106
     * Get random email
107
     *
108
     * @author Alireza Josheghani <[email protected]>
109
     * @since 13 Dec 2016
110
     * @return string
111
     */
112
    public function email()
113
    {
114
        $service = $this->getRandomKey('mailServices');
115
116
        return string(randomString(30,'lowercase') . '@' . $service);
117
    }
118
119
    /**
120
     * Get random domain
121
     *
122
     * @author Alireza Josheghani <[email protected]>
123
     * @since 13 Dec 2016
124
     * @return string
125
     */
126
    public function domain()
127
    {
128
        $domain = $this->getRandomKey('domains');
129
130
        return string(randomString(20,'lowercase') . $domain);
131
    }
132
133
    /**
134
     * Get random website
135
     *
136
     * @author Alireza Josheghani <[email protected]>
137
     * @since 13 Dec 2016
138
     * @return string
139
     */
140
    public function website()
141
    {
142
        $protocol = $this->getRandomKey('domains');
143
        
144
        return string($protocol . '://www' . $this->domain());
145
    }
146
147
    /**
148
     * Get random page url
149
     *
150
     * @author Alireza Josheghani <[email protected]>
151
     * @since 13 Dec 2016
152
     * @return string
153
     */
154
    public function pageUrl()
155
    {
156
        $rand = rand(3,7);
157
158
        $string = '';
159
160
        $count = rand(2,8);
161
162
        for($i=0;$i < $rand;$i++){
163
            $string .= randomString($count,'lowercase') . '/';
164
        }
165
166
        return rtrim($string,'/');
167
    }
168
169
    /**
170
     * Get random age
171
     *
172
     * @author Alireza Josheghani <[email protected]>
173
     * @since 13 Dec 2016
174
     * @param int $min
175
     * @param int $max
176
     * @return int
177
     */
178
    public function age($min = 16, $max = 70)
179
    {
180
        return rand($min,$max);
181
    }
182
183
    /**
184
     * Get random address
185
     *
186
     * @author Vahid Almasi <[email protected]>
187
     * @since 3 Feb 2017
188
     * @return mixed
189
     */
190
    public function address()
191
    {
192
        return $this->getRandomKey('address');
193
    }
194
195
    /**
196
     * Get random city
197
     *
198
     * @author Vahid Almasi <[email protected]>
199
     * @since 4 Feb 2017
200
     * @return mixed
201
     */
202
    public function city()
203
    {
204
        return $this->getRandomKey('city');
205
    }
206
207
    /**
208
     * Generate random melicode number
209
     *
210
     * @author Alireza Josheghani <[email protected]>
211
     * @since 4 Feb 2017
212
     * @return int|null|string
213
     */
214
    public function meliCode()
215
    {
216
        $code = $this->generateMeliCode();
217
218
        if(is_null($code)){
219
            $code = $this->generateMeliCode();
220
        }
221
222
        return $code;
223
    }
224
225
    /**
226
     * Generate meli code
227
     *
228
     * @author Alireza Josheghani <[email protected]>
229
     * @since 5 Feb 2017
230
     * @return int|null|string
231
     */
232
    private function generateMeliCode()
233
    {
234
        $code = null;
235
236
        for($i = 1;$i < 100;$i++){
237
238
            $meli = randomNumber(10,true);
239
240
            if(strlen($meli) != 10){
241
                continue;
242
            }
243
244
            $c = substr($meli,9,1);
245
246
            $n = substr($meli,0,1) * 10 +
247
                substr($meli,1,1) * 9 +
248
                substr($meli,2,1) * 8 +
249
                substr($meli,3,1) * 7 +
250
                substr($meli,4,1) * 6 +
251
                substr($meli,5,1) * 5 +
252
                substr($meli,6,1) * 4 +
253
                substr($meli,7,1) * 3 +
254
                substr($meli,8,1) * 2;
255
256
            $r = $n - (int)($n / 11) * 11;
257
258
            if (($r == 0 && $r == $c) || ($r == 1 && $c == 1) || ($r > 1 && $c == 11 - $r)) {
259
260
                if(! isMeliCode($meli)){
261
                    continue;
262
                }
263
264
                $code = $meli;
265
266
            } else {
267
                continue;
268
            }
269
270
            $i++;
271
        }
272
273
        return $code;
274
    }
275
276
    /**
277
     * Get random key from array
278
     *
279
     * @author Alireza Josheghani <[email protected]>
280
     * @since 4 Feb 2017
281
     * @param $object
282
     * @return string
283
     */
284
    private function getRandomKey($object = null)
285
    {
286
        $name = 0;
287
        $array = [];
288
289
        if(is_array($object)){
290
            $array = $object;
291
            $name = array_rand($object);
292
        } elseif(is_string($object)) {
293
            $array = $this->objects[$object];
294
            $name = array_rand($array);
295
        }
296
297
        return string($array[$name]);
298
    }
299
300
}
301