Completed
Push — master ( 52cd2c...047880 )
by Kamil
47:26 queued 26:32
created

TemplateBlockDataCollector   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 2
dl 0
loc 50
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A collect() 0 4 1
A getRenderedEvents() 0 4 1
A getNumberOfRenderedEvents() 0 4 1
A getNumberOfRenderedBlocks() 0 6 1
A getTotalDuration() 0 6 1
A getName() 0 4 1
A reset() 0 4 1
1
<?php
2
3
/*
4
 * This file is part of the Sylius package.
5
 *
6
 * (c) Paweł Jędrzejewski
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace Sylius\Bundle\UiBundle\DataCollector;
15
16
use Symfony\Component\HttpFoundation\Request;
17
use Symfony\Component\HttpFoundation\Response;
18
use Symfony\Component\HttpKernel\DataCollector\DataCollector;
19
20
/**
21
 * @internal
22
 */
23
final class TemplateBlockDataCollector extends DataCollector
24
{
25
    /** @var TemplateBlockRenderingHistory */
26
    private $templateBlockRenderingHistory;
27
28
    public function __construct(TemplateBlockRenderingHistory $templateBlockRenderingHistory)
29
    {
30
        $this->templateBlockRenderingHistory = $templateBlockRenderingHistory;
31
        $this->reset();
32
    }
33
34
    public function collect(Request $request, Response $response): void
35
    {
36
        $this->data['renderedEvents'] = $this->templateBlockRenderingHistory->getRenderedEvents();
37
    }
38
39
    public function getRenderedEvents(): array
40
    {
41
        return $this->data['renderedEvents'];
42
    }
43
44
    public function getNumberOfRenderedEvents(): int
45
    {
46
        return count($this->data['renderedEvents']);
47
    }
48
49
    public function getNumberOfRenderedBlocks(): int
50
    {
51
        return array_reduce($this->data['renderedEvents'], static function (int $accumulator, array $event): int {
52
            return $accumulator + count($event['blocks']);
53
        }, 0);
54
    }
55
56
    public function getTotalDuration(): float
57
    {
58
        return array_reduce($this->data['renderedEvents'], static function (float $accumulator, array $event): float {
59
            return $accumulator + $event['time'];
60
        }, 0.0);
61
    }
62
63
    public function getName(): string
64
    {
65
        return 'sylius_ui.template_block';
66
    }
67
68
    public function reset(): void
69
    {
70
        $this->data['renderedEvents'] = [];
71
    }
72
}
73