Passed
Push — master ( 4e0852...046d68 )
by Adrian
02:13
created

Query::reference()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 3
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 7
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A Query::getStatement() 0 5 1
1
<?php
2
declare(strict_types=1);
3
4
namespace Sirius\Orm;
5
6
use Sirius\Orm\Collection\Collection;
7
use Sirius\Orm\Collection\PaginatedCollection;
8
use Sirius\Sql\Select;
9
10
class Query extends Select
11
{
12
13
    /**
14
     * @var Orm
15
     */
16
    protected $orm;
17
18
    /**
19
     * @var Mapper
20
     */
21
    protected $mapper;
22
23
    /**
24
     * @var array
25
     */
26
    protected $load = [];
27
28
    /**
29
     * @var array
30
     */
31
    protected $guards = [];
32
33
    /**
34
     * @var array
35
     */
36
    protected $scopes = [];
37
38
    public function __construct(Mapper $mapper)
39
    {
40
        parent::__construct($mapper->getReadConnection());
41
        $this->mapper = $mapper;
42
        $this->from($this->mapper->getTableReference());
43
        $this->resetColumns();
44
        $this->columns($this->mapper->getTableAlias(true) . '.*');
45
    }
46
47
    public function __call(string $method, array $params)
48
    {
49
        $scope = $this->mapper->getScope($method);
50
        if ($scope && is_callable($scope)) {
51
            return $scope($this, ...$params);
52
        }
53
54
        return parent::__call($method, $params);
55
    }
56
57
58
    public function load(...$relations): self
59
    {
60
        foreach ($relations as $name => $callback) {
61
            if (is_int($name)) {
62
                $this->load[$callback] = null;
63
            } elseif (is_callable($callback)) {
64
                $this->load[$name] = $callback;
65
            } else {
66
                throw new \InvalidArgumentException('Invalid callable for relation');
67
            }
68
        }
69
70
        return $this;
71
    }
72
73
    public function first()
74
    {
75
        $row = $this->fetchOne();
76
77
        return $this->mapper->newEntityFromRow($row, $this->load);
78
    }
79
80
    public function get(): Collection
81
    {
82
        return $this->mapper->newCollectionFromRows($this->fetchAll(), $this->load);
83
    }
84
85
    public function paginate($perPage, $page): PaginatedCollection
86
    {
87
        /** @var Query $countQuery */
88
        $countQuery = clone $this;
89
        $total      = $countQuery->count();
90
91
        if ($total == 0) {
92
            $this->mapper->newPaginatedCollectionFromRows([], $total, $perPage, $page, $this->load);
93
        }
94
95
        $this->perPage($perPage);
96
        $this->page($page);
97
98
        return $this->mapper->newPaginatedCollectionFromRows($this->fetchAll(), $total, $perPage, $page, $this->load);
99
    }
100
101
    public function chunk($count, $callback)
0 ignored issues
show
Unused Code introduced by
The parameter $count is not used and could be removed. ( Ignorable by Annotation )

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

101
    public function chunk(/** @scrutinizer ignore-unused */ $count, $callback)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $callback is not used and could be removed. ( Ignorable by Annotation )

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

101
    public function chunk($count, /** @scrutinizer ignore-unused */ $callback)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
102
    {
103
    }
104
105
    public function count()
106
    {
107
        $this->resetOrderBy();
108
        $this->resetColumns();
109
        $this->columns('COUNT(*)');
110
111
        return (int)$this->fetchValue();
112
    }
113
114
    public function setGuards(array $guards)
115
    {
116
        foreach ($guards as $column => $value) {
117
            if (is_int($column)) {
118
                $this->guards[] = $value;
119
            } else {
120
                $this->guards[$column] = $value;
121
            }
122
        }
123
124
        return $this;
125
    }
126
127
    public function resetGuards()
128
    {
129
        $this->guards = [];
130
131
        return;
132
    }
133
134
    protected function applyGuards()
135
    {
136
        if (empty($this->guards)) {
137
            return;
138
        }
139
140
        $this->groupCurrentWhere();
141
        foreach ($this->guards as $column => $value) {
142
            if (is_int($column)) {
143
                $this->where($value);
144
            } else {
145
                $this->where($column, $value);
146
            }
147
        }
148
    }
149
150
    public function getStatement(): string
151
    {
152
        $this->applyGuards();
153
154
        return parent::getStatement();
155
    }
156
}
157