XmlEventDataTransformer::transform()   B
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 33
Code Lines 21

Duplication

Lines 5
Ratio 15.15 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 21
nc 4
nop 4
dl 5
loc 33
rs 8.8571
c 1
b 0
f 0
1
<?php
2
/**
3
 * This file is part of the Simple EventStore Manager package.
4
 *
5
 * (c) Mauro Cassani<https://github.com/mauretto78>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace SimpleEventStoreManager\Infrastructure\DataTransformers;
12
13
use SimpleEventStoreManager\Infrastructure\DataTransformers\Contracts\DataTransformerInterface;
14
use SimpleEventStoreManager\Infrastructure\DataTransformers\Representations\EventCollectionObjectRepresentation;
15
use SimpleEventStoreManager\Infrastructure\DataTransformers\Representations\EventObjectRepresentation;
16
use Symfony\Component\HttpFoundation\Response;
17
18
class XmlEventDataTransformer extends AbstractEventDataTransformer implements DataTransformerInterface
19
{
20
    /**
21
     * @param array $events
22
     * @param int $eventsCount
23
     * @param int $page
24
     * @param int $maxPerPage
25
     *
26
     * @return Response
27
     */
28
    public function transform($events, $eventsCount, $page, $maxPerPage)
29
    {
30
        $pageCount = count($events);
31
        $eventsCollection = new EventCollectionObjectRepresentation(
32
            (int) $page,
33
            (int) $maxPerPage,
34
            (int) $numberOfPages = ceil($eventsCount/$maxPerPage),
35
            (int) $eventsCount,
36
            $this->calculateLinks($page, $numberOfPages)
0 ignored issues
show
Bug introduced by
$numberOfPages of type double is incompatible with the type integer expected by parameter $numberOfPages of SimpleEventStoreManager\...ormer::calculateLinks(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

36
            $this->calculateLinks($page, /** @scrutinizer ignore-type */ $numberOfPages)
Loading history...
37
        );
38
        foreach ($events as $event) {
39
            $eventsCollection->addEvent(new EventObjectRepresentation($event));
40
        }
41
42
        $response = new Response(
43
            $this->serializer->serialize(
44
                [
45
                    $eventsCollection
46
                ], 'xml'),
47
            $this->getHttpStatusCode($pageCount)
48
        );
49
50
        $response->headers->set('Content-type', 'text/xml');
51
        $response->setCharset('utf-8');
52
53
        // set infinite cache if page is complete
54 View Code Duplication
        if ($maxPerPage === $pageCount) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
55
            $response
56
                ->setMaxAge(60 * 60 * 24 * 365)
57
                ->setSharedMaxAge(60 * 60 * 24 * 365);
58
        }
59
60
        return $response;
61
    }
62
}
63