Completed
Push — master ( 94e237...a912de )
by Iurii
01:07
created

Generator::setProperty()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 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;
11
12
use gplcart\core\Container;
13
14
/**
15
 * Base class for faker generators
16
 */
17
abstract class Generator
18
{
19
20
    /**
21
     * Config class instance
22
     * @var \gplcart\core\Config $config
23
     */
24
    protected $config;
25
26
    /**
27
     * Library class instance
28
     * @var \gplcart\core\Library
29
     */
30
    protected $library;
31
32
    /**
33
     * Faker class instance
34
     * @var \Faker\Generator $faker
35
     */
36
    protected $faker;
37
38
    /**
39
     * File model class instance
40
     * @var \gplcart\core\models\File $file
41
     */
42
    protected $file;
43
44
    /**
45
     * Store model class instance
46
     * @var \gplcart\core\models\Store $store
47
     */
48
    protected $store;
49
50
    /**
51
     * User model class instance
52
     * @var \gplcart\core\models\User $user
53
     */
54
    protected $user;
55
56
    /**
57
     * Category model class instance
58
     * @var \gplcart\core\models\Category $category
59
     */
60
    protected $category;
61
62
    /**
63
     * Alias model class instance
64
     * @var \gplcart\core\models\Alias $alias
65
     */
66
    protected $alias;
67
68
    /**
69
     * Translation UI model instance
70
     * @var \gplcart\core\models\Translation $translation
71
     */
72
    protected $translation;
73
74
    /**
75
     * Constructor
76
     */
77
    public function __construct()
78
    {
79
        $this->config = Container::get('gplcart\\core\\Config');
80
        $this->library = Container::get('gplcart\\core\\Library');
81
        $this->user = Container::get('gplcart\\core\\models\\User');
82
        $this->file = Container::get('gplcart\\core\\models\\File');
83
        $this->store = Container::get('gplcart\\core\\models\\Store');
84
        $this->alias = Container::get('gplcart\\core\\models\\Alias');
85
        $this->category = Container::get('gplcart\\core\\models\\Category');
86
        $this->translation = Container::get('gplcart\\core\\models\\Translation');
87
88
        $this->setFakerInstance();
89
    }
90
91
    /**
92
     * Sets a property
93
     * @param string $name
94
     * @param mixed $value
95
     */
96
    public function setProperty($name, $value)
97
    {
98
        $this->{$name} = $value;
99
    }
100
101
    /**
102
     * Create a single entity
103
     * Require generators to have this method
104
     */
105
    abstract protected function create();
106
107
    /**
108
     * Returns a generator name
109
     * Require generators to have this method
110
     */
111
    abstract protected function getName();
112
113
    /**
114
     * Set Faker class instance
115
     * @return \Faker\Generator
116
     * @throws \RuntimeException
117
     */
118
    protected function setFakerInstance()
119
    {
120
        $this->library->load('faker');
121
122
        if (class_exists('Faker\\Factory')) {
123
            return $this->faker = \Faker\Factory::create();
124
        }
125
        throw new \InvalidArgumentException('Class "Faker\Factory" not found');
126
    }
127
128
    /**
129
     * Generate a given number of entities
130
     * @param integer $limit
131
     * @return integer
132
     */
133
    public function generate($limit = 20)
134
    {
135
        ini_set('memory_limit', -1);
136
137
        $created = 0;
138
        for ($i = 0; $i < $limit; $i++) {
139
            $created += (int) $this->create();
140
        }
141
142
        return $created;
143
    }
144
145
    /**
146
     * Returns a random array of images from the database
147
     * @param bool $return_paths
148
     * @return array
149
     */
150
    protected function getImages($return_paths = true)
151
    {
152
        static $files = null;
153
154
        if (!isset($files)) {
155
            $files = $this->file->getList(array(
156
                'file_type' => 'image',
157
                'limit' => array(0, 300)
158
            ));
159
        }
160
161
        if (empty($files)) {
162
            return array();
163
        }
164
165
        $limit = $this->config->get('module_faker_entity_file_limit', 5);
166
        $images = array_slice($this->faker->shuffle($files), 0, $limit, true);
167
168
        if (!$return_paths) {
169
            return array_keys($images);
170
        }
171
172
        $paths = array();
173
        foreach ($images as $image) {
174
            $paths[] = array('path' => $image['path']);
175
        }
176
177
        return $paths;
178
    }
179
180
    /**
181
     * Returns a random store ID
182
     * @return integer
183
     */
184
    protected function getStoreId()
185
    {
186
        static $stores = null;
187
188
        if (!isset($stores)) {
189
            $stores = $this->store->getList(array('limit' => array(0, 100)));
190
        }
191
192
        return (int) array_rand($stores);
193
    }
194
195
    /**
196
     * Returns a random user ID
197
     * @return integer
198
     */
199
    protected function getUserId()
200
    {
201
        static $users = null;
202
203
        if (!isset($users)) {
204
            $users = $this->user->getList(array('limit' => array(0, 100)));
205
        }
206
207
        return (int) array_rand($users);
208
    }
209
210
    /**
211
     * Returns a random category ID
212
     * @param string $type
213
     * @return integer
214
     */
215
    protected function getCategoryId($type)
216
    {
217
        static $categories = array();
218
219
        if (!isset($categories[$type])) {
220
            $options = array('limit' => array(0, 100), 'type' => $type);
221
            $categories[$type] = $this->category->getList($options);
222
        }
223
224
        return (int) array_rand($categories[$type]);
225
    }
226
227
    /**
228
     * Returns a unique URL alias
229
     * @return string
230
     */
231
    protected function getAlias()
232
    {
233
        return $this->alias->getUnique($this->faker->slug());
234
    }
235
236
    /**
237
     * Returns an array of available generator models
238
     * This method is static because we cannot instantiate abstract classes
239
     * @return array
240
     */
241
    public static function getList()
242
    {
243
        $generators = array();
244
        foreach (glob(__DIR__ . "/generators/*.php") as $file) {
245
            $id = strtolower(pathinfo($file, PATHINFO_FILENAME));
246
247
            try {
248
                $instance = Container::get("gplcart\\modules\\faker\\models\\generators\\$id");
249
            } catch (\Exception $ex) {
250
                continue;
251
            }
252
253
            if ($instance instanceof \gplcart\modules\faker\models\Generator) {
254
                $generators[$id] = $instance;
255
            }
256
        }
257
258
        ksort($generators);
259
        return $generators;
260
    }
261
262
    /**
263
     * Returns a generator model instance
264
     * @param string $name
265
     * @return object|null
266
     */
267
    public static function get($name)
268
    {
269
        $generators = static::getList();
270
        if (empty($generators[$name])) {
271
            return null;
272
        }
273
        return $generators[$name];
274
    }
275
276
}
277