Completed
Push — master ( b3febe...348067 )
by Zach
04:06 queued 02:03
created

ModelFactoryBuilder::make()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 7
nc 3
nop 1
dl 0
loc 14
rs 9.2
c 0
b 0
f 0
1
<?php
2
3
namespace Yarak\DB;
4
5
use Faker\Generator;
6
use Phalcon\Mvc\Model;
7
8
class ModelFactoryBuilder
9
{
10
    /**
11
     * Name of class.
12
     *
13
     * @var string
14
     */
15
    protected $class;
16
17
    /**
18
     * Definition name.
19
     *
20
     * @var string
21
     */
22
    protected $name;
23
24
    /**
25
     * Array of all definitions.
26
     *
27
     * @var array
28
     */
29
    protected $definitions;
30
31
    /**
32
     * Faker instance.
33
     *
34
     * @var Generator
35
     */
36
    protected $faker;
37
38
    /**
39
     * Number of times to run factory.
40
     *
41
     * @var int|null
42
     */
43
    protected $times;
44
45
    /**
46
     * Construct.
47
     *
48
     * @param string    $class
49
     * @param string    $name
50
     * @param array     $definitions
51
     * @param Generator $faker
52
     */
53
    public function __construct($class, $name, array $definitions, Generator $faker)
54
    {
55
        $this->class = $class;
56
        $this->name = $name;
57
        $this->definitions = $definitions;
58
        $this->faker = $faker;
59
    }
60
61
    /**
62
     * Make an instance of class.
63
     *
64
     * @param array $attributes
65
     *
66
     * @return Phalcon\Mvc\Model|array
67
     */
68
    public function make(array $attributes = [])
69
    {
70
        if ($this->times === null || $this->times < 1) {
71
            return $this->makeInstance($attributes);
72
        }
73
74
        $instances = [];
75
76
        foreach (range(1, $this->times) as $time) {
77
            $instances[] = $this->makeInstance($attributes);
78
        }
79
80
        return $instances;
81
    }
82
83
    /**
84
     * Create an instance of class and persist in database.
85
     *
86
     * @param array $attributes
87
     *
88
     * @return Phalcon\Mvc\Model|array
89
     */
90
    public function create(array $attributes = [])
91
    {
92
        $made = $this->make($attributes);
93
94
        if ($made instanceof Model) {
1 ignored issue
show
Bug introduced by
The class Phalcon\Mvc\Model does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
95
            $made->save();
96
        }
97
98
        if (is_array($made)) {
99
            return array_map(function (Model $model) {
100
                $model->save();
101
102
                return $model;
103
            }, $made);
104
        }
105
106
        return $made;
107
    }
108
109
    /**
110
     * Set the number of times to run the make/create the class.
111
     *
112
     * @param int|null $times
113
     *
114
     * @return this
115
     */
116
    public function times($times = null)
117
    {
118
        $this->times = $times;
119
120
        return $this;
121
    }
122
123
    /**
124
     * Make an instance of the class with the given attributes.
125
     *
126
     * @param array $attributes
127
     *
128
     * @return Phalcon\Mvc\Model
129
     */
130
    protected function makeInstance(array $attributes = [])
131
    {
132
        if (!isset($this->definitions[$this->class][$this->name])) {
133
            throw new \Exception(
134
                "Definition with name {$this->name} does not exist."
135
            );
136
        }
137
138
        $fakerAttributes = call_user_func(
139
            $this->definitions[$this->class][$this->name],
140
            $this->faker
141
        );
142
143
        $finalAttributes = array_merge($fakerAttributes, $attributes);
144
145
        return new $this->class($finalAttributes);
146
    }
147
}
148