1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* 2017 Romain CANON <[email protected]> |
4
|
|
|
* |
5
|
|
|
* This file is part of the TYPO3 FormZ project. |
6
|
|
|
* It is free software; you can redistribute it and/or modify it |
7
|
|
|
* under the terms of the GNU General Public License, either |
8
|
|
|
* version 3 of the License, or any later version. |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, see: |
11
|
|
|
* http://www.gnu.org/licenses/gpl-3.0.html |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Romm\Formz\Persistence\Item\Repository; |
15
|
|
|
|
16
|
|
|
use Romm\Formz\Core\Core; |
17
|
|
|
use Romm\Formz\Exceptions\InvalidArgumentTypeException; |
18
|
|
|
use Romm\Formz\Form\FormInterface; |
19
|
|
|
use Romm\Formz\Form\Service\DataObject\FormIdentifierObject; |
20
|
|
|
use Romm\Formz\Persistence\AbstractPersistence; |
21
|
|
|
use TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface; |
22
|
|
|
use TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager; |
23
|
|
|
use TYPO3\CMS\Extbase\Persistence\RepositoryInterface; |
24
|
|
|
|
25
|
|
|
class RepositoryPersistence extends AbstractPersistence |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* @var \Romm\Formz\Persistence\Item\Repository\RepositoryPersistenceOption |
29
|
|
|
*/ |
30
|
|
|
protected $options; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var RepositoryInterface |
34
|
|
|
*/ |
35
|
|
|
protected $repository; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var PersistenceManager |
39
|
|
|
*/ |
40
|
|
|
protected $persistenceManager; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Creates the repository instance. |
44
|
|
|
*/ |
45
|
|
|
public function initialize() |
46
|
|
|
{ |
47
|
|
|
$this->repository = Core::instantiate($this->options->getRepositoryClassName()); |
48
|
|
|
$this->persistenceManager = Core::instantiate(PersistenceManager::class); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Checks that the form instance that matches the identifier exists in the |
53
|
|
|
* database. |
54
|
|
|
* |
55
|
|
|
* @param FormIdentifierObject $identifierObject |
56
|
|
|
* @return bool |
57
|
|
|
*/ |
58
|
|
|
public function has(FormIdentifierObject $identifierObject) |
59
|
|
|
{ |
60
|
|
|
$object = $identifierObject->hasIdentifier() |
61
|
|
|
? $this->repository->findByUid($identifierObject->getIdentifier()) |
62
|
|
|
: null; |
63
|
|
|
|
64
|
|
|
return $object !== null; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Returns the form instance that matches the identifier. If it does not |
69
|
|
|
* exist, and exception is thrown. |
70
|
|
|
* |
71
|
|
|
* @param FormIdentifierObject $identifierObject |
72
|
|
|
* @return FormInterface |
73
|
|
|
*/ |
74
|
|
|
public function fetch(FormIdentifierObject $identifierObject) |
75
|
|
|
{ |
76
|
|
|
$this->checkInstanceCanBeFetched($identifierObject); |
77
|
|
|
|
78
|
|
|
/** @var FormInterface $object */ |
79
|
|
|
$object = $this->repository->findByUid($identifierObject->getIdentifier()); |
80
|
|
|
|
81
|
|
|
return $object; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Saves the form instance in the database. |
86
|
|
|
* |
87
|
|
|
* If the given form instance is not an instance of `DomainObjectInterface`, |
88
|
|
|
* an exception is thrown. |
89
|
|
|
* |
90
|
|
|
* @param FormIdentifierObject $identifierObject |
91
|
|
|
* @param FormInterface $form |
92
|
|
|
* @throws InvalidArgumentTypeException |
93
|
|
|
*/ |
94
|
|
|
public function save(FormIdentifierObject $identifierObject, FormInterface $form) |
95
|
|
|
{ |
96
|
|
|
if (false === $form instanceof DomainObjectInterface) { |
97
|
|
|
throw InvalidArgumentTypeException::persistenceRepositoryWrongFormType($form); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** @var DomainObjectInterface $form */ |
101
|
|
|
if (null === $form->getUid()) { |
102
|
|
|
$this->repository->add($form); |
103
|
|
|
$identifierObject->invalidateIdentifierHash(); |
104
|
|
|
} else { |
105
|
|
|
$this->repository->update($form); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
$this->persistenceManager->persistAll(); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Removes the given entry from database. |
113
|
|
|
* |
114
|
|
|
* @param FormIdentifierObject $identifierObject |
115
|
|
|
*/ |
116
|
|
|
public function delete(FormIdentifierObject $identifierObject) |
117
|
|
|
{ |
118
|
|
|
if ($this->has($identifierObject)) { |
119
|
|
|
$this->repository->remove($this->fetch($identifierObject)); |
120
|
|
|
|
121
|
|
|
$this->persistenceManager->persistAll(); |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|