Passed
Push — master ( 9db5ce...9e9229 )
by Rougin
01:41
created

Order::set()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 9
ccs 5
cts 5
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
crap 2
1
<?php
2
3
namespace Rougin\Windstorm\Doctrine;
4
5
use Rougin\Windstorm\Doctrine\Builder;
6
use Rougin\Windstorm\OrderInterface;
7
8
/**
9
 * Order Query
10
 *
11
 * @package Windstorm
12
 * @author  Rougin Gutib <[email protected]>
13
 */
14
class Order implements OrderInterface
15
{
16
    /**
17
     * @var \Rougin\Windstorm\Doctrine\Builder
18
     */
19
    protected $builder;
20
21
    /**
22
     * @var string
23
     */
24
    protected $key;
25
26
    /**
27
     * @var string
28
     */
29
    protected $order = 'ASC';
30
31
    /**
32
     * @var \Rougin\Windstorm\Doctrine\Query
33
     */
34
    protected $query;
35
36
    /**
37
     * @var string
38
     */
39
    protected $type = '';
40
41
    /**
42
     * Initializes the query instance.
43
     *
44
     * @param \Rougin\Windstorm\Doctrine\Query   $query
45
     * @param \Rougin\Windstorm\Doctrine\Builder $builder
46
     * @param string                             $key
47
     * @param string                             $initial
48
     * @param string                             $type
49
     */
50 15
    public function __construct(Query $query, Builder $builder, $key, $initial, $type = '')
51
    {
52 15
        if (strpos($key, '.') === false)
53 10
        {
54 15
            $key = $initial . '.' . $key;
55 10
        }
56
57 15
        $this->builder = $builder;
58
59 15
        $this->key = $key;
60
61 15
        $this->query = $query;
62
63 15
        $this->type = $type;
64 15
    }
65
66
    /**
67
     * Sets the order in ascending.
68
     *
69
     * @return \Rougin\Windstorm\QueryInterface
70
     */
71 3
    public function ascending()
72
    {
73 3
        $this->order = 'ASC';
74
75 3
        return $this;
76
    }
77
78
    /**
79
     * Sets the order in descending.
80
     *
81
     * @return \Rougin\Windstorm\QueryInterface
82
     */
83 6
    public function descending()
84
    {
85 6
        $this->order = 'DESC';
86
87 6
        return $this;
88
    }
89
90
    /**
91
     * Calls a method from a QueryInterface instance.
92
     *
93
     * @param  string $method
94
     * @param  array  $parameters
95
     * @return mixed
96
     */
97 6
    public function __call($method, $parameters)
98
    {
99 6
        $instance = array($this->set(), (string) $method);
100
101 6
        return call_user_func_array($instance, $parameters);
102
    }
103
104
    /**
105
     * Calls a __toString() from a QueryInterface instance.
106
     *
107
     * @return string
108
     */
109 12
    public function __toString()
110
    {
111 12
        return $this->set()->__toString();
112
    }
113
114
    /**
115
     * Sets the order type and calls a method from QueryInstance.
116
     *
117
     * @return \Rougin\Windstorm\QueryInterface
118
     */
119 15
    protected function set()
120
    {
121 15
        $type = strtolower($this->type) . 'OrderBy';
122
123 15
        $this->builder->$type($this->key, $this->order);
124
125 15
        $type === 'OrderBy' && $type = 'orderBy';
0 ignored issues
show
Unused Code introduced by
The assignment to $type is dead and can be removed.
Loading history...
126
127 15
        return $this->query->builder($this->builder);
128
    }
129
}
130