Completed
Push — in-memory-cache2 ( 3be408...987c52 )
by André
38:44 queued 18:27
created

PersistenceCacheCollector::simplifyCallArguments()   A

Complexity

Conditions 5
Paths 9

Size

Total Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
nc 9
nop 1
dl 0
loc 21
rs 9.2728
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A PersistenceCacheCollector::getHandlers() 0 11 2
1
<?php
2
3
/**
4
 * This file is part of the eZ Publish Kernel package.
5
 *
6
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
7
 * @license For full copyright and license information view LICENSE file distributed with this source code.
8
 */
9
namespace eZ\Bundle\EzPublishDebugBundle\Collector;
10
11
use eZ\Publish\Core\Persistence\Cache\PersistenceLogger;
12
use Symfony\Component\HttpFoundation\Request;
13
use Symfony\Component\HttpFoundation\Response;
14
use Symfony\Component\HttpKernel\DataCollector\DataCollector;
15
16
/**
17
 * Data collector listing SPI cache calls.
18
 */
19
class PersistenceCacheCollector extends DataCollector
20
{
21
    /**
22
     * @var PersistenceLogger
23
     */
24
    private $logger;
25
26
    public function __construct(PersistenceLogger $logger)
27
    {
28
        $this->logger = $logger;
29
    }
30
31
    public function collect(Request $request, Response $response, \Exception $exception = null)
32
    {
33
        $this->data = [
34
            'stats' => $this->logger->getStats(),
35
            'calls_logging_enabled' => $this->logger->isCallsLoggingEnabled(),
36
            'calls' => $this->logger->getCalls(),
37
            'handlers' => $this->logger->getLoadedUnCachedHandlers(),
38
        ];
39
    }
40
41
    public function getName()
42
    {
43
        return 'ezpublish.debug.persistence';
44
    }
45
46
    /**
47
     * Returns call count.
48
     *
49
     * @deprecaterd since 7.5, use getStats().
50
     *
51
     * @return int
52
     */
53
    public function getCount()
54
    {
55
        return $this->data['stats']['call'] + $this->data['stats']['miss'];
56
    }
57
58
    /**
59
     * Returns stats on Persistance cache usage.
60
     *
61
     * @since 7.5
62
     *
63
     * @return int[<string>]
0 ignored issues
show
Documentation introduced by
The doc-type int[<string>] could not be parsed: Expected "]" at position 2, but found "<". (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
64
     */
65
    public function getStats()
66
    {
67
        return $this->data['stats'];
68
    }
69
70
    /**
71
     * Returns flag to indicate if logging of calls is enabled or not.
72
     *
73
     * Typically not enabled in prod.
74
     *
75
     * @return bool
76
     */
77
    public function getCallsLoggingEnabled()
78
    {
79
        return $this->data['calls_logging_enabled'];
80
    }
81
82
    /**
83
     * Returns all calls.
84
     *
85
     * @return array
86
     */
87
    public function getCalls()
88
    {
89
        if (empty($this->data['calls'])) {
90
            return [];
91
        }
92
93
        $calls = $count = [];
94
        foreach ($this->data['calls'] as $hash => $call) {
95
            list($class, $method) = explode('::', $call['method']);
96
            $namespace = explode('\\', $class);
97
            $class = array_pop($namespace);
98
            $calls[$hash] = [
99
                'namespace' => $namespace,
100
                'class' => $class,
101
                'method' => $method,
102
                'arguments' => $call['arguments'],
103
                'traces' => $call['traces'],
104
                'stats' => $call['stats'],
105
            ];
106
            // Leave out in-memory lookups from sorting
107
            $count[$hash] = $call['stats']['uncached'] + $call['stats']['miss'] + $call['stats']['hit'];
108
        }
109
        unset($data);
110
111
        array_multisort($count, SORT_DESC, $calls);
112
113
        return $calls;
114
    }
115
116
    /**
117
     * Returns un cached handlers being loaded.
118
     *
119
     * @return array
120
     */
121
    public function getHandlers()
122
    {
123
        $handlers = [];
124
        foreach ($this->data['handlers'] as $handler => $count) {
125
            list($class, $method) = explode('::', $handler);
126
            unset($class);
127
            $handlers[$method] = $method . '(' . $count . ')';
128
        }
129
130
        return $handlers;
131
    }
132
133
    /**
134
     * Returns un cached handlers being loaded.
135
     *
136
     * @return array
137
     */
138
    public function getHandlersCount()
139
    {
140
        return array_sum($this->data['handlers']);
141
    }
142
}
143