Passed
Push — master ( 1908fb...8df30e )
by Rougin
03:01
created

Order::set()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Rougin\Windstorm\Eloquent;
4
5
use Illuminate\Database\Eloquent\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
    protected $key;
17
18
    protected $builder;
19
20
    protected $order = 'asc';
21
22
    protected $query;
23
24 15
    public function __construct(Query $query, Builder $builder, $key)
25
    {
26 15
        $this->query = $query;
27
28 15
        $this->builder = $builder;
29
30 15
        $this->key = $key;
31 15
    }
32
33
    /**
34
     * Sets the order in ascending.
35
     *
36
     * @return \Rougin\Windstorm\QueryInterface
37
     */
38 3
    public function ascending()
39
    {
40 3
        $this->order = 'asc';
41
42 3
        return $this;
43
    }
44
45
    /**
46
     * Sets the order in descending.
47
     *
48
     * @return \Rougin\Windstorm\QueryInterface
49
     */
50 6
    public function descending()
51
    {
52 6
        $this->order = 'desc';
53
54 6
        return $this;
55
    }
56
57
    /**
58
     * Calls a method from a QueryInterface instance.
59
     *
60
     * @param  string $method
61
     * @param  array  $parameters
62
     * @return \Rougin\Windstorm\QueryInterface
63
     */
64 12
    public function __call($method, $parameters)
65
    {
66 12
        return call_user_func_array(array($this->set(), $method), $parameters);
67
    }
68
69
    /**
70
     * Calls a __toString() from a QueryInterface instance.
71
     *
72
     * @return string
73
     */
74 3
    public function __toString()
75
    {
76 3
        return $this->set()->__toString();
77
    }
78
79
    /**
80
     * Sets the query builder instance.
81
     *
82
     * @return \Rougin\Windstorm\QueryInterface
83
     */
84 15
    protected function set()
85
    {
86 15
        $this->builder = $this->builder->orderBy($this->key, $this->order);
87
88 15
        return $this->query->builder($this->builder);
0 ignored issues
show
Bug introduced by
It seems like $this->builder can also be of type Illuminate\Database\Query\Builder; however, parameter $builder of Rougin\Windstorm\Eloquent\Query::builder() does only seem to accept Illuminate\Database\Eloquent\Builder, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

88
        return $this->query->builder(/** @scrutinizer ignore-type */ $this->builder);
Loading history...
89
    }
90
}
91