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

OrcService   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 81
Duplicated Lines 12.35 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 2
dl 10
loc 81
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A createFromArray() 0 6 1
B updateFromArray() 10 19 9
A saveOrc() 0 4 1
A deleteOrc() 0 4 1
A getRepository() 0 7 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\Service;
6
7
use BoneMvc\Module\Orc\Entity\Orc;
8
use BoneMvc\Module\Orc\Repository\OrcRepository;
9
use DateTime;
10
use Doctrine\ORM\EntityManager;
11
12
class OrcService
13
{
14
    /** @var EntityManager $em */
15
    private $em;
16
17
    /**
18
     * @param EntityManager $em
19
     */
20
    public function __construct(EntityManager $em)
21
    {
22
        $this->em = $em;
23
    }
24
25
    /**
26
     * @param array $data
27
     * @return Orc
28
     */
29
    public function createFromArray(array $data): Orc
30
    {
31
        $orc = new Orc();
32
33
        return $this->updateFromArray($orc, $data);
34
    }
35
36
    /**
37
     * @param Orc $orc
38
     * @param array $data
39
     * @return Orc
40
     */
41
    public function updateFromArray(Orc $orc, array $data): Orc
42
    {
43
        isset($data['id']) ? $orc->setId($data['id']) : null;
44
        isset($data['name']) ? $orc->setName($data['name']) : null;
45
46 View Code Duplication
        if (isset($data['dob'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
47
            $dob = $data['dob'] instanceof DateTime ? $data['dob'] : DateTime::createFromFormat('d/m/Y', $data['dob']);
48
            $dob = $dob ?: null;
49
            $orc->setDob($dob);
50
        }
51
52 View Code Duplication
        if (isset($data['eventTime'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
53
            $eventTime = $data['eventTime'] instanceof DateTime ? $data['eventTime'] : DateTime::createFromFormat('d/m/Y H:i', $data['eventTime']);
54
                $eventTime = $eventTime ?: null;
55
            $orc->setEventTime($eventTime);
56
        }
57
58
        return $orc;
59
    }
60
61
    /**
62
     * @param Orc $orc
63
     * @return Orc
64
     * @throws \Doctrine\ORM\ORMException
65
     * @throws \Doctrine\ORM\OptimisticLockException
66
     */
67
    public function saveOrc(Orc $orc): Orc
68
    {
69
        return $this->getRepository()->save($orc);
70
    }
71
72
    /**
73
     * @param Orc $orc
74
     * @throws \Doctrine\ORM\ORMException
75
     * @throws \Doctrine\ORM\OptimisticLockException
76
     */
77
    public function deleteOrc(Orc $orc): void
78
    {
79
        $this->getRepository()->delete($orc);
80
    }
81
82
    /**
83
     * @return OrcRepository
84
     */
85
    public function getRepository(): OrcRepository
86
    {
87
        /** @var OrcRepository $repository */
88
        $repository = $this->em->getRepository(Orc::class);
89
90
        return $repository;
91
    }
92
}
93