1 | <?php |
||
24 | class PersistenceManager |
||
25 | { |
||
26 | /** |
||
27 | * @var FormObject |
||
28 | */ |
||
29 | protected $formObject; |
||
30 | |||
31 | /** |
||
32 | * @var bool |
||
33 | */ |
||
34 | protected $formIsPersistent = false; |
||
35 | |||
36 | /** |
||
37 | * @var bool |
||
38 | */ |
||
39 | protected $initializationDone = false; |
||
40 | |||
41 | /** |
||
42 | * @param FormObject $formObject |
||
43 | */ |
||
44 | public function __construct(FormObject $formObject) |
||
45 | { |
||
46 | $this->formObject = $formObject; |
||
47 | } |
||
48 | |||
49 | /** |
||
50 | * Loops on the registered persistence services and saves the given form |
||
51 | * instance in each one. |
||
52 | * |
||
53 | * @param FormInterface $form |
||
54 | */ |
||
55 | public function save(FormInterface $form) |
||
56 | { |
||
57 | $this->initializePersistence(); |
||
58 | |||
59 | $metadata = $this->formObject->getFormMetadata(); |
||
60 | $identifier = $metadata->getIdentifier(); |
||
61 | $persistenceServices = $this->getSortedPersistenceServices(); |
||
62 | |||
63 | foreach ($persistenceServices as $persistence) { |
||
64 | $persistence->save($metadata, $form); |
||
65 | } |
||
66 | |||
67 | if (count($persistenceServices) > 0) { |
||
68 | $this->formIsPersistent = true; |
||
69 | } |
||
70 | |||
71 | /* |
||
72 | * If the form identifier has changed during the saving process (for |
||
73 | * instance the form has been saved in database and has a new uid), the |
||
74 | * metadata is persisted to be sure the form identifier is saved in |
||
75 | * database. |
||
76 | */ |
||
77 | if ($identifier !== $metadata->getIdentifier()) { |
||
78 | $metadata->commit(); |
||
79 | } |
||
80 | } |
||
81 | |||
82 | /** |
||
83 | * Loops on the registered persistence services, and tries to fetch the |
||
84 | * form. If a form is found, it is returned and the loop breaks. If not form |
||
85 | * is found, `null` is returned. |
||
86 | * |
||
87 | * @param FormMetadata $metadata |
||
88 | * @return FormInterface|null |
||
89 | * @throws InvalidEntryException |
||
90 | */ |
||
91 | public function fetchFirst(FormMetadata $metadata) |
||
92 | { |
||
93 | $this->initializePersistence(); |
||
94 | |||
95 | foreach ($this->getSortedPersistenceServices() as $persistence) { |
||
96 | if ($persistence->has($metadata)) { |
||
97 | $form = $persistence->fetch($metadata); |
||
98 | |||
99 | if (false === $form instanceof FormInterface) { |
||
100 | throw InvalidEntryException::persistenceInvalidEntryFetched($persistence, $form); |
||
101 | } |
||
102 | |||
103 | $this->formIsPersistent = true; |
||
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() |
||
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 | /** |
||
151 | * @return bool |
||
152 | */ |
||
153 | public function formIsPersistent() |
||
157 | } |
||
158 |