Completed
Push — feature/middleware ( e85030...d864e0 )
by Derek Stephen
05:41
created

OrcRepository   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 61
Duplicated Lines 100 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
wmc 6
lcom 0
cbo 1
dl 61
loc 61
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A find() 10 10 2
A save() 9 9 2
A delete() 5 5 1
A getTotalOrcCount() 8 8 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace BoneMvc\Module\Orc\Repository;
6
7
use BoneMvc\Module\Orc\Collection\OrcCollection;
8
use BoneMvc\Module\Orc\Entity\Orc;
9
use Doctrine\ORM\EntityNotFoundException;
10
use Doctrine\ORM\EntityRepository;
11
12 View Code Duplication
class OrcRepository extends EntityRepository
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
13
{
14
    /**
15
     * @param int $id
16
     * @param int|null $lockMode
17
     * @param int|null $lockVersion
18
     * @return Orc
19
     * @throws \Doctrine\ORM\ORMException
20
     */
21
    public function find($id, $lockMode = null, $lockVersion = null): Orc
22
    {
23
        /** @var Orc $orc */
24
        $orc =  parent::find($id, $lockMode, $lockVersion);
25
        if (!$orc) {
26
            throw new EntityNotFoundException('Orc not found.', 404);
27
        }
28
29
        return $orc;
30
    }
31
32
    /**
33
     * @param Orc $orc
34
     * @return $orc
0 ignored issues
show
Documentation introduced by
The doc-type $orc could not be parsed: Unknown type name "$orc" 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...
35
     * @throws \Doctrine\ORM\ORMException
36
     * @throws \Doctrine\ORM\OptimisticLockException
37
     */
38
    public function save(Orc $orc): Orc
39
    {
40
        if(!$orc->getID()) {
41
            $this->_em->persist($orc);
42
        }
43
        $this->_em->flush($orc);
44
45
        return $orc;
46
    }
47
48
    /**
49
     * @param Orc $orc
50
     * @throws \Doctrine\ORM\OptimisticLockException
51
     * @throws \Doctrine\ORM\ORMException
52
     */
53
    public function delete(Orc $orc): void
54
    {
55
        $this->_em->remove($orc);
56
        $this->_em->flush($orc);
57
    }
58
59
    /**
60
     * @return int
61
     * @throws \Doctrine\ORM\NoResultException
62
     * @throws \Doctrine\ORM\NonUniqueResultException
63
     */
64
    public function getTotalOrcCount(): int
65
    {
66
        $qb = $this->createQueryBuilder('o');
67
        $qb->select('count(o.id)');
68
        $query = $qb->getQuery();
69
70
        return (int) $query->getSingleScalarResult();
71
    }
72
}
73