1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace App\Genres\Service; |
6
|
|
|
|
7
|
|
|
use App\Genres\Entity\Genre; |
8
|
|
|
use App\Genres\Entity\GenreTranslations; |
9
|
|
|
use App\Genres\Request\CreateGenreRequest; |
10
|
|
|
use App\Genres\Request\UpdateGenreRequest; |
11
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
12
|
|
|
|
13
|
|
|
class GenreManageService |
14
|
|
|
{ |
15
|
|
|
private $entityManager; |
16
|
|
|
|
17
|
2 |
|
public function __construct(EntityManagerInterface $entityManager) |
18
|
|
|
{ |
19
|
2 |
|
$this->entityManager = $entityManager; |
20
|
2 |
|
} |
21
|
|
|
|
22
|
1 |
|
public function createGenreByRequest(CreateGenreRequest $request): Genre |
23
|
|
|
{ |
24
|
1 |
|
return $this->createGenre($request->get('genre')['translations']); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
public function updateGenreByRequest(UpdateGenreRequest $request, Genre $genre): Genre |
28
|
|
|
{ |
29
|
|
|
return $this->updateGenre($genre, $request->get('genre')['translations']); |
30
|
|
|
} |
31
|
|
|
|
32
|
1 |
|
public function createGenre(array $translations): Genre |
33
|
|
|
{ |
34
|
1 |
|
$genre = new Genre(); |
35
|
|
|
|
36
|
|
|
$addTranslation = function ($translation) use ($genre) { |
37
|
1 |
|
$genre->addTranslation( |
38
|
1 |
|
new GenreTranslations($genre, $translation['locale'], $translation['name']) |
39
|
|
|
); |
40
|
1 |
|
}; |
41
|
|
|
|
42
|
1 |
|
$genre->updateTranslations($translations, $addTranslation); |
43
|
1 |
|
$this->entityManager->persist($genre); |
44
|
|
|
|
45
|
1 |
|
return $genre; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function updateGenre(Genre $genre, array $translations): Genre |
49
|
|
|
{ |
50
|
|
|
$addTranslation = function (array $translation) use ($genre) { |
51
|
|
|
$genre->addTranslation( |
52
|
|
|
new GenreTranslations($genre, $translation['locale'], $translation['name']) |
53
|
|
|
); |
54
|
|
|
}; |
55
|
|
|
|
56
|
|
|
$updateTranslation = function (array $translation, GenreTranslations $oldTranslation) { |
57
|
|
|
$oldTranslation->changeName($translation['name']); |
58
|
|
|
}; |
59
|
|
|
|
60
|
|
|
$genre->updateTranslations($translations, $addTranslation, $updateTranslation); |
61
|
|
|
$this->entityManager->persist($genre); |
62
|
|
|
|
63
|
|
|
return $genre; |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|