Completed
Push — standalone ( 7c3a2c...83bb11 )
by Philip
05:16
created

DoctrineEntityRepositoryCrudService::isUuid()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Dontdrinkandroot\RestBundle\Service;
4
5
use Doctrine\ORM\EntityRepository;
6
use Doctrine\ORM\Tools\Pagination\Paginator;
7
8
class DoctrineEntityRepositoryCrudService extends EntityRepository implements CrudServiceInterface
9
{
10 18
    public function __construct($entityManager, $entityClass)
11
    {
12 18
        parent::__construct($entityManager, $entityManager->getClassMetadata($entityClass));
13 18
    }
14
15
    /**
16
     * {@inheritdoc}
17
     */
18 14
    public function findById($id)
19
    {
20 14
        if ($this->isUuid($id)) {
21
            return $this->findOneBy(['uuid' => $id]);
22
        }
23
24 14
        return $this->find($id);
25
    }
26
27
    /**
28
     * {@inheritdoc}
29
     */
30 4
    public function listPaginated(int $page, int $perPage = 50): Paginator
31
    {
32 4
        $queryBuilder = $this->createQueryBuilder('entity');
33 4
        $queryBuilder->setFirstResult(($page - 1) * $perPage);
34 4
        $queryBuilder->setMaxResults($perPage);
35
36 4
        return new Paginator($queryBuilder);
37
    }
38
39 14
    protected function isUuid($id)
40
    {
41 14
        return 1 === preg_match('/^[0-9A-Fa-f]{8}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{12}$/', $id);
42
    }
43
44
    /**
45
     * {@inheritdoc}
46
     */
47
    public function create($entity)
48
    {
49
        $this->getEntityManager()->persist($entity);
50
        $this->getEntityManager()->flush($entity);
51
52
        return $entity;
53
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58 2
    public function update($entity)
59
    {
60 2
        $this->getEntityManager()->flush($entity);
61
62 2
        return $entity;
63
    }
64
65
    /**
66
     * {@inheritdoc}
67
     */
68
    public function remove($entity)
69
    {
70
        $this->getEntityManager()->remove($entity);
71
        $this->getEntityManager()->flush($entity);
72
    }
73
}
74