FieldValue   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 2

Importance

Changes 0
Metric Value
wmc 5
lcom 2
cbo 2
dl 0
loc 72
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
A create() 0 15 1
A getFieldId() 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\Field as FieldModel,
13
    gplcart\core\models\FieldValue as FieldValueModel;
14
use gplcart\modules\faker\models\Generator as FakerModuleGenerator;
15
16
/**
17
 * Manages basic behaviors and data related to field values
18
 */
19
class FieldValue extends FakerModuleGenerator
20
{
21
22
    /**
23
     * Field model class instance
24
     * @var \gplcart\core\models\Field $field
25
     */
26
    protected $field;
27
28
    /**
29
     * Field value model class instance
30
     * @var \gplcart\core\models\FieldValue $field_value
31
     */
32
    protected $field_value;
33
34
    /**
35
     * @param FieldModel $field
36
     * @param FieldValueModel $field_value
37
     */
38
    public function __construct(FieldModel $field, FieldValueModel $field_value)
39
    {
40
        parent::__construct();
41
42
        $this->field = $field;
43
        $this->field_value = $field_value;
44
    }
45
46
    /**
47
     * Returns the generator name
48
     * @return string
49
     */
50
    public function getName()
51
    {
52
        return $this->translation->text('Field value');
53
    }
54
55
    /**
56
     * Generate a single field value
57
     * @return bool
58
     */
59
    public function create()
60
    {
61
        $images = $this->getImages();
62
        $image = $images[array_rand($images)];
63
64
        $data = array(
65
            'path' => $image['path'],
66
            'title' => $this->faker->text(50),
67
            'field_id' => $this->getFieldId(),
68
            'color' => $this->faker->hexcolor(),
69
            'weight' => $this->faker->numberBetween(0, 20)
70
        );
71
72
        return (bool) $this->field_value->add($data);
73
    }
74
75
    /**
76
     * Returns a random field ID
77
     * @return integer
78
     */
79
    protected function getFieldId()
80
    {
81
        static $data = null;
82
83
        if (!isset($data)) {
84
            $data = $this->field->getList(array('limit' => array(0, 100)));
85
        }
86
87
        return array_rand($data);
88
    }
89
90
}
91