Passed
Push — master ( 921011...5d9034 )
by Petr
04:49
created

EventExtractor::generateImageUrl()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 2
crap 1
1
<?php
2
3
namespace AppBundle\Service\Extractor;
4
5
use AppBundle\Entity\Event;
6
use AppBundle\Entity\Image;
7
use AppBundle\Entity\Link;
8
use AppBundle\Exception\UnsupportedEntityException;
9
use AppBundle\Service\Extractor\Infrastructure\AbstractExtractor;
10
11
/**
12
 * @author Vehsamrak
13
 */
14
class EventExtractor extends AbstractExtractor
15
{
16
17
    /**
18
     * @param Event $entity
19
     * @throws UnsupportedEntityException
20
     */
21 9
    public function extract($entity): array
22
    {
23 9
        if ($entity instanceof Event) {
24
            $serializedData = [
25 9
                'id'          => $entity->getId(),
26 9
                'date'        => $entity->getDate(),
27 9
                'name'        => $entity->getName(),
28 9
                'description' => $entity->getDescription(),
29 9
                'creator'     => $entity->getCreatorLogin(),
30 9
                'images'      => $this->getImages($entity),
31 9
                'links'       => $this->getLinks($entity),
32
            ];
33
        } else {
34
            throw new UnsupportedEntityException();
35
        }
36
37 9
        return $serializedData;
38
    }
39
40
    /**
41
     * @return string[]
42
     */
43 9
    private function getImages(Event $event): array
44
    {
45 9
        $images = $event->getImages();
46 9
        $eventId = $event->getId();
47
48 9
        return array_map(
49
            function (Image $image) use ($eventId) {
50 7
                return $this->generateImageUrl($eventId, $image->getName());
51 9
            },
52 9
            $images->toArray()
53
        );
54
    }
55
56 9
    private function getLinks(Event $event): array
57
    {
58 9
        return array_map(
59 9
            function (Link $link) {
60
                return [
61 7
                    'id'          => $link->getId(),
62 7
                    'url'         => $link->getUrl(),
63 7
                    'description' => $link->getDescription(),
64
                ];
65 9
            },
66 9
            $event->getLinks()->toArray()
67
        );
68
    }
69
70 7
    private function generateImageUrl(string $eventId, string $imageName): string
71
    {
72 7
        return $this->generateUrl(
73 7
            'event_image_view',
74
            [
75 7
                'eventId'   => $eventId,
76 7
                'imageName' => $imageName,
77
            ]
78
        );
79
    }
80
}
81