Code Duplication    Length = 32-33 lines in 2 locations

src/Offer/ReadModel/JSONLD/OfferLDProjector.php 1 location

@@ 279-310 (lines=32) @@
276
    /**
277
     * @param AbstractLabelRemoved $labelRemoved
278
     */
279
    protected function applyLabelRemoved(AbstractLabelRemoved $labelRemoved)
280
    {
281
        $document = $this->loadDocumentFromRepository($labelRemoved);
282
283
        $offerLd = $document->getBody();
284
285
        // Don't presume that the label visibility is correct when removing.
286
        // So iterate over both the visible and invisible labels.
287
        $labelsProperties = ['labels', 'hiddenLabels'];
288
289
        foreach ($labelsProperties as $labelsProperty) {
290
            if (isset($offerLd->{$labelsProperty}) && is_array($offerLd->{$labelsProperty})) {
291
                $offerLd->{$labelsProperty} = array_filter(
292
                    $offerLd->{$labelsProperty},
293
                    function ($label) use ($labelRemoved) {
294
                        return !$labelRemoved->getLabel()->equals(
295
                            new Label($label)
296
                        );
297
                    }
298
                );
299
                // Ensure array keys start with 0 so json_encode() does encode it
300
                // as an array and not as an object.
301
                if (count($offerLd->{$labelsProperty}) > 0) {
302
                    $offerLd->{$labelsProperty} = array_values($offerLd->{$labelsProperty});
303
                } else {
304
                    unset($offerLd->{$labelsProperty});
305
                }
306
            }
307
        }
308
309
        $this->repository->save($document->withBody($offerLd));
310
    }
311
312
    /**
313
     * Apply the imageAdded event to the item repository.

src/Organizer/OrganizerLDProjector.php 1 location

@@ 275-307 (lines=33) @@
272
    /**
273
     * @param LabelRemoved $labelRemoved
274
     */
275
    public function applyLabelRemoved(LabelRemoved $labelRemoved)
276
    {
277
        $document = $this->repository->get($labelRemoved->getOrganizerId());
278
        $jsonLD = $document->getBody();
279
280
        // Don't presume that the label visibility is correct when removing.
281
        // So iterate over both the visible and invisible labels.
282
        $labelsProperties = ['labels', 'hiddenLabels'];
283
284
        foreach ($labelsProperties as $labelsProperty) {
285
            if (isset($jsonLD->{$labelsProperty}) && is_array($jsonLD->{$labelsProperty})) {
286
                $jsonLD->{$labelsProperty} = array_filter(
287
                    $jsonLD->{$labelsProperty},
288
                    function ($label) use ($labelRemoved) {
289
                        return !$labelRemoved->getLabel()->equals(
290
                            new Label($label)
291
                        );
292
                    }
293
                );
294
295
                // Ensure array keys start with 0 so json_encode() does encode it
296
                // as an array and not as an object.
297
                if (count($jsonLD->{$labelsProperty}) > 0) {
298
                    $jsonLD->{$labelsProperty} = array_values($jsonLD->{$labelsProperty});
299
                } else {
300
                    unset($jsonLD->{$labelsProperty});
301
                }
302
            }
303
        }
304
305
        $this->repository->save($document->withBody($jsonLD));
306
    }
307
308
    /**
309
     * @param OrganizerDeleted $organizerDeleted
310
     */