1 | <?php |
||
25 | class PersistenceManager |
||
26 | { |
||
27 | /** |
||
28 | * @var FormObject |
||
29 | */ |
||
30 | protected $formObject; |
||
31 | |||
32 | /** |
||
33 | * @var bool |
||
34 | */ |
||
35 | protected $initializationDone = false; |
||
36 | |||
37 | /** |
||
38 | * @param FormObject $formObject |
||
39 | */ |
||
40 | public function __construct(FormObject $formObject) |
||
41 | { |
||
42 | $this->formObject = $formObject; |
||
43 | } |
||
44 | |||
45 | /** |
||
46 | * Loops on the registered persistence services and saves the given form |
||
47 | * instance in each one. |
||
48 | */ |
||
49 | public function save() |
||
50 | { |
||
51 | $this->initializePersistence(); |
||
52 | |||
53 | $form = $this->formObject->getForm(); |
||
54 | $metadata = $this->formObject->getFormMetadata(); |
||
55 | $identifier = $metadata->getIdentifier(); |
||
56 | $persistenceServices = $this->getSortedPersistenceServices(); |
||
57 | |||
58 | foreach ($persistenceServices as $persistence) { |
||
59 | $persistence->save($metadata, $form); |
||
60 | } |
||
61 | |||
62 | if (count($persistenceServices) > 0) { |
||
63 | $proxy = FormObjectFactory::get()->getProxy($form); |
||
64 | $proxy->markFormAsPersistent(); |
||
65 | } |
||
66 | |||
67 | /* |
||
68 | * If the form identifier has changed during the saving process (for |
||
69 | * instance the form has been saved in database and has a new uid), the |
||
70 | * metadata is persisted to be sure the form identifier is saved in |
||
71 | * database. |
||
72 | */ |
||
73 | if ($identifier !== $metadata->getIdentifier()) { |
||
74 | $metadata->commit(); |
||
75 | } |
||
76 | } |
||
77 | |||
78 | /** |
||
79 | * Loops on the registered persistence services, and tries to fetch the |
||
80 | * form. If a form is found, it is returned and the loop breaks. If not form |
||
81 | * is found, `null` is returned. |
||
82 | * |
||
83 | * @param FormMetadata $metadata |
||
84 | * @return FormInterface|null |
||
85 | * @throws InvalidEntryException |
||
86 | */ |
||
87 | public function fetchFirst(FormMetadata $metadata) |
||
88 | { |
||
89 | $this->initializePersistence(); |
||
90 | |||
91 | foreach ($this->getSortedPersistenceServices() as $persistence) { |
||
92 | if ($persistence->has($metadata)) { |
||
93 | $form = $persistence->fetch($metadata); |
||
94 | |||
95 | if (false === $form instanceof FormInterface) { |
||
96 | throw InvalidEntryException::persistenceInvalidEntryFetched($persistence, $form); |
||
97 | } |
||
98 | |||
99 | $this->formObject->setForm($form); |
||
100 | |||
101 | $proxy = FormObjectFactory::get()->getProxy($form); |
||
102 | $proxy->setFormHash($metadata->getHash()); |
||
103 | $proxy->markFormAsPersistent(); |
||
104 | |||
105 | return $form; |
||
106 | } |
||
107 | } |
||
108 | |||
109 | return null; |
||
110 | } |
||
111 | |||
112 | /** |
||
113 | * Loops on persistence services and initializes them. |
||
114 | */ |
||
115 | protected function initializePersistence() |
||
116 | { |
||
117 | if (false === $this->initializationDone) { |
||
118 | $this->initializationDone = true; |
||
119 | |||
120 | foreach ($this->formObject->getDefinition()->getPersistence() as $persistence) { |
||
121 | $persistence->initialize(); |
||
122 | } |
||
123 | } |
||
124 | } |
||
125 | |||
126 | /** |
||
127 | * Sorts the persistence services, based on their priority property: the |
||
128 | * ones with the highest priority will come first. |
||
129 | * |
||
130 | * @return PersistenceInterface[] |
||
131 | */ |
||
132 | protected function getSortedPersistenceServices() |
||
149 | } |
||
150 |