QueryObject::addCriteria()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace MetadataMapping\Query;
6
7
use ArrayIterator;
8
use Behavioral\UnitOfWork;
9
use MetadataMapping\Metadata\DataMap;
10
11
class QueryObject
12
{
13
    private $className;
14
15
    private $criteria;
16
17
    private $bindValues = [];
18
19 1
    public function __construct(string $className)
20
    {
21 1
        $this->className = $className;
22 1
        $this->criteria = new ArrayIterator();
23 1
    }
24
25 1
    public function addCriteria(Criteria $criteria): void
26
    {
27 1
        $this->criteria->append($criteria);
28 1
    }
29
30 1
    public function execute(UnitOfWork $uow): ArrayIterator
31
    {
32 1
        $dataMapper = $uow->getDataMapper($this->className);
33
34 1
        return $dataMapper->findObjectsWhere(
35 1
            $this->generateWhereClause($dataMapper->getDataMap()),
36 1
            $this->bindValues
37
        );
38
    }
39
40 1
    private function generateWhereClause(DataMap $dataMap): string
41
    {
42 1
        $result = "";
43 1
        foreach ($this->criteria as $criterion) {
44 1
            if (strlen($result) != 0) {
45
                $result .= " AND ";
46
            }
47 1
            $result .= $criterion->generateSql($dataMap);
48 1
            $this->bindValues[] = $criterion->getValue();
49
        }
50
51 1
        return $result;
52
    }
53
}
54