YamlEventDataTransformer   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 41
Duplicated Lines 12.2 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 5
loc 41
rs 10
c 1
b 0
f 0
wmc 3

1 Method

Rating   Name   Duplication   Size   Complexity  
B transform() 5 31 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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 Symfony\Component\HttpFoundation\JsonResponse;
15
use Symfony\Component\HttpFoundation\Request;
16
use Symfony\Component\HttpFoundation\Response;
17
use JMS\Serializer\Serializer;
18
19
class YamlEventDataTransformer 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
        $response = new Response(
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
                ], 'yml'),
46
            $this->getHttpStatusCode($pageCount)
47
        );
48
49
        // set infinite cache if page is complete
50 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...
51
            $response
52
                ->setMaxAge(60 * 60 * 24 * 365)
53
                ->setSharedMaxAge(60 * 60 * 24 * 365);
54
        }
55
56
        $response->headers->set('Content-type', 'text/yaml');
57
        $response->setCharset('utf-8');
58
59
        return $response;
60
    }
61
}
62