EventRepresentation   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 57
rs 10
c 0
b 0
f 0
wmc 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A paginateAggregate() 0 3 1
A __construct() 0 7 1
A aggregate() 0 7 1
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\Application\Event;
12
13
use SimpleEventStoreManager\Infrastructure\DataTransformers\Contracts\DataTransformerInterface;
14
use Symfony\Component\HttpFoundation\Response;
15
16
class EventRepresentation
17
{
18
    /**
19
     * @var DataTransformerInterface
20
     */
21
    private $dataTransformer;
22
23
    /**
24
     * @var EventManager
25
     */
26
    private $eventManger;
27
28
    /**
29
     * @var EventQuery
30
     */
31
    private $eventQuery;
32
33
    /**
34
     * EventRepresentation constructor.
35
     * @param EventManager $eventManger
36
     * @param DataTransformerInterface $dataTransformer
37
     */
38
    public function __construct(
39
        EventManager $eventManger,
40
        DataTransformerInterface $dataTransformer
41
    ) {
42
        $this->eventManger = $eventManger;
43
        $this->eventQuery = new EventQuery($eventManger);
44
        $this->dataTransformer = $dataTransformer;
45
    }
46
47
    /**
48
     * @param int $page
49
     * @param int $maxPerPage
50
     *
51
     * @return Response
52
     */
53
    public function aggregate($uuid, $page = 1, $maxPerPage = 25)
54
    {
55
        return $this->dataTransformer->transform(
56
            $this->paginateAggregate($this->eventQuery->fromAggregate($uuid), $page, $maxPerPage),
57
            $this->eventQuery->streamCount($uuid),
58
            $page,
59
            $maxPerPage
60
        );
61
    }
62
63
    /**
64
     * @param $array
65
     * @param $page
66
     * @param $maxPerPage
67
     *
68
     * @return array
69
     */
70
    private function paginateAggregate($array, $page, $maxPerPage)
71
    {
72
        return array_slice($array, ($page - 1) * $maxPerPage, $maxPerPage);
73
    }
74
}
75