Factory   A
last analyzed

Complexity

Total Complexity 24

Size/Duplication

Total Lines 143
Duplicated Lines 9.79 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 24
lcom 1
cbo 5
dl 14
loc 143
rs 10
c 0
b 0
f 0

21 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A make() 0 4 1
A getModel() 0 4 1
A __set() 0 4 1
A __get() 0 4 1
A getAttribute() 0 7 2
A setAttributes() 0 4 1
A setAttribute() 0 4 1
A build() 0 5 1
A create() 0 5 1
A newInstance() 0 7 1
A mergeAttributes() 0 7 2
A getOverridesFromClosure() 0 6 1
A buildMany() 7 7 1
A expandAttributesForList() 0 6 1
A extractAttributesForIndex() 0 6 2
A createMany() 7 7 1
A define() 0 8 1
A belongsTo() 0 5 1
A hasMany() 0 5 1
A hasOne() 0 5 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php namespace AdamWathan\Faktory\Factory;
2
3
use AdamWathan\Faktory\Strategy\Build as BuildStrategy;
4
use AdamWathan\Faktory\Strategy\Create as CreateStrategy;
5
use AdamWathan\Faktory\Relationship\BelongsTo;
6
use AdamWathan\Faktory\Relationship\HasMany;
7
use AdamWathan\Faktory\Relationship\HasOne;
8
9
class Factory
10
{
11
    protected $model;
12
    protected $attributes;
13
    protected $factory_repository;
14
    protected $sequence = 1;
15
16
    public function __construct($model, $factory_repository)
17
    {
18
        $this->model = $model;
19
        $this->factory_repository = $factory_repository;
20
    }
21
22
    public static function make($model, $factory_repository)
23
    {
24
        return new static($model, $factory_repository);
25
    }
26
27
    public function getModel()
28
    {
29
        return $this->model;
30
    }
31
32
    public function __set($key, $value)
33
    {
34
        $this->setAttribute($key, $value);
35
    }
36
37
    public function __get($key)
38
    {
39
        return $this->getAttribute($key);
40
    }
41
42
    protected function getAttribute($key)
43
    {
44
        if (is_callable($this->attributes[$key])) {
45
            return $this->attributes[$key]($this, $this->sequence);
46
        }
47
        return $this->attributes[$key];
48
    }
49
50
    public function setAttributes($attributes)
51
    {
52
        $this->attributes = $attributes;
53
    }
54
55
    protected function setAttribute($key, $value)
56
    {
57
        $this->attributes[$key] = $value;
58
    }
59
60
    public function build($override_attributes)
61
    {
62
        $strategy = BuildStrategy::make($this->model, $this->sequence);
63
        return $this->newInstance($strategy, $override_attributes);
64
    }
65
66
    public function create($override_attributes)
67
    {
68
        $strategy = CreateStrategy::make($this->model, $this->sequence);
69
        return $this->newInstance($strategy, $override_attributes);
70
    }
71
72
    protected function newInstance($strategy, $override_attributes)
73
    {
74
        $strategy->attributes($this->mergeAttributes($override_attributes));
75
        $instance = $strategy->newInstance();
76
        $this->sequence++;
77
        return $instance;
78
    }
79
80
    protected function mergeAttributes($override_attributes)
81
    {
82
        if (is_callable($override_attributes)) {
83
            $override_attributes = $this->getOverridesFromClosure($override_attributes);
84
        }
85
        return array_merge($this->attributes, $override_attributes);
86
    }
87
88
    protected function getOverridesFromClosure($closure)
89
    {
90
        $that = clone $this;
91
        $closure($that);
92
        return $that->attributes;
93
    }
94
95 View Code Duplication
    public function buildMany($count, $override_attributes)
96
    {
97
        $override_attributes = $this->expandAttributesForList($override_attributes, $count);
98
        return array_map(function ($i) use ($override_attributes) {
99
            return $this->build($override_attributes[$i]);
100
        }, range(0, $count - 1));
101
    }
102
103
    protected function expandAttributesForList($attributes, $count)
104
    {
105
        return array_map(function ($i) use ($attributes) {
106
            return $this->extractAttributesForIndex($i, $attributes);
107
        }, range(0, $count - 1));
108
    }
109
110
    protected function extractAttributesForIndex($i, $attributes)
111
    {
112
        return array_map(function ($value) use ($i) {
113
            return is_array($value) ? $value[$i] : $value;
114
        }, $attributes);
115
    }
116
117 View Code Duplication
    public function createMany($count, $override_attributes)
118
    {
119
        $override_attributes = $this->expandAttributesForList($override_attributes, $count);
120
        return array_map(function ($i) use ($override_attributes) {
121
            return $this->create($override_attributes[$i]);
122
        }, range(0, $count - 1));
123
    }
124
125
    public function define($name, $definitionCallback)
126
    {
127
        $callback = function ($f) use ($definitionCallback) {
128
            $f->setAttributes($this->attributes);
129
            $definitionCallback($f);
130
        };
131
        $this->factory_repository->define($this->model, $name, $callback);
132
    }
133
134
    public function belongsTo($name, $foreign_key = null, $attributes = [])
135
    {
136
        $factory = $this->factory_repository->getFactory($name);
137
        return new BelongsTo($this->model, $factory, $foreign_key, $attributes);
138
    }
139
140
    public function hasMany($name, $count, $foreign_key = null, $attributes = [])
141
    {
142
        $factory = $this->factory_repository->getFactory($name);
143
        return new HasMany($this->model, $factory, $count, $foreign_key, $attributes);
144
    }
145
146
    public function hasOne($name, $foreign_key = null, $attributes = [])
147
    {
148
        $factory = $this->factory_repository->getFactory($name);
149
        return new HasOne($this->model, $factory, $foreign_key, $attributes);
150
    }
151
}
152