JsonEventDataTransformer::transform()   B
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 30
Code Lines 19

Duplication

Lines 5
Ratio 16.67 %

Importance

Changes 0
Metric Value
cc 3
eloc 19
nc 2
nop 4
dl 5
loc 30
rs 8.8571
c 0
b 0
f 0
1
<?php
2
/**
3
 * This file is part of the EventStoreManager 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 Symfony\Component\HttpFoundation\JsonResponse;
15
use Symfony\Component\HttpFoundation\Request;
16
use Symfony\Component\HttpFoundation\Response;
17
use JMS\Serializer\Serializer;
18
19
class JsonEventDataTransformer extends AbstractEventDataTransformer implements DataTransformerInterface
20
{
21
    /**
22
     * @param array $events
23
     * @param int $eventsCount
24
     * @param int $page
25
     * @param int $maxPerPage
26
     *
27
     * @return Response
28
     */
29
    public function transform($events, $eventsCount, $page, $maxPerPage)
30
    {
31
        $pageCount = count($events);
32
        $jsonResponse = new JsonResponse(
33
            $this->serializer->serialize(
34
                [
35
                    '_meta' => [
36
                        'page' => $page,
37
                        'records_per_page' => $maxPerPage,
38
                        'total_pages' => $numberOfPages = ceil($eventsCount/$maxPerPage),
39
                        'total_count' => $eventsCount
40
                    ],
41
                    '_links' => [
42
                        $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

42
                        $this->calculateLinks($page, /** @scrutinizer ignore-type */ $numberOfPages)
Loading history...
43
                    ],
44
                    'events' => (is_array($events)) ? $events : $this->convertEventsDataToArray($events)
45
                ], 'json'),
46
            $this->getHttpStatusCode($pageCount),
47
            [],
48
            true
49
        );
50
51
        // set infinite cache if page is complete
52 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...
53
            $jsonResponse
54
                ->setMaxAge(60 * 60 * 24 * 365)
55
                ->setSharedMaxAge(60 * 60 * 24 * 365);
56
        }
57
58
        return $jsonResponse;
59
    }
60
}
61