Passed
Push — master ( e008f3...ac9d28 )
by Petr
03:49
created

EventExtractor   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 92%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 4
dl 0
loc 57
ccs 23
cts 25
cp 0.92
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A extract() 0 17 2
A getImages() 0 17 2
A generateImageUrl() 0 10 1
1
<?php
2
3
namespace AppBundle\Service\Extractor;
4
5
use AppBundle\Entity\Event;
6
use AppBundle\Entity\Image;
7
use AppBundle\Exception\UnsupportedEntityException;
8
use AppBundle\Service\Extractor\Infrastructure\AbstractExtractor;
9
10
/**
11
 * @author Vehsamrak
12
 */
13
class EventExtractor extends AbstractExtractor
14
{
15
16
    /**
17
     * @param Event $entity
18
     * @throws UnsupportedEntityException
19
     */
20 6
    public function extract($entity): array
21
    {
22 6
        if ($entity instanceof Event) {
23
            $serializedData = [
24 6
                'id'          => $entity->getId(),
25 6
                'date'        => $entity->getDate(),
26 6
                'name'        => $entity->getName(),
27 6
                'description' => $entity->getDescription(),
28 6
                'creator'     => $entity->getCreatorLogin(),
29 6
                'images'      => $this->getImages($entity),
30
            ];
31
        } else {
32
            throw new UnsupportedEntityException();
33
        }
34
35 6
        return $serializedData;
36
    }
37
38
    /**
39
     * @return array|null
40
     */
41 6
    private function getImages(Event $event)
42
    {
43 6
        $images = $event->getImages();
44
45 6
        if ($images) {
46 6
            $eventId = $event->getId();
47
48 6
            return array_map(
49 6
                function (Image $image) use ($eventId) {
50 4
                    return $this->generateImageUrl($eventId, $image->getName());
51 6
                },
52 6
                $images->toArray()
53
            );
54
        } else {
55
            return null;
56
        }
57
    }
58
59 4
    private function generateImageUrl(string $eventId, string $imageName): string
60
    {
61 4
        return $this->generateUrl(
62 4
            'event_image_view',
63
            [
64 4
                'eventId'   => $eventId,
65 4
                'imageName' => $imageName,
66
            ]
67
        );
68
    }
69
}
70