Product::create()   B
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 32
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 32
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 27
nc 1
nop 0
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\Convertor as ConvertorModel;
13
use gplcart\core\models\Product as ProductModel;
14
use gplcart\core\models\ProductClass as ProductClassModel;
15
use gplcart\modules\faker\models\Generator as FakerModuleGenerator;
16
17
/**
18
 * Manages basic behaviors and data related to products
19
 */
20
class Product extends FakerModuleGenerator
21
{
22
23
    /**
24
     * Product model class instance
25
     * @var \gplcart\core\models\Product $product
26
     */
27
    protected $product;
28
29
    /**
30
     * Product model class instance
31
     * @var \gplcart\core\models\ProductClass $product_class
32
     */
33
    protected $product_class;
34
35
    /**
36
     * Convertor model class instance
37
     * @var \gplcart\core\models\Convertor $convertor
38
     */
39
    protected $convertor;
40
41
    /**
42
     * @param ProductModel $product
43
     * @param ProductClassModel $product_class
44
     * @param ConvertorModel $convertor
45
     */
46
    public function __construct(ProductModel $product, ProductClassModel $product_class, ConvertorModel $convertor)
47
    {
48
        parent::__construct();
49
50
        $this->product = $product;
51
        $this->convertor = $convertor;
52
        $this->product_class = $product_class;
53
    }
54
55
    /**
56
     * Returns the generator name
57
     * @return string
58
     */
59
    public function getName()
60
    {
61
        return $this->translation->text('Product');
62
    }
63
64
    /**
65
     * Generate a single product
66
     * @return bool
67
     */
68
    public function create()
69
    {
70
        $size_units = $this->convertor->getUnitNames('size');
71
        $weight_units = $this->convertor->getUnitNames('weight');
72
73
        $product = array(
74
            'user_id' => $this->getUserId(),
75
            'store_id' => $this->getStoreId(),
76
            'title' => $this->faker->text(100),
77
            'description' => $this->faker->paragraphs(3, true),
78
            'status' => $this->faker->boolean(),
79
            'subtract' => $this->faker->boolean(),
80
            'sku' => strtoupper($this->faker->word()),
81
            'stock' => $this->faker->numberBetween(0, 100),
82
            'price' => $this->faker->numberBetween(1, 1000),
83
            'product_class_id' => $this->getProductClassId(),
84
            'brand_category_id' => $this->getCategoryId('brand'),
85
            'category_id' => $this->getCategoryId('catalog'),
86
            'length' => $this->faker->numberBetween(1, 100),
87
            'width' => $this->faker->numberBetween(1, 100),
88
            'height' => $this->faker->numberBetween(1, 100),
89
            'weight' => $this->faker->numberBetween(1, 100),
90
            'size_unit' => array_rand($size_units),
91
            'weight_unit' => array_rand($weight_units),
92
            'meta_title' => $this->faker->text(60),
93
            'meta_description' => $this->faker->text(160),
94
            'alias' => $this->getAlias(),
95
            'images' => $this->getImages(),
96
        );
97
98
        return (bool) $this->product->add($product);
99
    }
100
101
    /**
102
     * Returns a random product class ID
103
     * @return integer
104
     */
105
    protected function getProductClassId()
106
    {
107
        static $classes = null;
108
109
        if (!isset($classes)) {
110
            $classes = $this->product_class->getList(array('limit' => array(0, 100)));
111
        }
112
113
        return array_rand($classes);
114
    }
115
116
}
117