AbstractRepository::flush()   A
last analyzed

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 11
    public function flush($entity = null)
22
    {
23 11
        $this->_em->flush($entity);
24 11
    }
25
26
    /**
27
     * @param object $entity
28
     */
29 9
    public function persist($entity)
30
    {
31 9
        $this->_em->persist($entity);
32 9
    }
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 28
    public function findOneById($id)
47
    {
48 28
        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 = 50, 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
        $id = $this->getEntityIdField();
73 4
        $queryBuilder = $this->createQueryBuilder('entity');
74 4
        $queryBuilder->select($queryBuilder->expr()->count('entity.' . $id));
75
76 4
        return $queryBuilder->getQuery()->getSingleScalarResult();
77
    }
78
79 4
    private function getEntityIdField(): string
80
    {
81 4
        $ids = $this->getClassMetadata()->getIdentifierFieldNames();
82 4
        $id = reset($ids);
83
84 4
        return $id;
85
    }
86
}
87