AbstractImagesEvent   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 3
dl 0
loc 56
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getImages() 0 4 1
A serialize() 0 18 2
A deserialize() 0 18 2
1
<?php
2
3
namespace CultuurNet\UDB3\Offer\Events\Image;
4
5
use CultuurNet\UDB3\Media\Image;
6
use CultuurNet\UDB3\Media\ImageCollection;
7
use CultuurNet\UDB3\Offer\Events\AbstractEvent;
8
9
abstract class AbstractImagesEvent extends AbstractEvent
10
{
11
    /**
12
     * @var ImageCollection
13
     */
14
    protected $images;
15
16
    final public function __construct(string $eventId, ImageCollection $images)
17
    {
18
        parent::__construct($eventId);
19
        $this->images = $images;
20
    }
21
22
    public function getImages(): ImageCollection
23
    {
24
        return $this->images;
25
    }
26
27
    public function serialize(): array
28
    {
29
        $serializedData =  parent::serialize() + array(
30
            'images' => array_map(
31
                function (Image $image) {
32
                    return $image->serialize();
33
                },
34
                $this->images->toArray()
35
            ),
36
        );
37
38
        $mainImage = $this->images->getMain();
39
        if ($mainImage) {
40
            $serializedData[] = $mainImage->serialize();
41
        }
42
43
        return $serializedData;
44
    }
45
46
    public static function deserialize(array $data): AbstractImagesEvent
47
    {
48
        $images = ImageCollection::fromArray(
49
            array_map(
50
                function ($imageData) {
51
                    return Image::deserialize($imageData);
52
                },
53
                $data['images']
54
            )
55
        );
56
57
        return new static(
58
            $data['item_id'],
59
            isset($data['main_image'])
60
                ? $images->withMain(Image::deserialize($data['main_image']))
61
                : $images
62
        );
63
    }
64
}
65