1 | <?php |
||
19 | abstract class Generator |
||
20 | { |
||
21 | |||
22 | /** |
||
23 | * Config class instance |
||
24 | * @var \gplcart\core\Config $config |
||
25 | */ |
||
26 | protected $config; |
||
27 | |||
28 | /** |
||
29 | * Library class instance |
||
30 | * @var \gplcart\core\Library |
||
31 | */ |
||
32 | protected $library; |
||
33 | |||
34 | /** |
||
35 | * Faker class instance |
||
36 | * @var \Faker\Generator $faker |
||
37 | */ |
||
38 | protected $faker; |
||
39 | |||
40 | /** |
||
41 | * File model class instance |
||
42 | * @var \gplcart\core\models\File $file |
||
43 | */ |
||
44 | protected $file; |
||
45 | |||
46 | /** |
||
47 | * Store model class instance |
||
48 | * @var \gplcart\core\models\Store $store |
||
49 | */ |
||
50 | protected $store; |
||
51 | |||
52 | /** |
||
53 | * User model class instance |
||
54 | * @var \gplcart\core\models\User $user |
||
55 | */ |
||
56 | protected $user; |
||
57 | |||
58 | /** |
||
59 | * Category model class instance |
||
60 | * @var \gplcart\core\models\Category $category |
||
61 | */ |
||
62 | protected $category; |
||
63 | |||
64 | /** |
||
65 | * Alias model class instance |
||
66 | * @var \gplcart\core\models\Alias $alias |
||
67 | */ |
||
68 | protected $alias; |
||
69 | |||
70 | /** |
||
71 | * Translation UI model instance |
||
72 | * @var \gplcart\core\models\Translation $translation |
||
73 | */ |
||
74 | protected $translation; |
||
75 | |||
76 | /** |
||
77 | * Constructor |
||
78 | */ |
||
79 | public function __construct() |
||
80 | { |
||
81 | $this->config = Container::get('gplcart\\core\\Config'); |
||
82 | $this->library = Container::get('gplcart\\core\\Library'); |
||
83 | $this->user = Container::get('gplcart\\core\\models\\User'); |
||
84 | $this->file = Container::get('gplcart\\core\\models\\File'); |
||
85 | $this->store = Container::get('gplcart\\core\\models\\Store'); |
||
86 | $this->alias = Container::get('gplcart\\core\\models\\Alias'); |
||
87 | $this->category = Container::get('gplcart\\core\\models\\Category'); |
||
88 | $this->translation = Container::get('gplcart\\core\\models\\Translation'); |
||
89 | |||
90 | $this->setFakerInstance(); |
||
91 | } |
||
92 | |||
93 | /** |
||
94 | * Create a single entity |
||
95 | * Require generators to have this method |
||
96 | */ |
||
97 | abstract public function create(); |
||
98 | |||
99 | /** |
||
100 | * Returns a generator name |
||
101 | * Require generators to have this method |
||
102 | */ |
||
103 | abstract public function getName(); |
||
104 | |||
105 | /** |
||
106 | * Sets a property |
||
107 | * @param string $name |
||
108 | * @param mixed $value |
||
109 | */ |
||
110 | public function setProperty($name, $value) |
||
111 | { |
||
112 | $this->{$name} = $value; |
||
113 | } |
||
114 | |||
115 | /** |
||
116 | * Set Faker class instance |
||
117 | * @return \Faker\Generator |
||
118 | * @throws LogicException |
||
119 | */ |
||
120 | protected function setFakerInstance() |
||
121 | { |
||
122 | $this->library->load('faker'); |
||
123 | |||
124 | if (!class_exists('Faker\\Factory')) { |
||
125 | throw new LogicException('Class "Faker\Factory" not found'); |
||
126 | } |
||
127 | |||
128 | return $this->faker = \Faker\Factory::create(); |
||
129 | } |
||
130 | |||
131 | /** |
||
132 | * Generate a given number of entities |
||
133 | * @param integer $limit |
||
134 | * @return integer |
||
135 | */ |
||
136 | public function generate($limit = 20) |
||
137 | { |
||
138 | ini_set('memory_limit', -1); |
||
139 | |||
140 | $created = 0; |
||
141 | for ($i = 0; $i < $limit; $i++) { |
||
142 | $created += (int) $this->create(); |
||
143 | } |
||
144 | |||
145 | return $created; |
||
146 | } |
||
147 | |||
148 | /** |
||
149 | * Returns a random array of images from the database |
||
150 | * @param bool $return_paths |
||
151 | * @return array |
||
152 | */ |
||
153 | protected function getImages($return_paths = true) |
||
154 | { |
||
155 | static $files = null; |
||
156 | |||
157 | if (!isset($files)) { |
||
158 | $files = $this->file->getList(array('file_type' => 'image', 'limit' => array(0, 300))); |
||
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() |
||
194 | |||
195 | /** |
||
196 | * Returns a random user ID |
||
197 | * @return integer |
||
198 | */ |
||
199 | protected function getUserId() |
||
209 | |||
210 | /** |
||
211 | * Returns a random category ID |
||
212 | * @param string $type |
||
213 | * @return integer |
||
214 | */ |
||
215 | protected function getCategoryId($type) |
||
231 | |||
232 | /** |
||
233 | * Returns a unique URL alias |
||
234 | * @return string |
||
235 | */ |
||
236 | protected function getAlias() |
||
240 | |||
241 | /** |
||
242 | * Returns an array of available generator models |
||
243 | * This method is static because we cannot instantiate abstract classes |
||
244 | * @return array |
||
245 | */ |
||
246 | public static function getList() |
||
267 | |||
268 | /** |
||
269 | * Returns a generator model instance |
||
270 | * @param string $name |
||
271 | * @return object|null |
||
272 | */ |
||
273 | public static function get($name) |
||
278 | |||
279 | } |
||
280 |