ProductClass::__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\Field as FieldModel;
13
use gplcart\core\models\ProductClass as ProductClassModel;
14
use gplcart\core\models\ProductClassField as ProductClassFieldModel;
15
use gplcart\modules\faker\models\Generator as FakerModuleGenerator;
16
17
/**
18
 * Manages basic behaviors and data related to product classes
19
 */
20
class ProductClass extends FakerModuleGenerator
21
{
22
23
    /**
24
     * Field model instance
25
     * @var \gplcart\core\models\Field $field
26
     */
27
    protected $field;
28
29
    /**
30
     * Product class model instance
31
     * @var \gplcart\core\models\ProductClass $product_class
32
     */
33
    protected $product_class;
34
35
    /**
36
     * Product class field model instance
37
     * @var \gplcart\core\models\ProductClassField $product_class_field
38
     */
39
    protected $product_class_field;
40
41
    /**
42
     * @param ProductClassModel $product_class
43
     * @param ProductClassFieldModel $product_class_field
44
     * @param FieldModel $field
45
     */
46
    public function __construct(ProductClassModel $product_class, ProductClassFieldModel $product_class_field, FieldModel $field)
47
    {
48
        parent::__construct();
49
50
        $this->field = $field;
51
        $this->product_class = $product_class;
52
        $this->product_class_field = $product_class_field;
53
    }
54
55
    /**
56
     * Returns the generator name
57
     * @return string
58
     */
59
    public function getName()
60
    {
61
        return $this->translation->text('Product class');
62
    }
63
64
    /**
65
     * Generate a single product class
66
     * @return bool
67
     */
68
    public function create()
69
    {
70
        $data = array(
71
            'title' => $this->faker->text(50),
72
            'status' => $this->faker->boolean()
73
        );
74
75
        $product_class_id = $this->product_class->add($data);
76
77
        if (empty($product_class_id)) {
78
            return false;
79
        }
80
81
        foreach ($this->getFields() as $field_id) {
82
83
            $field = array(
84
                'field_id' => $field_id,
85
                'required' => $this->faker->boolean(),
86
                'multiple' => $this->faker->boolean(),
87
                'product_class_id' => $product_class_id,
88
                'weight' => $this->faker->numberBetween(0, 20)
89
            );
90
91
            $this->product_class_field->add($field);
92
        }
93
94
        return true;
95
    }
96
97
    /**
98
     * Returns a random array of field IDs
99
     * @return array
100
     */
101
    protected function getFields()
102
    {
103
        static $fields = null;
104
105
        if (!isset($fields)) {
106
            $fields = $this->field->getList(array('limit' => array(0, 300)));
107
        }
108
109
        if (empty($fields)) {
110
            return array();
111
        }
112
113
        $limit = $this->config->get('module_faker_product_class_field_limit', 20);
114
        $chunk = array_slice($this->faker->shuffle($fields), 0, $limit, true);
115
        return array_keys($chunk);
116
    }
117
118
}
119