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'])) { |
|
|
|
|
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
|
|
|
|
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.