Completed
Pull Request — 1.5 (#769)
by Paweł
19:46 queued 09:38
created

TraceableChainLoader::getData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
/*
4
 * This file is part of the Superdesk Web Publisher Template Engine Bundle.
5
 *
6
 * Copyright 2019 Sourcefabric z.u. and contributors.
7
 *
8
 * For the full copyright and license information, please see the
9
 * AUTHORS and LICENSE files distributed with this source code.
10
 *
11
 * @copyright 2019 Sourcefabric z.ú
12
 * @license http://www.superdesk.org/license
13
 */
14
15
namespace SWP\Bundle\TemplatesSystemBundle\Loader;
16
17
use SWP\Component\TemplatesSystem\Gimme\Loader\ChainLoader;
18
use SWP\Component\TemplatesSystem\Gimme\Loader\LoaderInterface;
19
use Symfony\Component\Stopwatch\Stopwatch;
20
21
class TraceableChainLoader extends ChainLoader
22
{
23
    private $data = [
24
        'calledLoaders' => [],
25
        'loaders' => [],
26
        'totalCalls' => 0,
27
        'totalDuration' => 0,
28
    ];
29
30
    public function load($type, $parameters = [], $withoutParameters = [], $responseType = LoaderInterface::SINGLE)
31
    {
32
        foreach ($this->loaders as $priority => $loader) {
33
            $loaderClass = \get_class($loader);
34 View Code Duplication
            if (!\array_key_exists($loaderClass, $this->data['loaders'])) {
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...
35
                $this->data['loaders'][$loaderClass] = [
36
                    'priority' => $priority,
37
                    'calls' => [],
38
                ];
39
            }
40
        }
41
42
        foreach ($this->loaders as $priority => $loader) {
43
            $loaderClass = \get_class($loader);
44
            $stopwatch = new Stopwatch();
45
            if ($loader->isSupported($type)) {
46 View Code Duplication
                if (!\array_key_exists($loaderClass, $this->data['calledLoaders'])) {
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...
47
                    $this->data['calledLoaders'][$loaderClass] = [
48
                        'priority' => $priority,
49
                        'calls' => [],
50
                    ];
51
                }
52
53
                $stopwatch->start($loaderClass);
54
                $meta = $loader->load($type, $parameters, $withoutParameters, $responseType);
55
                $event = $stopwatch->stop($loaderClass);
56
57
                $this->data['calledLoaders'][$loaderClass]['calls'][] = [
58
                    'type' => $type,
59
                    'parameters' => $parameters,
60
                    'withoutParameters' => $withoutParameters,
61
                    'responseType' => $responseType,
62
                    'duration' => $event->getDuration(),
63
                    'found' => false !== $meta,
64
                ];
65
                ++$this->data['totalCalls'];
66
                $this->data['totalDuration'] = $this->data['totalDuration'] + $event->getDuration();
67
68
                if (false !== $meta) {
69
                    return $meta;
70
                }
71
            }
72
        }
73
74
        return false;
75
    }
76
77
    public function getData(): array
78
    {
79
        return $this->data;
80
    }
81
}
82