Passed
Push — master ( 21e202...0aab8f )
by Petr
03:53
created

AbstractRepository::flush()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
namespace AppBundle\Entity\Infrasctucture;
4
5
use Doctrine\ORM\EntityRepository;
6
7
/**
8
 * @author Vehsamrak
9
 */
10
abstract class AbstractRepository extends EntityRepository
11
{
12
13
    /**
14
     * @return \Doctrine\ORM\EntityManager
15
     */
16
    public function getEntityManager()
17
    {
18
        return $this->_em;
19
    }
20
21 14
    public function flush($entity = null)
22
    {
23 14
        $this->_em->flush($entity);
24 14
    }
25
26
    /**
27
     * @param object $entity
28
     */
29 6
    public function persist($entity)
30
    {
31 6
        $this->_em->persist($entity);
32 6
    }
33
34
    /**
35
     * @param object $entity
36
     */
37 1
    public function remove($entity)
38
    {
39 1
        $this->_em->remove($entity);
40 1
    }
41
42
    /**
43
     * @param int|string|array $id
44
     * @return object|null Entity
45
     */
46 21
    public function findOneById($id)
47
    {
48 21
        return $this->find($id);
49
    }
50
51
    /**
52
     * @param int $limit
53
     * @param int $offset
54
     * @return object[] Entity array
55
     */
56 4
    public function findAllWithLimitAndOffset(int $limit = null, int $offset = null): array
57
    {
58
        // Doctrine can handle correctly only null limits, but not 0
59 4
        if (!$limit) { $limit = null; }
60 4
        if (!$offset) { $offset = null; }
61
62 4
        return $this->findBy(
63 4
            [],
64 4
            null,
65
            $limit,
66
            $offset
67
        );
68
    }
69
70 4
    public function countAll(): int
71
    {
72 4
        $ids = $this->getClassMetadata()->getIdentifierFieldNames();
73 4
        $id = reset($ids);
74 4
        $queryBuilder = $this->createQueryBuilder('entity');
75 4
        $queryBuilder->select($queryBuilder->expr()->count('entity.' . $id));
76
77 4
        return $queryBuilder->getQuery()->getSingleScalarResult();
78
    }
79
}
80