ModelFactory::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Yarak\DB\Factories;
4
5
use Faker\Generator;
6
use Yarak\Config\Config;
7
use Yarak\Helpers\Filesystem;
8
use Phalcon\Mvc\User\Component;
9
use Yarak\Exceptions\FactoryNotFound;
10
11
class ModelFactory extends Component
12
{
13
    use Filesystem;
14
15
    /**
16
     * Faker instance.
17
     *
18
     * @var Generator
19
     */
20
    protected $faker;
21
22
    /**
23
     * Array of user defined factory definitions.
24
     *
25
     * @var array
26
     */
27
    protected $definitions;
28
29
    /**
30
     * Construct.
31
     *
32
     * @param Generator $faker
33
     */
34
    public function __construct(Generator $faker)
35
    {
36
        $this->faker = $faker;
37
38
        $this->load();
39
    }
40
41
    /**
42
     * Make instances of class, overriding default values with attributes.
43
     *
44
     * @param string $class
45
     * @param array  $attributes
46
     * @param int    $times
47
     *
48
     * @return Phalcon\Mvc\Model|array
49
     */
50
    public function make($class, array $attributes = [], $times = null)
51
    {
52
        return $this->forClass($class)->times($times)->make($attributes);
53
    }
54
55
    /**
56
     * Make instances of class using name.
57
     *
58
     * @param string $class
59
     * @param string $name
60
     * @param array  $attributes
61
     * @param int    $times
62
     *
63
     * @return Phalcon\Mvc\Model|array
64
     */
65
    public function makeAs($class, $name, array $attributes = [], $times = null)
66
    {
67
        return $this->forClass($class, $name)->times($times)->make($attributes);
68
    }
69
70
    /**
71
     * Create instances of class, overriding default values with attributes.
72
     *
73
     * @param string $class
74
     * @param array  $attributes
75
     * @param int    $times
76
     *
77
     * @return Phalcon\Mvc\Model|array
78
     */
79
    public function create($class, array $attributes = [], $times = null)
80
    {
81
        return $this->forClass($class)->times($times)->create($attributes);
82
    }
83
84
    /**
85
     * Create instances of class using name.
86
     *
87
     * @param string $class
88
     * @param string $name
89
     * @param array  $attributes
90
     * @param int    $times
91
     *
92
     * @return Phalcon\Mvc\Model|array
93
     */
94
    public function createAs($class, $name, array $attributes = [], $times = null)
95
    {
96
        return $this->forClass($class, $name)->times($times)->create($attributes);
97
    }
98
99
    /**
100
     * Get instance of factory builder with set attributes.
101
     *
102
     * @param string $class
103
     * @param string $name
104
     *
105
     * @return ModelFactoryBuilder
106
     */
107
    public function forClass($class, $name = 'default')
108
    {
109
        return new ModelFactoryBuilder(
110
            $class,
111
            $name,
112
            $this->definitions,
113
            $this->faker
114
        );
115
    }
116
117
    /**
118
     * Load factory definitions onto object.
119
     *
120
     * @return $this
121
     */
122
    protected function load()
123
    {
124
        $factory = $this;
125
126
        $config = Config::getInstance();
127
128
        $path = $config->getFactoryDirectory();
129
130
        $this->makeDirectoryStructure([
131
            $config->getDatabaseDirectory(),
132
            $path,
133
        ]);
134
135
        $this->requireFactories($path, $factory);
136
137
        return $factory;
138
    }
139
140
    /**
141
     * Require all factory files in path.
142
     *
143
     * @param string       $path
144
     * @param ModelFactory $factory
145
     *
146
     * @throws FactoryNotFound
147
     */
148
    protected function requireFactories($path, $factory)
149
    {
150
        $dir = new \DirectoryIterator($path);
151
152
        foreach ($dir as $fileinfo) {
153
            if (!$fileinfo->isDot()) {
154
                require $fileinfo->getRealPath();
155
            }
156
        }
157
158
        if (empty($this->definitions)) {
159
            throw FactoryNotFound::factoryDefinitionNotFound(
160
                'No factory definitions found.'
161
            );
162
        }
163
    }
164
165
    /**
166
     * Define a class with a given set of attributes.
167
     *
168
     * @param string   $class
169
     * @param callable $attributes
170
     * @param string   $name
171
     *
172
     * @return $this
173
     */
174
    public function define($class, callable $attributes, $name = 'default')
175
    {
176
        $this->definitions[$class][$name] = $attributes;
177
178
        return $this;
179
    }
180
}
181