Completed
Push — middleware-wip-tmp ( d8f2e1 )
by Romain
02:50
created

PersistenceManager::fetchFirst()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 22
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 22
rs 8.9197
c 0
b 0
f 0
cc 4
eloc 11
nc 4
nop 0
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;
15
16
use Romm\Formz\Exceptions\InvalidEntryException;
17
use Romm\Formz\Form\FormInterface;
18
use Romm\Formz\Form\FormObject\FormObject;
19
20
/**
21
 * Manages persistence for a given form object.
22
 */
23
class PersistenceManager
24
{
25
    /**
26
     * @var FormObject
27
     */
28
    protected $formObject;
29
30
    /**
31
     * @var bool
32
     */
33
    protected $formIsPersistent = false;
34
35
    /**
36
     * @var bool
37
     */
38
    protected $initializationDone = false;
39
40
    /**
41
     * @param FormObject $formObject
42
     */
43
    public function __construct(FormObject $formObject)
44
    {
45
        $this->formObject = $formObject;
46
    }
47
48
    /**
49
     * Loops on the registered persistence services and saves the given form
50
     * instance in each one.
51
     *
52
     * @param FormInterface $form
53
     */
54
    public function save(FormInterface $form)
55
    {
56
        $this->initializePersistence();
57
58
59
        $metadata = $this->formObject->getMetadata();
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.
75
         */
76
        if ($identifier !== $metadata->getIdentifier()) {
77
            $metadata->commit();
78
        }
79
    }
80
81
    /**
82
     * Loops on the registered persistence services, and tries to fetch the
83
     * form. If a form is found, it is returned and the loop breaks. If not form
84
     * is found, `null` is returned.
85
     *
86
     * @return FormInterface|null
87
     * @throws InvalidEntryException
88
     */
89
    public function fetchFirst()
90
    {
91
        $this->initializePersistence();
92
93
        $metadata = $this->formObject->getMetadata();
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()
116
    {
117
        if (false === $this->initializationDone) {
118
            $this->initializationDone = true;
119
120
            foreach ($this->formObject->getConfiguration()->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()
133
    {
134
        $items = $this->formObject->getConfiguration()->getPersistence();
135
136
        usort($items, function (PersistenceInterface $a, PersistenceInterface $b) {
137
            $priorityA = (int)$a->getPriority();
138
            $priorityB = (int)$b->getPriority();
139
140
            if ($priorityA === $priorityB) {
141
                return 0;
142
            }
143
144
            return $priorityA < $priorityB ? 1 : -1;
145
        });
146
147
        return $items;
148
    }
149
150
    /**
151
     * @return bool
152
     */
153
    public function formIsPersistent()
154
    {
155
        return $this->formIsPersistent;
156
    }
157
}
158