Passed
Push — master ( eb296b...63b45b )
by Petr
03:53
created

EventExtractor::extract()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 18
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 2.003

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 18
ccs 10
cts 11
cp 0.9091
rs 9.4285
cc 2
eloc 13
nc 2
nop 1
crap 2.003
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 7
    public function extract($entity): array
22
    {
23 7
        if ($entity instanceof Event) {
24
            $serializedData = [
25 7
                'id'          => $entity->getId(),
26 7
                'date'        => $entity->getDate(),
27 7
                'name'        => $entity->getName(),
28 7
                'description' => $entity->getDescription(),
29 7
                'creator'     => $entity->getCreatorLogin(),
30 7
                'images'      => $this->getImages($entity),
31 7
                'links'       => $this->getLinks($entity),
32
            ];
33
        } else {
34
            throw new UnsupportedEntityException();
35
        }
36
37 7
        return $serializedData;
38
    }
39
40
    /**
41
     * @return string[]
42
     */
43 7
    private function getImages(Event $event): array
44
    {
45 7
        $images = $event->getImages();
46 7
        $eventId = $event->getId();
47
48 7
        return array_map(
49
            function (Image $image) use ($eventId) {
50 5
                return $this->generateImageUrl($eventId, $image->getName());
51 7
            },
52 7
            $images->toArray()
53
        );
54
    }
55
56 7
    private function getLinks(Event $event): array
57
    {
58 7
        return array_map(
59 7
            function (Link $link) {
60
                return [
61 5
                    'url'         => $link->getUrl(),
62 5
                    'description' => $link->getDescription(),
63
                ];
64 7
            },
65 7
            $event->getLinks()->toArray()
66
        );
67
    }
68
69 5
    private function generateImageUrl(string $eventId, string $imageName): string
70
    {
71 5
        return $this->generateUrl(
72 5
            'event_image_view',
73
            [
74 5
                'eventId'   => $eventId,
75 5
                'imageName' => $imageName,
76
            ]
77
        );
78
    }
79
}
80