File::create()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 22
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 22
rs 9.2
c 1
b 0
f 0
cc 3
eloc 14
nc 2
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\modules\faker\models\Generator as FakerModuleGenerator;
13
14
/**
15
 * Manages basic behaviors and data related to files
16
 */
17
class File extends FakerModuleGenerator
18
{
19
20
    /**
21
     * Constructor
22
     */
23
    public function __construct()
24
    {
25
        parent::__construct();
26
    }
27
28
    /**
29
     * Returns the generator name
30
     * @return string
31
     */
32
    public function getName()
33
    {
34
        return $this->translation->text('File');
35
    }
36
37
    /**
38
     * Generate a single file
39
     * @return bool
40
     */
41
    public function create()
42
    {
43
        $directory = GC_DIR_UPLOAD . '/faker';
44
45
        if (!file_exists($directory) && !mkdir($directory, 0775)) {
46
            return false;
47
        }
48
49
        $topics = (array) $this->config->get('module_faker_image_topics', array('abstract', 'technics'));
50
        $image = $this->faker->image($directory, 500, 500, $topics[array_rand($topics)], true);
51
52
        $field = array(
53
            'entity' => '',
54
            'entity_id' => '',
55
            'title' => $this->faker->text(50),
56
            'description' => $this->faker->text(100),
57
            'weight' => $this->faker->numberBetween(0, 20),
58
            'path' => str_replace('\\', '/', gplcart_file_relative($image))
59
        );
60
61
        return (bool) $this->file->add($field);
62
    }
63
64
}
65