Completed
Push — feature/middleware ( a81095...7bc422 )
by Derek Stephen
04:05
created

DragonRepository::getTotalDragonCount()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace BoneMvc\Module\Dragon\Repository;
4
5
use BoneMvc\Module\Dragon\Collection\DragonCollection;
6
use BoneMvc\Module\Dragon\Entity\Dragon;
7
use Doctrine\ORM\EntityNotFoundException;
8
use Doctrine\ORM\EntityRepository;
9
10
class DragonRepository extends EntityRepository
11
{
12
    /**
13
     * @param mixed $id
14
     * @param null $lockMode
15
     * @param null $lockVersion
16
     * @return Dragon
17
     * @throws EntityNotFoundException
18
     */
19
    public function find($id, $lockMode = null, $lockVersion = null): Dragon
20
    {
21
        /** @var Dragon $dragon */
22
        $dragon =  parent::find($id, $lockMode, $lockVersion);
23
24
        if (!$dragon) {
25
            throw new EntityNotFoundException('Dragon not found.', 404);
26
        }
27
28
        return $dragon;
29
    }
30
31
    /**
32
     * @param Dragon $dragon
33
     * @return $dragon
0 ignored issues
show
Documentation introduced by
The doc-type $dragon could not be parsed: Unknown type name "$dragon" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
34
     * @throws \Doctrine\ORM\ORMException
35
     * @throws \Doctrine\ORM\OptimisticLockException
36
     */
37
    public function save(Dragon $dragon): Dragon
38
    {
39
        if(!$dragon->getID()) {
40
            $this->_em->persist($dragon);
41
        }
42
        $this->_em->flush($dragon);
43
44
        return $dragon;
45
    }
46
47
    /**
48
     * @param Dragon $dragon
49
     * @throws \Doctrine\ORM\OptimisticLockException
50
     * @throws \Doctrine\ORM\ORMException
51
     */
52
    public function delete(Dragon $dragon): void
53
    {
54
        $this->_em->remove($dragon);
55
        $this->_em->flush($dragon);
56
    }
57
58
    /**
59
     * @return mixed
60
     * @throws \Doctrine\ORM\NonUniqueResultException
61
     */
62
    public function getTotalDragonCount()
63
    {
64
        $qb = $this->createQueryBuilder('d');
65
        $qb->select('count(d.id)');
66
        $query = $qb->getQuery();
67
68
        return (int) $query->getSingleScalarResult();
69
    }
70
}
71