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

@@ 290-322 (lines=33) @@
287
    /**
288
     * @param LabelRemoved $labelRemoved
289
     */
290
    public function applyLabelRemoved(LabelRemoved $labelRemoved)
291
    {
292
        $document = $this->repository->get($labelRemoved->getOrganizerId());
293
        $jsonLD = $document->getBody();
294
295
        // Don't presume that the label visibility is correct when removing.
296
        // So iterate over both the visible and invisible labels.
297
        $labelsProperties = ['labels', 'hiddenLabels'];
298
299
        foreach ($labelsProperties as $labelsProperty) {
300
            if (isset($jsonLD->{$labelsProperty}) && is_array($jsonLD->{$labelsProperty})) {
301
                $jsonLD->{$labelsProperty} = array_filter(
302
                    $jsonLD->{$labelsProperty},
303
                    function ($label) use ($labelRemoved) {
304
                        return !$labelRemoved->getLabel()->equals(
305
                            new Label($label)
306
                        );
307
                    }
308
                );
309
310
                // Ensure array keys start with 0 so json_encode() does encode it
311
                // as an array and not as an object.
312
                if (count($jsonLD->{$labelsProperty}) > 0) {
313
                    $jsonLD->{$labelsProperty} = array_values($jsonLD->{$labelsProperty});
314
                } else {
315
                    unset($jsonLD->{$labelsProperty});
316
                }
317
            }
318
        }
319
320
        $this->repository->save($document->withBody($jsonLD));
321
    }
322
323
    /**
324
     * @param OrganizerDeleted $organizerDeleted
325
     */