Passed
Pull Request — 1.11.x (#3943)
by Angel Fernando Quiroz
14:18
created

StatementRepository   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 15
dl 0
loc 47
rs 10
c 2
b 0
f 0
wmc 7

4 Methods

Rating   Name   Duplication   Size   Complexity  
A findStatement() 0 3 1
A storeStatement() 0 6 2
A findStatements() 0 3 1
A getQueryBuilder() 0 17 3
1
<?php
2
3
/*
4
 * This file is part of the xAPI package.
5
 *
6
 * (c) Christian Flothmann <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace XApi\Repository\ORM;
13
14
use Doctrine\ORM\EntityRepository;
15
use XApi\Repository\Doctrine\Mapping\Statement;
16
use XApi\Repository\Doctrine\Repository\Mapping\StatementRepository as BaseStatementRepository;
17
18
/**
19
 * @author Christian Flothmann <[email protected]>
20
 */
21
final class StatementRepository extends EntityRepository implements BaseStatementRepository
22
{
23
    /**
24
     * {@inheritdoc}
25
     */
26
    public function findStatement(array $criteria)
27
    {
28
        return parent::findOneBy($criteria);
29
    }
30
31
    /**
32
     * {@inheritdoc}
33
     */
34
    public function findStatements(array $criteria)
35
    {
36
        return $this->getQueryBuilder($criteria)->getQuery()->getResult();
37
    }
38
39
    /**
40
     * {@inheritdoc}
41
     */
42
    public function storeStatement(Statement $mappedStatement, $flush = true)
43
    {
44
        $this->_em->persist($mappedStatement);
45
46
        if ($flush) {
47
            $this->_em->flush();
48
        }
49
    }
50
51
    private function getQueryBuilder(array $criteria): \Doctrine\ORM\QueryBuilder
52
    {
53
        $qb = $this->createQueryBuilder('statement');
54
55
        if (!empty($criteria['verb'])) {
56
            $qb->innerJoin('statement.verb', 'verb');
57
            $qb->andWhere($qb->expr()->eq('verb.id', ':c_verb'));
58
            $qb->setParameter('c_verb', $criteria['verb']);
59
        }
60
61
        $qb->setFirstResult($criteria['cursor']);
62
63
        if (isset($criteria['limit'])) {
64
            $qb->setMaxResults($criteria['limit']);
65
        }
66
67
        return $qb;
68
    }
69
}
70