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