Address   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 2

Importance

Changes 0
Metric Value
wmc 6
lcom 2
cbo 2
dl 0
loc 86
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A getName() 0 4 1
B create() 0 30 2
A getCity() 0 8 2
1
<?php
2
3
/**
4
 * @package Faker
5
 * @author Iurii Makukh <[email protected]>
6
 * @copyright Copyright (c) 2017, Iurii Makukh <[email protected]>
7
 * @license https://www.gnu.org/licenses/gpl-3.0.en.html GNU General Public License 3.0
8
 */
9
10
namespace gplcart\modules\faker\models\generators;
11
12
use gplcart\core\models\City as CityModel,
13
    gplcart\core\models\Address as AddressModel;
14
use gplcart\modules\faker\models\Generator as FakerModuleGenerator;
15
16
/**
17
 * Manages basic behaviors and data related to addresses
18
 */
19
class Address extends FakerModuleGenerator
20
{
21
22
    /**
23
     * City model class instance
24
     * @var \gplcart\core\models\City $city
25
     */
26
    protected $city;
27
28
    /**
29
     * Address model class instance
30
     * @var \gplcart\core\models\Address $address
31
     */
32
    protected $address;
33
34
    /**
35
     * @param CityModel $city
36
     * @param AddressModel $address
37
     */
38
    public function __construct(CityModel $city, AddressModel $address)
39
    {
40
        parent::__construct();
41
42
        $this->city = $city;
43
        $this->address = $address;
44
    }
45
46
    /**
47
     * Returns the generator name
48
     * @return string
49
     */
50
    public function getName()
51
    {
52
        return $this->translation->text('Address');
53
    }
54
55
    /**
56
     * Generate a single address
57
     * @return bool
58
     */
59
    public function create()
60
    {
61
        $city = $this->getCity();
62
63
        if (empty($city)) {
64
            return false;
65
        }
66
67
        $types = $this->address->getTypes();
68
69
        $data = array(
70
            'state_id' => $city['state_id'],
71
            'country' => $city['country'],
72
            'city_id' => $city['city_id'],
73
            'address_1' => $this->faker->address(),
74
            'address_2' => $this->faker->secondaryAddress(),
75
            'phone' => $this->faker->phoneNumber(),
76
            'type' => $types[array_rand($types)],
77
            'user_id' => $this->getUserId(),
78
            'last_name' => $this->faker->lastName(),
79
            'middle_name' => $this->faker->lastName(),
80
            'first_name' => $this->faker->firstName(),
81
            'postcode' => $this->faker->postcode(),
82
            'company' => $this->faker->company(),
83
            'fax' => $this->faker->tollFreePhoneNumber(),
84
            'created' => $this->faker->dateTimeBetween('-1 year')->getTimestamp()
85
        );
86
87
        return (bool) $this->address->add($data);
88
    }
89
90
    /**
91
     * Returns a random city
92
     * @staticvar array $cities
93
     * @return array
94
     */
95
    protected function getCity()
96
    {
97
        static $cities = null;
98
        if (!isset($cities)) {
99
            $cities = $this->city->getList(array('limit' => array(0, 300)));
100
        }
101
        return $cities[array_rand($cities)];
102
    }
103
104
}
105