Code Duplication    Length = 32-32 lines in 2 locations

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

@@ 307-338 (lines=32) @@
304
     * @param AbstractLabelRemoved $labelRemoved
305
     * @return JsonDocument
306
     */
307
    protected function applyLabelRemoved(AbstractLabelRemoved $labelRemoved)
308
    {
309
        $document = $this->loadDocumentFromRepository($labelRemoved);
310
311
        $offerLd = $document->getBody();
312
313
        // Don't presume that the label visibility is correct when removing.
314
        // So iterate over both the visible and invisible labels.
315
        $labelsProperties = ['labels', 'hiddenLabels'];
316
317
        foreach ($labelsProperties as $labelsProperty) {
318
            if (isset($offerLd->{$labelsProperty}) && is_array($offerLd->{$labelsProperty})) {
319
                $offerLd->{$labelsProperty} = array_filter(
320
                    $offerLd->{$labelsProperty},
321
                    function ($label) use ($labelRemoved) {
322
                        return !$labelRemoved->getLabel()->equals(
323
                            new Label($label)
324
                        );
325
                    }
326
                );
327
                // Ensure array keys start with 0 so json_encode() does encode it
328
                // as an array and not as an object.
329
                if (count($offerLd->{$labelsProperty}) > 0) {
330
                    $offerLd->{$labelsProperty} = array_values($offerLd->{$labelsProperty});
331
                } else {
332
                    unset($offerLd->{$labelsProperty});
333
                }
334
            }
335
        }
336
337
        return $document->withBody($offerLd);
338
    }
339
340
    /**
341
     * Apply the imageAdded event to the item repository.

src/Organizer/OrganizerLDProjector.php 1 location

@@ 365-396 (lines=32) @@
362
    /**
363
     * @param LabelRemoved $labelRemoved
364
     */
365
    private function applyLabelRemoved(LabelRemoved $labelRemoved)
366
    {
367
        $document = $this->repository->get($labelRemoved->getOrganizerId());
368
        $jsonLD = $document->getBody();
369
370
        // Don't presume that the label visibility is correct when removing.
371
        // So iterate over both the visible and invisible labels.
372
        $labelsProperties = ['labels', 'hiddenLabels'];
373
374
        foreach ($labelsProperties as $labelsProperty) {
375
            if (isset($jsonLD->{$labelsProperty}) && is_array($jsonLD->{$labelsProperty})) {
376
                $jsonLD->{$labelsProperty} = array_filter(
377
                    $jsonLD->{$labelsProperty},
378
                    function ($label) use ($labelRemoved) {
379
                        return !$labelRemoved->getLabel()->equals(
380
                            new Label($label)
381
                        );
382
                    }
383
                );
384
385
                // Ensure array keys start with 0 so json_encode() does encode it
386
                // as an array and not as an object.
387
                if (count($jsonLD->{$labelsProperty}) > 0) {
388
                    $jsonLD->{$labelsProperty} = array_values($jsonLD->{$labelsProperty});
389
                } else {
390
                    unset($jsonLD->{$labelsProperty});
391
                }
392
            }
393
        }
394
395
        return $document->withBody($jsonLD);
396
    }
397
398
    /**
399
     * @param OrganizerDeleted $organizerDeleted