xref: /plugin/combo/ComboStrap/MetadataStoreTransfer.php (revision 4cadd4f8c541149bdda95f080e38a6d4e3a640ca)
1<?php
2
3
4namespace ComboStrap;
5
6
7class MetadataStoreTransfer
8{
9
10    /**
11     * @var MetadataStore
12     */
13    private $sourceStore;
14    /**
15     * @var MetadataStore
16     */
17    private $targetStore;
18
19    /**
20     * @var Page
21     */
22    private $page;
23    /**
24     * @var array - the normalized data after processing
25     * Name of metadata may be deprecated
26     * After processing, this array will have the new keys
27     * Use in a frontmatter to send correct data to the rendering metadata phase
28     */
29    private $normalizedData;
30    /**
31     * @var array - the processing messages
32     */
33    private $messages;
34
35    /**
36     * MetadataStoreTransfer constructor.
37     * @param Page $resource
38     */
39    public function __construct(ResourceCombo $resource)
40    {
41        $this->page = $resource;
42    }
43
44
45    public static function createForPage($page): MetadataStoreTransfer
46    {
47        return new MetadataStoreTransfer($page);
48    }
49
50    public function fromStore(MetadataStore $sourceStore): MetadataStoreTransfer
51    {
52        $this->sourceStore = $sourceStore;
53        return $this;
54    }
55
56    public function toStore(MetadataStore $targetStore): MetadataStoreTransfer
57    {
58        $this->targetStore = $targetStore;
59        return $this;
60    }
61
62    /**
63     * @param array $data
64     * @return $this
65     */
66    public function process(array $data): MetadataStoreTransfer
67    {
68        $messages = [];
69
70        /**
71         * Pre-processing
72         * Check/ validity and list of metadata building
73         */
74        $metadatas = [];
75        foreach ($data as $persistentName => $value) {
76
77
78            $metadata = Metadata::getForName($persistentName);
79
80            /**
81             * Take old name or renaming into account
82             *
83             * ie The old key should be replace by the new one
84             * (ie {@link \ComboStrap\PagePublicationDate::OLD_META_KEY}
85             * by {@link \ComboStrap\PagePublicationDate::PROPERTY_NAME}
86             */
87            $name = $persistentName;
88            if ($metadata !== null) {
89                $name = $metadata::getName();
90                $persistentName = $metadata::getPersistentName();
91            }
92            $this->normalizedData[$persistentName] = $value;
93
94            /**
95             * Not modifiable meta check
96             */
97            if (in_array($name, Metadata::NOT_MODIFIABLE_METAS)) {
98                $messages[] = Message::createWarningMessage("The metadata ($name) is a protected metadata and cannot be modified")
99                    ->setCanonical(Metadata::CANONICAL);
100                continue;
101            }
102
103            /**
104             * Unknown meta
105             */
106            if ($metadata === null) {
107                $this->targetStore->setFromPersistentName($persistentName, $value);
108                continue;
109            }
110
111            /**
112             * Valid meta to proceed in the next phase
113             */
114            $metadatas[$name] = $metadata;
115
116        }
117
118
119        foreach ($metadatas as $metadata) {
120
121
122            /**
123             * Persistent ?
124             */
125            if ($metadata->getPersistenceType() !== Metadata::PERSISTENT_METADATA) {
126                $messages[] = Message::createWarningMessage("The metadata ({$metadata->getName()}) is not persistent and cannot be modified")
127                    ->setCanonical($metadata->getCanonical());
128                continue;
129            }
130
131            /**
132             * Sync
133             */
134            try {
135                $metadata
136                    ->setResource($this->page)
137                    ->setReadStore($this->sourceStore)
138                    ->buildFromReadStore()
139                    ->setWriteStore($this->targetStore)
140                    ->sendToWriteStore();
141            } catch (ExceptionCombo $e) {
142                $messages[] = Message::createErrorMessage("Error while replicating the meta ($metadata) from the store ($this->sourceStore) to the store ($this->targetStore). Message: " . $e->getMessage())
143                    ->setCanonical($metadata->getCanonical());
144            }
145        }
146        $this->targetStore->persist();
147
148        $this->messages = $messages;
149        return $this;
150    }
151
152    /**
153     * @return Message[]
154     */
155    public function getMessages(): array
156    {
157        return $this->messages;
158    }
159
160    public function getNormalizedDataArray(): array
161    {
162        return $this->normalizedData;
163    }
164
165
166}
167