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

UnicornService   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 78
Duplicated Lines 6.41 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 2
dl 5
loc 78
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() 5 16 9
A saveUnicorn() 0 4 1
A deleteUnicorn() 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\Unicorn\Service;
6
7
use BoneMvc\Module\Unicorn\Entity\Unicorn;
8
use BoneMvc\Module\Unicorn\Repository\UnicornRepository;
9
use DateTime;
10
use Doctrine\ORM\EntityManager;
11
12
class UnicornService
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 Unicorn
28
     */
29
    public function createFromArray(array $data): Unicorn
30
    {
31
        $unicorn = new Unicorn();
32
33
        return $this->updateFromArray($unicorn, $data);
34
    }
35
36
    /**
37
     * @param Unicorn $unicorn
38
     * @param array $data
39
     * @return Unicorn
40
     */
41
    public function updateFromArray(Unicorn $unicorn, array $data): Unicorn
42
    {
43
        isset($data['id']) ? $unicorn->setId($data['id']) : null;
44
        isset($data['name']) ? $unicorn->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
            $unicorn->setDob($dob);
50
        }
51
        isset($data['food']) ? $unicorn->setFood((int) $data['food']) : null;
52
        isset($data['canFly']) ? $unicorn->setCanFly((bool) $data['c']) : null;
53
        isset($data['drink']) ? $unicorn->setDrink((int) $data['drink']) : null;
54
55
        return $unicorn;
56
    }
57
58
    /**
59
     * @param Unicorn $unicorn
60
     * @return Unicorn
61
     * @throws \Doctrine\ORM\ORMException
62
     * @throws \Doctrine\ORM\OptimisticLockException
63
     */
64
    public function saveUnicorn(Unicorn $unicorn): Unicorn
65
    {
66
        return $this->getRepository()->save($unicorn);
67
    }
68
69
    /**
70
     * @param Unicorn $unicorn
71
     * @throws \Doctrine\ORM\ORMException
72
     * @throws \Doctrine\ORM\OptimisticLockException
73
     */
74
    public function deleteUnicorn(Unicorn $unicorn): void
75
    {
76
        $this->getRepository()->delete($unicorn);
77
    }
78
79
    /**
80
     * @return UnicornRepository
81
     */
82
    public function getRepository(): UnicornRepository
83
    {
84
        /** @var UnicornRepository $repository */
85
        $repository = $this->em->getRepository(Unicorn::class);
86
87
        return $repository;
88
    }
89
}
90