QueryObject::generateWhereClause()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3.0175

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 7
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 12
ccs 7
cts 8
cp 0.875
crap 3.0175
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