Completed
Push — spi_log_cache_hits ( 9f001e )
by André
21:42
created

PersistenceCacheCollector::collect()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 3
dl 0
loc 9
rs 9.9666
c 0
b 0
f 0
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
            'calls_logging_enabled' => $this->logger->isCallsLoggingEnabled(),
35
            'misses' => $this->logger->getCacheMisses(),
36
            'hits' => $this->logger->getCacheHits(),
37
            'handlers' => $this->logger->getLoadedUnCachedHandlers(),
38
        ];
39
    }
40
41
    public function getName()
42
    {
43
        return 'ezpublish.debug.persistence';
44
    }
45
46
    /**
47
     * @return int
48
     */
49
    public function getCountMisses()
50
    {
51
        return count($this->data['misses']);
52
    }
53
54
    /**
55
     * @return int
56
     */
57
    public function getCountHits()
58
    {
59
        return count($this->data['hits']);
60
    }
61
62
    /**
63
     * Returns flag to indicate if logging of calls is enabled or not.
64
     *
65
     * Typically not enabled in prod.
66
     *
67
     * @return bool
68
     */
69
    public function getCallsLoggingEnabled()
70
    {
71
        return $this->data['calls_logging_enabled'];
72
    }
73
74
    /**
75
     * Returns calls.
76
     *
77
     * @return array
78
     */
79
    public function getMisses()
80
    {
81
        return $this->getCallData($this->data['misses']);
82
    }
83
84
    /**
85
     * Returns hits.
86
     *
87
     * @return array
88
     */
89
    public function getHits()
90
    {
91
        return $this->getCallData($this->data['hits']);
92
    }
93
94
95
    private function getCallData(array $data): array
96
    {
97
        if (empty($data)) {
98
            return [];
99
        }
100
101
        $calls = $count = [];
102
        foreach ($data as $call) {
103
            $hash = md5(json_encode($call));
104
            if (isset($calls[$hash])) {
105
                $calls[$hash]['count']++;
106
                $count[$hash]++;
107
108
                continue;
109
            }
110
111
            list($class, $method) = explode('::', $call['method']);
112
            $namespace = explode('\\', $class);
113
            $class = array_pop($namespace);
114
            $calls[$hash] = array(
115
                'namespace' => $namespace,
116
                'class' => $class,
117
                'method' => $method,
118
                'arguments' => $this->simplifyCallArguments($call['arguments']),
119
                'trace' => implode(', ', $call['trace']),
120
                'count' => 1,
121
            );
122
            $count[$hash] = 1;
123
        }
124
125
        array_multisort($count, SORT_DESC, $calls);
126
127
        return $calls;
128
    }
129
130
    private function simplifyCallArguments(array $arguments): string
131
    {
132
        $string = '';
133
        foreach ($arguments as $key => $value) {
134
            if (empty($string)) {
135
                $string = $key . ':';
136
            } else {
137
                $string .= ', '. $key . ':';
138
            }
139
140
            if (is_array($value)) {
141
                $string .= '[' . implode(',', $value) . ']';
142
            } else {
143
                $string .= $value;
144
            }
145
        }
146
147
        return $string;
148
    }
149
150
    /**
151
     * Returns un cached handlers being loaded.
152
     *
153
     * @return array
154
     */
155
    public function getHandlers()
156
    {
157
        $handlers = [];
158
        foreach ($this->data['handlers'] as $handler => $count) {
159
            list($class, $method) = explode('::', $handler);
160
            unset($class);
161
            $handlers[$method] = $method . '(' . $count . ')';
162
        }
163
164
        return $handlers;
165
    }
166
167
    /**
168
     * Returns un cached handlers being loaded.
169
     *
170
     * @return array
171
     */
172
    public function getHandlersCount()
173
    {
174
        return array_sum($this->data['handlers']);
175
    }
176
}
177