State::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 3
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\Country as CountryModel;
13
use gplcart\core\models\CountryState as CountryStateModel;
14
use gplcart\core\models\Zone as ZoneModel;
15
use gplcart\modules\faker\models\Generator as FakerModuleGenerator;
16
17
/**
18
 * Manages basic behaviors and data related to country states
19
 */
20
class State extends FakerModuleGenerator
21
{
22
23
    /**
24
     * Zone model class instance
25
     * @var \gplcart\core\models\Zone $zone
26
     */
27
    protected $zone;
28
29
    /**
30
     * State model class instance
31
     * @var \gplcart\core\models\CountryState $state
32
     */
33
    protected $state;
34
35
    /**
36
     * Country model class instance
37
     * @var \gplcart\core\models\Country $country
38
     */
39
    protected $country;
40
41
    /**
42
     * @param ZoneModel $zone
43
     * @param CountryStateModel $state
44
     * @param CountryModel $country
45
     */
46
    public function __construct(ZoneModel $zone, CountryStateModel $state, CountryModel $country)
47
    {
48
        parent::__construct();
49
50
        $this->zone = $zone;
51
        $this->state = $state;
52
        $this->country = $country;
53
    }
54
55
    /**
56
     * Returns the generator name
57
     * @return string
58
     */
59
    public function getName()
60
    {
61
        return $this->translation->text('Country state');
62
    }
63
64
    /**
65
     * Generate a single country state
66
     * @return bool
67
     */
68
    public function create()
69
    {
70
        $data = array(
71
            'name' => $this->faker->state(),
72
            'zone_id' => $this->getZoneId(),
73
            'status' => $this->faker->boolean(),
74
            'code' => $this->faker->stateAbbr(),
75
            'country' => $this->getCountryCode()
76
        );
77
78
        return (bool) $this->state->add($data);
79
    }
80
81
    /**
82
     * Returns a random country code
83
     * @return string
84
     */
85
    protected function getCountryCode()
86
    {
87
        $countries = $this->country->getList(array('limit' => array(0, 100)));
88
        return array_rand($countries);
89
    }
90
91
    /**
92
     * Returns a random zone ID
93
     * @staticvar array|null $zones
94
     * @return integer
95
     */
96
    protected function getZoneId()
97
    {
98
        static $zones = null;
99
        if (!isset($zones)) {
100
            $zones = $this->zone->getList(array('limit' => array(0, 100)));
101
        }
102
        return (int) array_rand($zones);
103
    }
104
105
}
106