Completed
Push — master ( 3c83e3...f3bc99 )
by recca
04:01
created

Compiler   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 124
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 14
c 0
b 0
f 0
lcom 1
cbo 1
dl 0
loc 124
ccs 34
cts 34
cp 1
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A push() 0 10 4
A apply() 0 9 3
A defaults() 0 6 1
A isExpression() 0 4 1
A groupByType() 0 13 2
A convertToCriteria() 0 7 2
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 39
        foreach ($items as $key => $value) {
44 22
            $this->items[] = $value instanceof Criteria ?
45 22
                $value : $this->convertToCriteria($value, $key);
46 39
        }
47
48 39
        return $this;
49
    }
50
51
    /**
52
     * apply.
53
     *
54
     * @return mixed
55
     */
56 39
    public function apply()
57
    {
58 39
        foreach ($this->groupByType() as $type => $actions) {
59 22
            $method = method_exists($this, $type) === true ? $type : 'defaults';
60 22
            $this->model = call_user_func_array([$this, $method], [$this->model, $actions]);
61 38
        }
62
63 38
        return $this->model;
64
    }
65
66
    /**
67
     * defaults apply.
68
     *
69
     * @param mixed $model
70
     * @param array $actions
71
     * @return mixed
72
     */
73 13
    public function defaults($model, $actions)
74
    {
75
        return array_reduce($actions, function ($model, $action) {
76 13
            return call_user_func_array([$model, $action['method']], $action['parameters']);
77 13
        }, $model);
78
    }
79
80
    /**
81
     * isExpression.
82
     *
83
     * @param mixed$param
84
     * @return bool
85
     */
86 13
    protected function isExpression($param)
87
    {
88 13
        return $param instanceof Expression;
89
    }
90
91
    /**
92
     * groupByType.
93
     *
94
     * @return array
95
     */
96
    protected function groupByType()
97
    {
98 39
        return array_reduce($this->items, function ($allows, $criteria) {
99 22
            foreach ($criteria->all() as $action) {
100 22
                $allows[$action->type][] = [
101 22
                    'method' => $action->method,
102 22
                    'parameters' => $this->compileParameters($action->parameters),
103
                ];
104 22
            }
105
106 22
            return $allows;
107 39
        }, []);
108
    }
109
110
    /**
111
     * convertToCriteria.
112
     *
113
     * @param array $value
114
     * @return \Recca0120\Repository\Criteria
115
     */
116 4
    protected function convertToCriteria($value, $key)
117
    {
118 4
        return call_user_func_array(
119 4
            [Criteria::create(), 'where'],
120 4
            is_array($value) ? $value : [$key, $value]
121 4
        );
122
    }
123
124
    /**
125
     * transformParameters.
126
     *
127
     * @param array $parameters
128
     * @return array
129
     */
130
    abstract protected function compileParameters($parameters);
131
}
132