Completed
Push — master ( 0c9e12...6c5b45 )
by Derek Stephen
01:57
created

Entry::checkLimit()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 2
eloc 2
nc 2
nop 1
crap 2
1
<?php
2
3
namespace Del\Expenses\Repository;
4
5
use Del\Expenses\Entity\EntryInterface;
6
use Del\Expenses\Criteria\EntryCriteria;
7
use Doctrine\ORM\EntityRepository;
8
use Doctrine\ORM\QueryBuilder;
9
10
class Entry extends EntityRepository
11
{
12
    /** @var QueryBuilder */
13
    private $qb;
14
15
    /**
16
     * @param EntryInterface $entry
17
     * @return EntryInterface
18
     */
19 4
    public function save(EntryInterface $entry)
20
    {
21 4
        $this->_em->persist($entry);
22 4
        $this->_em->flush();
23 4
        return $entry;
24
    }
25
26
    /**
27
     * @param EntryInterface $entry
28
     */
29 4
    public function delete(EntryInterface $entry)
30
    {
31 4
        $this->_em->remove($entry);
32 4
        $this->_em->flush();
33 4
    }
34
35
36
    /**
37
     * @param EntryCriteria $criteria
38
     * @return EntryInterface|null
39
     */
40 3
    public function findOneByCriteria(EntryCriteria $criteria)
41
    {
42 3
        $results = $this->findByCriteria($criteria);
43 3
        return isset($results[0]) ? $results[0] : null;
44
    }
45
46
47
48
    /**
49
     * @param EntryCriteria $criteria
50
     * @return array
51
     */
52 4
    public function findByCriteria(EntryCriteria $criteria)
53
    {
54 4
        $this->qb = $this->createQueryBuilder('e');
55 4
        $this->checkId($criteria);
56 4
        $this->checkUserId($criteria);
57 4
        $this->checkDate($criteria);
58 4
        $this->checkAmount($criteria);
59 4
        $this->checkCategory($criteria);
60 4
        $this->checkDescription($criteria);
61 4
        $this->checkNote($criteria);
62 4
        $this->checkType($criteria);
63 4
        $this->checkOrder($criteria);
64 4
        $this->checkLimit($criteria);
65 4
        $this->checkOffset($criteria);
66 4
        $query = $this->qb->getQuery();
67 4
        unset($this->qb);
68 4
        return $query->getResult();
69
    }
70
71
    /**
72
     * @param EntryCriteria $criteria
73
     */
74 4
    private function checkId(EntryCriteria $criteria)
75
    {
76 4
        if($criteria->hasId()) {
77 3
            $this->qb->where('e.id = :id');
78 3
            $this->qb->setParameter('id', $criteria->getId());
79 3
        }
80 4
    }
81
82
    /**
83
     * @param EntryCriteria $criteria
84
     */
85 4
    private function checkUserId(EntryCriteria $criteria)
86
    {
87 4
        if($criteria->hasUserId()) {
88 1
            $this->qb->where('e.userId = :userid');
89 1
            $this->qb->setParameter('userid', $criteria->getUserId());
90 1
        }
91 4
    }
92
93
    /**
94
     * @param EntryCriteria $criteria
95
     */
96 4
    private function checkDate(EntryCriteria $criteria)
97
    {
98 4
        if($criteria->hasDate()) {
99 1
            $this->qb->where('e.date = :date');
100 1
            $this->qb->setParameter('date', $criteria->getDate());
101 1
        }
102 4
    }
103
104
    /**
105
     * @param EntryCriteria $criteria
106
     */
107 4
    private function checkAmount(EntryCriteria $criteria)
108
    {
109 4
        if($criteria->hasAmount()) {
110 1
            $this->qb->where('e.amount = :amount');
111 1
            $this->qb->setParameter('amount', $criteria->getAmount());
112 1
        }
113 4
    }
114
115
    /**
116
     * @param EntryCriteria $criteria
117
     */
118 4
    private function checkCategory(EntryCriteria $criteria)
119
    {
120 4
        if($criteria->hasCategory()) {
121 1
            $this->qb->andWhere('e.category = :category');
122 1
            $this->qb->setParameter('category', $criteria->getCategory());
123 1
        }
124 4
    }
125
126
    /**
127
     * @param EntryCriteria $criteria
128
     */
129 4
    private function checkDescription(EntryCriteria $criteria)
130
    {
131 4
        if($criteria->hasDescription()) {
132 1
            $this->qb->andWhere('e.description = :description');
133 1
            $this->qb->setParameter('description', $criteria->getDescription());
134 1
        }
135 4
    }
136
137
    /**
138
     * @param EntryCriteria $criteria
139
     */
140 4
    private function checkNote(EntryCriteria $criteria)
141
    {
142 4
        if($criteria->hasNote()) {
143 1
            $this->qb->andWhere('e.note = :note');
144 1
            $this->qb->setParameter('note', $criteria->getNote());
145 1
        }
146 4
    }
147
148
    /**
149
     * @param EntryCriteria $criteria
150
     */
151 4
    private function checkType(EntryCriteria $criteria)
152
    {
153 4
        if($criteria->hasType()) {
154 1
            $this->processType($criteria);
155 1
        }
156 4
    }
157
158
    /**
159
     * @param EntryCriteria $criteria
160
     */
161 1
    private function processType(EntryCriteria $criteria)
162
    {
163 1
        switch($criteria->getType()) {
164 1
            case 'IN':
165 1
                $this->qb->andWhere('e INSTANCE OF Del\Expenses\Entity\Income');
166 1
                break;
167 1
            case 'OUT':
168 1
                $this->qb->andWhere('e INSTANCE OF Del\Expenses\Entity\Expenditure');
169 1
                break;
170 1
            case 'CLAIM':
171 1
                $this->qb->andWhere('e INSTANCE OF Del\Expenses\Entity\ExpenseClaim');
172 1
                break;
173 1
        }
174 1
    }
175
176
    /**
177
     * @param EntryCriteria $criteria
178
     */
179 4
    private function checkOrder(EntryCriteria $criteria)
180
    {
181 4
        $criteria->hasOrder() ? $this->qb->addOrderBy('e.'.$criteria->getOrder(), $criteria->getOrderDirection()) : null;
182
183 4
    }
184
185
    /**
186
     * @param EntryCriteria $criteria
187
     */
188 4
    private function checkLimit(EntryCriteria $criteria)
189
    {
190 4
        $criteria->hasLimit() ? $this->qb->setMaxResults($criteria->getLimit()) : null;
191 4
    }
192
193
    /**
194
     * @param EntryCriteria $criteria
195
     */
196 4
    private function checkOffset(EntryCriteria $criteria)
197
    {
198 4
        $criteria->hasOffset() ? $this->qb->setFirstResult($criteria->getOffset()) : null;
199 4
    }
200
201
}
202