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 Doctrine\ORM\EntityManagerInterface; |
17
|
|
|
use Tardigrades\Entity\FieldType; |
18
|
|
|
use Tardigrades\SectionField\ValueObject\FullyQualifiedClassName; |
19
|
|
|
use Tardigrades\SectionField\ValueObject\Id; |
20
|
|
|
use Tardigrades\SectionField\ValueObject\Type; |
21
|
|
|
use Tardigrades\Entity\FieldTypeInterface; |
22
|
|
|
|
23
|
|
|
class DoctrineFieldTypeManager implements FieldTypeManagerInterface |
24
|
|
|
{ |
25
|
|
|
/** @var EntityManagerInterface */ |
26
|
|
|
private $entityManager; |
27
|
|
|
|
28
|
|
|
public function __construct(EntityManagerInterface $entityManager) |
29
|
|
|
{ |
30
|
|
|
$this->entityManager = $entityManager; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function create(FieldTypeInterface $entity): FieldTypeInterface |
34
|
|
|
{ |
35
|
|
|
$this->entityManager->persist($entity); |
36
|
|
|
$this->entityManager->flush(); |
37
|
|
|
|
38
|
|
|
return $entity; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function read(Id $id): FieldTypeInterface |
42
|
|
|
{ |
43
|
|
|
$fieldTypeRepo = $this->entityManager->getRepository(FieldType::class); |
44
|
|
|
/** @var $fieldType FieldType */ |
45
|
|
|
$fieldType = $fieldTypeRepo->find($id->toInt()); |
46
|
|
|
if (empty($fieldType)) { |
47
|
|
|
throw new FieldTypeNotFoundException(); |
48
|
|
|
} |
49
|
|
|
return $fieldType; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function readAll(): array |
53
|
|
|
{ |
54
|
|
|
$fieldTypeRepository = $this->entityManager->getRepository(FieldType::class); |
55
|
|
|
$fieldTypes = $fieldTypeRepository->findAll(); |
56
|
|
|
if (empty($fieldTypes)) { |
57
|
|
|
throw new FieldTypeNotFoundException(); |
58
|
|
|
} |
59
|
|
|
return $fieldTypes; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function update(): void |
63
|
|
|
{ |
64
|
|
|
$this->entityManager->flush(); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function delete(FieldTypeInterface $entity): void |
68
|
|
|
{ |
69
|
|
|
$this->entityManager->remove($entity); |
70
|
|
|
$this->entityManager->flush(); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function createWithFullyQualifiedClassName( |
74
|
|
|
FullyQualifiedClassName $fullyQualifiedClassName |
75
|
|
|
): FieldTypeInterface { |
76
|
|
|
$fieldType = new FieldType(); |
77
|
|
|
$fieldType->setType($fullyQualifiedClassName->getClassName()); |
78
|
|
|
$fieldType->setFullyQualifiedClassName((string) $fullyQualifiedClassName); |
79
|
|
|
|
80
|
|
|
/** @var $fieldType FieldType */ |
81
|
|
|
$fieldType = $this->create($fieldType); |
82
|
|
|
return $fieldType; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* {@inheritdoc} |
87
|
|
|
*/ |
88
|
|
|
public function readByType(Type $type): FieldTypeInterface |
89
|
|
|
{ |
90
|
|
|
$fieldTypeRepository = $this->entityManager->getRepository(FieldType::class); |
91
|
|
|
/** @var $fieldType FieldType */ |
92
|
|
|
$fieldType = $fieldTypeRepository->findOneBy([ |
93
|
|
|
'type' => (string) $type |
94
|
|
|
]); |
95
|
|
|
if (empty($fieldType)) { |
96
|
|
|
throw new FieldTypeNotFoundException(); |
97
|
|
|
} |
98
|
|
|
return $fieldType; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* {@inheritdoc} |
103
|
|
|
*/ |
104
|
|
|
public function readByFullyQualifiedClassName(FullyQualifiedClassName $fullyQualifiedClassName): FieldTypeInterface |
105
|
|
|
{ |
106
|
|
|
$fieldTypeRepository = $this->entityManager->getRepository(FieldType::class); |
107
|
|
|
/** @var $fieldType FieldType */ |
108
|
|
|
$fieldType = $fieldTypeRepository->findOneBy([ |
109
|
|
|
'fullyQualifiedClassName' => (string) $fullyQualifiedClassName |
110
|
|
|
]); |
111
|
|
|
if (empty($fieldType)) { |
112
|
|
|
throw new FieldTypeNotFoundException(); |
113
|
|
|
} |
114
|
|
|
return $fieldType; |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|