Strategy::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 2
1
<?php namespace AdamWathan\Faktory\Strategy;
2
3
abstract class Strategy
4
{
5
    protected $model;
6
    protected $sequence;
7
    protected $attributes;
8
9
    public function __construct($model, $sequence)
10
    {
11
        $this->model = $model;
12
        $this->sequence = $sequence;
13
    }
14
15
    public static function make($model, $sequence)
16
    {
17
        return new static($model, $sequence);
18
    }
19
20
    public function attributes($attributes)
21
    {
22
        $this->attributes = $attributes;
23
    }
24
25
    protected function newModel()
26
    {
27
        return new $this->model;
28
    }
29
30
    protected function setAttribute($attribute, $value)
31
    {
32
        $this->attributes[$attribute] = $value;
33
    }
34
35
    protected function unsetAttribute($attribute)
36
    {
37
        unset($this->attributes[$attribute]);
38
    }
39
40
    public function __get($key)
41
    {
42
        return $this->getAttributeValue($this->attributes[$key]);
43
    }
44
45
    protected function getAttributeValue($value)
46
    {
47
        if (is_callable($value)) {
48
            return $value($this, $this->sequence);
49
        }
50
        return $value;
51
    }
52
53
    abstract public function newInstance();
54
}
55