Passed
Push — master ( 25c2e9...26c6a8 )
by Petr
08:32
created

AbstractRepository::findOneByFormData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 0
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 2
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 object|null Entity
17
     */
18
    public function findOneByFormData(AbstractFormType $formType)
19
    {
20
        throw new MethodNotImplementedException(__METHOD__);
21
    }
22
23
    /**
24
     * @return \Doctrine\ORM\EntityManager
25
     */
26 2
    public function getEntityManager()
27
    {
28 2
        return $this->_em;
29
    }
30
31 14
    public function flush($entity = null)
32
    {
33 14
        $this->_em->flush($entity);
34 14
    }
35
36
    /**
37
     * @param object $entity
38
     */
39 7
    public function persist($entity)
40
    {
41 7
        $this->_em->persist($entity);
42 7
    }
43
44
    /**
45
     * @param object $entity
46
     */
47 1
    public function remove($entity)
48
    {
49 1
        $this->_em->remove($entity);
50 1
    }
51
52
    /**
53
     * @param int|string|array $id
54
     * @return object|null Entity
55
     */
56 22
    public function findOneById($id)
57
    {
58 22
        return $this->find($id);
59
    }
60
61
    /**
62
     * @param int $limit
63
     * @param int $offset
64
     * @return object[] Entity array
65
     */
66 4
    public function findAllWithLimitAndOffset(int $limit = null, int $offset = null): array
67
    {
68
        // Doctrine can handle correctly only null limits, but not 0
69 4
        if (!$limit) { $limit = null; }
70 4
        if (!$offset) { $offset = null; }
71
72 4
        return $this->findBy(
73 4
            [],
74 4
            null,
75
            $limit,
76
            $offset
77
        );
78
    }
79
80 4
    public function countAll(): int
81
    {
82 4
        $ids = $this->getClassMetadata()->getIdentifierFieldNames();
83 4
        $id = reset($ids);
84 4
        $queryBuilder = $this->createQueryBuilder('entity');
85 4
        $queryBuilder->select($queryBuilder->expr()->count('entity.' . $id));
86
87 4
        return $queryBuilder->getQuery()->getSingleScalarResult();
88
    }
89
}
90