Country   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 97
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 11
lcom 2
cbo 2
dl 0
loc 97
rs 10
c 1
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A getName() 0 4 1
A create() 0 21 2
B getFormat() 0 14 5
A getZoneId() 0 10 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\Zone as ZoneModel,
13
    gplcart\core\models\Country as CountryModel;
14
use gplcart\modules\faker\models\Generator as FakerModuleGenerator;
15
16
/**
17
 * Manages basic behaviors and data related to countries
18
 */
19
class Country extends FakerModuleGenerator
20
{
21
22
    /**
23
     * Zone model class instance
24
     * @var \gplcart\core\models\Zone $zone
25
     */
26
    protected $zone;
27
28
    /**
29
     * Country model class instance
30
     * @var \gplcart\core\models\Country $country
31
     */
32
    protected $country;
33
34
    /**
35
     * @param ZoneModel $zone
36
     * @param CountryModel $country
37
     */
38
    public function __construct(ZoneModel $zone, CountryModel $country)
39
    {
40
        parent::__construct();
41
42
        $this->zone = $zone;
43
        $this->country = $country;
44
    }
45
46
    /**
47
     * Returns the generator name
48
     * @return string
49
     */
50
    public function getName()
51
    {
52
        return $this->translation->text('Country');
53
    }
54
55
    /**
56
     * Generate a single country
57
     * @return bool
58
     */
59
    public function create()
60
    {
61
        $countries = $this->country->getIso();
62
        $code = array_rand($countries);
63
64
        if ($this->country->get($code)) {
65
            return false;
66
        }
67
68
        $data = array(
69
            'code' => $code,
70
            'name' => $countries[$code]['name'],
71
            'format' => $this->getFormat(),
72
            'zone_id' => $this->getZoneId(),
73
            'native_name' => $countries[$code]['name'],
74
            'status' => $this->faker->boolean(),
75
            'weight' => $this->faker->numberBetween(0, 20)
76
        );
77
78
        return (bool) $this->country->add($data);
79
    }
80
81
    /**
82
     * Returns a random country format
83
     * @return array
84
     */
85
    protected function getFormat()
86
    {
87
        $format = $this->country->getDefaultFormat();
88
        foreach ($format as &$data) {
89
            foreach ($data as $key => &$value) {
90
                if (in_array($key, array('status', 'required'))) {
91
                    $value = (int) $this->faker->boolean();
92
                } else if ($key === 'weight') {
93
                    $value = $this->faker->numberBetween(0, 20);
94
                }
95
            }
96
        }
97
        return $format;
98
    }
99
100
    /**
101
     * Return a random zone ID
102
     * @return integer
103
     */
104
    protected function getZoneId()
105
    {
106
        static $zones = null;
107
108
        if (!isset($zones)) {
109
            $zones = $this->zone->getList(array('limit' => array(0, 300)));
110
        }
111
112
        return (int) array_rand($zones);
113
    }
114
115
}
116