Repository::findBy()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 8
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 5
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace TZachi\PhalconRepository;
6
7
use Phalcon\Mvc\Model;
8
use Phalcon\Mvc\Model\Criteria;
9
use Phalcon\Mvc\Model\Resultset\Simple as SimpleResultset;
10
use TZachi\PhalconRepository\Resolver\Parameter as ParameterResolver;
11
12
class Repository
13
{
14
    /**
15
     * @var ModelWrapper
16
     */
17
    protected $modelWrapper;
18
19
    /**
20
     * @var ParameterResolver
21
     */
22
    protected $parameterResolver;
23
24
    /**
25
     * @var string
26
     */
27
    protected $idField;
28
29 27
    public function __construct(
30
        ModelWrapper $modelWrapper,
31
        ParameterResolver $parameterResolver,
32
        string $idField = 'id'
33
    ) {
34 27
        $this->modelWrapper      = $modelWrapper;
35 27
        $this->parameterResolver = $parameterResolver;
36 27
        $this->idField           = $idField;
37 27
    }
38
39
    /**
40
     * Returns the first record matching the specified conditions
41
     *
42
     * @param mixed[]  $where   Where condition
43
     * @param string[] $orderBy One or more order by statements
44
     */
45 2
    public function findFirstWhere(array $where = [], array $orderBy = []): ?Model
46
    {
47 2
        $parameters = $this->parameterResolver->where($where) + $this->parameterResolver->orderBy($orderBy);
48
49 2
        $model = $this->modelWrapper->findFirst($parameters);
50 2
        if ($model === false) {
51 1
            return null;
52
        }
53
54 1
        return $model;
55
    }
56
57
    /**
58
     * Returns the first record that matches a single condition
59
     *
60
     * @param mixed    $value
61
     * @param string[] $orderBy One or more order by statements
62
     */
63 2
    public function findFirstBy(string $field, $value, array $orderBy = []): ?Model
64
    {
65 2
        return $this->findFirstWhere([$field => $value], $orderBy);
66
    }
67
68
    /**
69
     * Returns the first record that matches a primary key
70
     *
71
     * @param mixed $id
72
     */
73 3
    public function findFirst($id): ?Model
74
    {
75 3
        return $this->findFirstBy($this->idField, $id);
76
    }
77
78
    /**
79
     * Finds all records matching the specified conditions
80
     *
81
     * @param mixed[]  $where
82
     * @param string[] $orderBy
83
     */
84 2
    public function findWhere(array $where = [], array $orderBy = [], int $limit = 0, int $offset = 0): SimpleResultset
85
    {
86 2
        $parameters = $this->parameterResolver->where($where)
87 2
            + $this->parameterResolver->orderBy($orderBy)
88 2
            + $this->parameterResolver->limit($limit, $offset);
89
90 2
        return $this->modelWrapper->find($parameters);
91
    }
92
93
    /**
94
     * Finds all records that match a single condition
95
     *
96
     * @param mixed    $value
97
     * @param string[] $orderBy
98
     */
99 2
    public function findBy(
100
        string $field,
101
        $value,
102
        array $orderBy = [],
103
        int $limit = 0,
104
        int $offset = 0
105
    ): SimpleResultset {
106 2
        return $this->findWhere([$field => $value], $orderBy, $limit, $offset);
107
    }
108
109
    /**
110
     * Returns a query builder (Criteria) to create custom queries
111
     */
112 1
    public function query(): Criteria
113
    {
114 1
        return $this->modelWrapper->query();
115
    }
116
117
    /**
118
     * Returns the number of rows that match a certain condition
119
     *
120
     * @param mixed[] $where
121
     */
122 2
    public function count(?string $column = null, array $where = []): int
123
    {
124 2
        $parameters = [];
125 2
        if ($column !== null) {
126 1
            $parameters = $this->parameterResolver->column($column);
127
        }
128 2
        $parameters += $this->parameterResolver->where($where);
129
130 2
        return $this->modelWrapper->count($parameters);
131
    }
132
133
    /**
134
     * Returns the sum on a column of rows or null if the conditions don't match any rows
135
     *
136
     * @param mixed[] $where
137
     */
138 2
    public function sum(string $column, array $where = []): ?float
139
    {
140 2
        $parameters = $this->parameterResolver->column($column) + $this->parameterResolver->where($where);
141
142 2
        $sum = $this->modelWrapper->sum($parameters);
143 2
        if ($sum === null) {
144 1
            return null;
145
        }
146
147 1
        return (float) $sum;
148
    }
149
150
    /**
151
     * Returns the average on a column of rows or null if the conditions don't match any rows
152
     *
153
     * @param mixed[] $where
154
     */
155 2
    public function average(string $column, array $where = []): ?float
156
    {
157 2
        $parameters = $this->parameterResolver->column($column) + $this->parameterResolver->where($where);
158
159 2
        $average = $this->modelWrapper->average($parameters);
160 2
        if ($average === null) {
161 1
            return null;
162
        }
163
164 1
        return (float) $average;
165
    }
166
167
    /**
168
     * Returns the minimum on a column of rows or null if the conditions don't match any rows
169
     *
170
     * @param mixed[] $where
171
     */
172 2
    public function minimum(string $column, array $where = []): ?string
173
    {
174 2
        $parameters = $this->parameterResolver->column($column) + $this->parameterResolver->where($where);
175
176 2
        return $this->modelWrapper->minimum($parameters);
177
    }
178
179
    /**
180
     * Returns the maximum on a column of rows or null if the conditions don't match any rows
181
     *
182
     * @param mixed[] $where
183
     */
184 2
    public function maximum(string $column, array $where = []): ?string
185
    {
186 2
        $parameters = $this->parameterResolver->column($column) + $this->parameterResolver->where($where);
187
188 2
        return $this->modelWrapper->maximum($parameters);
189
    }
190
191
    /**
192
     * Returns the current parameter resolver that is being used
193
     */
194 2
    public function getParameterResolver(): ParameterResolver
195
    {
196 2
        return $this->parameterResolver;
197
    }
198
}
199