Code Duplication    Length = 32-32 lines in 2 locations

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

@@ 435-466 (lines=32) @@
432
     * @param AbstractLabelRemoved $labelRemoved
433
     * @return JsonDocument
434
     */
435
    protected function applyLabelRemoved(AbstractLabelRemoved $labelRemoved)
436
    {
437
        $document = $this->loadDocumentFromRepository($labelRemoved);
438
439
        $offerLd = $document->getBody();
440
441
        // Don't presume that the label visibility is correct when removing.
442
        // So iterate over both the visible and invisible labels.
443
        $labelsProperties = ['labels', 'hiddenLabels'];
444
445
        foreach ($labelsProperties as $labelsProperty) {
446
            if (isset($offerLd->{$labelsProperty}) && is_array($offerLd->{$labelsProperty})) {
447
                $offerLd->{$labelsProperty} = array_filter(
448
                    $offerLd->{$labelsProperty},
449
                    function ($label) use ($labelRemoved) {
450
                        return !$labelRemoved->getLabel()->equals(
451
                            new Label($label)
452
                        );
453
                    }
454
                );
455
                // Ensure array keys start with 0 so json_encode() does encode it
456
                // as an array and not as an object.
457
                if (count($offerLd->{$labelsProperty}) > 0) {
458
                    $offerLd->{$labelsProperty} = array_values($offerLd->{$labelsProperty});
459
                } else {
460
                    unset($offerLd->{$labelsProperty});
461
                }
462
            }
463
        }
464
465
        return $document->withBody($offerLd);
466
    }
467
468
    /**
469
     * Apply the imageAdded event to the item repository.

src/Organizer/OrganizerLDProjector.php 1 location

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