Completed
Push — master ( b70263...7f186a )
by recca
01:50
created

Compiler   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 108
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 1
dl 0
loc 108
ccs 32
cts 32
cp 1
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B push() 0 16 5
A apply() 0 20 4
A defaults() 0 6 1
A isExpression() 0 4 1
compileParameters() 0 1 ?
1
<?php
2
3
namespace Recca0120\Repository\Compilers;
4
5
use Recca0120\Repository\Criteria;
6
use Recca0120\Repository\Expression;
7
8
abstract class Compiler
9
{
10
    /**
11
     * $items.
12
     *
13
     * @var array
14
     */
15
    protected $items = [];
16
17
    /**
18
     * $model.
19
     *
20
     * @var mixed
21
     */
22
    protected $model;
23
24
    /**
25
     * __construct.
26
     *
27
     * @param \Illuminate\Database\Eloquent\Model $model
28
     */
29 39
    public function __construct($model)
30
    {
31 39
        $this->model = $model;
32 39
    }
33
34
    /**
35
     * push.
36
     *
37
     * @param \Recca0120\Repository\Criteria $criteria
38
     * @return $this
39
     */
40 39
    public function push($criteria)
41
    {
42 39
        $items = is_array($criteria) ? $criteria : [$criteria];
43
44 39
        foreach ($items as $key => $value) {
45 22
            if (($value instanceof Criteria) === true) {
46 20
                $this->items[] = $value;
47 20
            } else {
48 4
                $value = is_array($value) ? $value : [$key, $value];
49 4
                $criteria = call_user_func_array([Criteria::create(), 'where'], $value);
50 4
                $this->items[] = $criteria;
51
            }
52 39
        }
53
54 39
        return $this;
55
    }
56
57
    /**
58
     * apply.
59
     *
60
     * @return mixed
61
     */
62 39
    public function apply()
63
    {
64
        $allowTypes = array_reduce($this->items, function($allows, $criteria) {
65 22
            foreach ($criteria->all() as $action) {
66 22
                $allows[$action->type][] = [
67 22
                    'method' => $action->method,
68 22
                    'parameters' => $this->compileParameters($action->parameters),
69
                ];
70 22
            }
71
72 22
            return $allows;
73 39
        }, []);
74
75 39
        foreach ($allowTypes as $type => $actions) {
76 22
            $method = method_exists($this, $type) === true ? $type : 'defaults';
77 22
            $this->model = call_user_func_array([$this, $method], [$this->model, $actions]);
78 38
        }
79
80 38
        return $this->model;
81
    }
82
83
    /**
84
     * defaults apply.
85
     *
86
     * @param mixed $model
87
     * @param array $actions
88
     * @return mixed
89
     */
90
    public function defaults($model, $actions)
91
    {
92 13
        return array_reduce($actions, function($model, $action) {
93 13
            return call_user_func_array([$model, $action['method']], $action['parameters']);
94 13
        }, $model);
95
    }
96
97
    /**
98
     * isExpression.
99
     *
100
     * @param mixed$param
101
     * @return bool
102
     */
103 13
    protected function isExpression($param)
104
    {
105 13
        return $param instanceof Expression;
106
    }
107
108
    /**
109
     * transformParameters.
110
     *
111
     * @param array $parameters
112
     * @return array
113
     */
114
    abstract protected function compileParameters($parameters);
115
}
116