|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the SexyField package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Dion Snoeijen <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
declare (strict_types = 1); |
|
13
|
|
|
|
|
14
|
|
|
namespace Tardigrades\SectionField\Service; |
|
15
|
|
|
|
|
16
|
|
|
use Tardigrades\Entity\Application; |
|
17
|
|
|
use Tardigrades\Entity\ApplicationInterface; |
|
18
|
|
|
use Tardigrades\SectionField\ValueObject\ApplicationConfig; |
|
19
|
|
|
use Tardigrades\SectionField\ValueObject\Handle; |
|
20
|
|
|
use Tardigrades\SectionField\ValueObject\Id; |
|
21
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
|
22
|
|
|
|
|
23
|
|
|
class DoctrineApplicationManager implements ApplicationManagerInterface |
|
24
|
|
|
{ |
|
25
|
|
|
/** |
|
26
|
|
|
* @var EntityManagerInterface |
|
27
|
|
|
*/ |
|
28
|
|
|
private $entityManager; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var LanguageManagerInterface |
|
32
|
|
|
*/ |
|
33
|
|
|
private $languageManager; |
|
34
|
|
|
|
|
35
|
|
|
public function __construct( |
|
36
|
|
|
EntityManagerInterface $entityManager, |
|
37
|
|
|
LanguageManagerInterface $languageManager |
|
38
|
|
|
) { |
|
39
|
|
|
$this->entityManager = $entityManager; |
|
40
|
|
|
$this->languageManager = $languageManager; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
public function create(ApplicationInterface $entity): ApplicationInterface |
|
44
|
|
|
{ |
|
45
|
|
|
$this->entityManager->persist($entity); |
|
46
|
|
|
$this->entityManager->flush(); |
|
47
|
|
|
|
|
48
|
|
|
return $entity; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
public function read(Id $id): ApplicationInterface |
|
52
|
|
|
{ |
|
53
|
|
|
$applicationRepository = $this->entityManager->getRepository(Application::class); |
|
54
|
|
|
|
|
55
|
|
|
/** @var $application Application */ |
|
56
|
|
|
$application = $applicationRepository->find($id->toInt()); |
|
57
|
|
|
|
|
58
|
|
|
if (empty($application)) { |
|
59
|
|
|
throw new ApplicationNotFoundException(); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
return $application; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
public function readByHandle(Handle $handle): ApplicationInterface |
|
66
|
|
|
{ |
|
67
|
|
|
$applicationRepository = $this->entityManager->getRepository(Application::class); |
|
68
|
|
|
|
|
69
|
|
|
/** @var $application Application */ |
|
70
|
|
|
$application = $applicationRepository->findOneBy(['handle' => (string)$handle]); |
|
71
|
|
|
|
|
72
|
|
|
if (empty($application)) { |
|
73
|
|
|
throw new ApplicationNotFoundException(); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
return $application; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
public function readAll(): array |
|
80
|
|
|
{ |
|
81
|
|
|
$applicationRepository = $this->entityManager->getRepository(Application::class); |
|
82
|
|
|
$applications = $applicationRepository->findAll(); |
|
83
|
|
|
|
|
84
|
|
|
if (empty($applications)) { |
|
85
|
|
|
throw new ApplicationNotFoundException(); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
return $applications; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
public function update(): void |
|
92
|
|
|
{ |
|
93
|
|
|
$this->entityManager->flush(); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
public function delete(ApplicationInterface $entity): void |
|
97
|
|
|
{ |
|
98
|
|
|
$this->entityManager->remove($entity); |
|
99
|
|
|
$this->entityManager->flush(); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
public function createByConfig(ApplicationConfig $applicationConfig): ApplicationInterface |
|
103
|
|
|
{ |
|
104
|
|
|
$application = $this->setUpByConfig($applicationConfig, new Application()); |
|
105
|
|
|
$this->entityManager->persist($application); |
|
106
|
|
|
$this->entityManager->flush(); |
|
107
|
|
|
|
|
108
|
|
|
return $application; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
public function updateByConfig( |
|
112
|
|
|
ApplicationConfig $applicationConfig, |
|
113
|
|
|
ApplicationInterface $application |
|
114
|
|
|
): ApplicationInterface { |
|
115
|
|
|
$this->setUpByConfig($applicationConfig, $application); |
|
116
|
|
|
$this->entityManager->flush(); |
|
117
|
|
|
|
|
118
|
|
|
return $application; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
private function setUpByConfig( |
|
122
|
|
|
ApplicationConfig $applicationConfig, |
|
123
|
|
|
ApplicationInterface $application |
|
124
|
|
|
): ApplicationInterface { |
|
125
|
|
|
$applicationConfig = $applicationConfig->toArray(); |
|
126
|
|
|
|
|
127
|
|
|
$installedLanguages = $this->languageManager->readByI18ns($applicationConfig['application']['languages']); |
|
128
|
|
|
|
|
129
|
|
|
$application->setName($applicationConfig['application']['name']); |
|
130
|
|
|
$application->setHandle($applicationConfig['application']['handle']); |
|
131
|
|
|
foreach ($installedLanguages as $language) { |
|
132
|
|
|
$application->addLanguage($language); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
return $application; |
|
136
|
|
|
} |
|
137
|
|
|
} |
|
138
|
|
|
|