Passed
Push — master ( 1b9891...006f9a )
by Petr
04:14
created

AbstractRepository   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 6

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 10
c 1
b 0
f 1
lcom 2
cbo 6
dl 0
loc 77
ccs 26
cts 26
cp 1
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A getEntityManager() 0 4 1
A flush() 0 4 1
A persist() 0 4 1
A remove() 0 4 1
A findOneById() 0 4 1
A findAllWithLimitAndOffset() 0 13 3
A countAll() 0 8 1
A getEntityIdField() 0 7 1
1
<?php
2
3
namespace AppBundle\Entity\Infrasctucture;
4
5
use AppBundle\Form\AbstractFormType;
6
use Doctrine\ORM\EntityRepository;
7
use Symfony\Component\Intl\Exception\MethodNotImplementedException;
8
9
/**
10
 * @author Vehsamrak
11
 */
12
abstract class AbstractRepository extends EntityRepository
13
{
14
15
    /**
16
     * @return \Doctrine\ORM\EntityManager
17
     */
18
    public function getEntityManager()
19
    {
20
        return $this->_em;
21
    }
22
23 14
    public function flush($entity = null)
24
    {
25 14
        $this->_em->flush($entity);
26 14
    }
27
28
    /**
29
     * @param object $entity
30
     */
31 7
    public function persist($entity)
32
    {
33 7
        $this->_em->persist($entity);
34 7
    }
35
36
    /**
37
     * @param object $entity
38
     */
39 1
    public function remove($entity)
40
    {
41 1
        $this->_em->remove($entity);
42 1
    }
43
44
    /**
45
     * @param int|string|array $id
46
     * @return object|null Entity
47
     */
48 27
    public function findOneById($id)
49
    {
50 27
        return $this->find($id);
51
    }
52
53
    /**
54
     * @param int $limit
55
     * @param int $offset
56
     * @return object[] Entity array
57
     */
58 4
    public function findAllWithLimitAndOffset(int $limit = 50, int $offset = null): array
59
    {
60
        // Doctrine can handle correctly only null limits, but not 0
61 4
        if (!$limit) { $limit = null; }
62 4
        if (!$offset) { $offset = null; }
63
64 4
        return $this->findBy(
65 4
            [],
66 4
            null,
67
            $limit,
68
            $offset
69
        );
70
    }
71
72 4
    public function countAll(): int
73
    {
74 4
        $id = $this->getEntityIdField();
75 4
        $queryBuilder = $this->createQueryBuilder('entity');
76 4
        $queryBuilder->select($queryBuilder->expr()->count('entity.' . $id));
77
78 4
        return $queryBuilder->getQuery()->getSingleScalarResult();
79
    }
80
81 4
    private function getEntityIdField(): string
82
    {
83 4
        $ids = $this->getClassMetadata()->getIdentifierFieldNames();
84 4
        $id = reset($ids);
85
86 4
        return $id;
87
    }
88
}
89