Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
51 / 51
100.00% covered (success)
100.00%
5 / 5
CRAP
100.00% covered (success)
100.00%
1 / 1
CacheCollector
100.00% covered (success)
100.00%
51 / 51
100.00% covered (success)
100.00%
5 / 5
14
100.00% covered (success)
100.00%
1 / 1
 setInfo
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getActivities
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
1 / 1
2
 getContents
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
2
 renderCommands
100.00% covered (success)
100.00%
24 / 24
100.00% covered (success)
100.00%
1 / 1
6
 getHandler
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
3
1<?php declare(strict_types=1);
2/*
3 * This file is part of Aplus Framework Cache Library.
4 *
5 * (c) Natan Felles <natanfelles@gmail.com>
6 *
7 * For the full copyright and license information, please view the LICENSE
8 * file that was distributed with this source code.
9 */
10namespace Framework\Cache\Debug;
11
12use Framework\Cache\FilesCache;
13use Framework\Cache\MemcachedCache;
14use Framework\Cache\RedisCache;
15use Framework\Debug\Collector;
16
17/**
18 * Class CacheCollector.
19 *
20 * @package cache
21 */
22class CacheCollector extends Collector
23{
24    /**
25     * @var array<string,mixed>
26     */
27    protected array $info;
28
29    /**
30     * @param array<string,mixed> $info
31     *
32     * @return static
33     */
34    public function setInfo(array $info) : static
35    {
36        $this->info = $info;
37        return $this;
38    }
39
40    public function getActivities() : array
41    {
42        $activities = [];
43        foreach ($this->getData() as $index => $data) {
44            $activities[] = [
45                'collector' => $this->getName(),
46                'class' => static::class,
47                'description' => 'Run command ' . ($index + 1),
48                'start' => $data['start'],
49                'end' => $data['end'],
50            ];
51        }
52        return $activities;
53    }
54
55    public function getContents() : string
56    {
57        if (empty($this->info)) {
58            return '<p>This collector has not been added to a Cache instance.</p>';
59        }
60        \ob_start(); ?>
61        <p><strong>Handler:</strong> <?= $this->getHandler() ?></p>
62        <p><strong>Serializer:</strong> <?= $this->info['serializer'] ?></p>
63        <h1>Commands</h1>
64        <?php
65        echo $this->renderCommands();
66        return \ob_get_clean(); // @phpstan-ignore-line
67    }
68
69    protected function renderCommands() : string
70    {
71        if ( ! $this->hasData()) {
72            return '<p>No command was run.</p>';
73        }
74        $count = \count($this->getData());
75        \ob_start(); ?>
76        <p>Ran <?= $count ?> command<?= $count === 1 ? '' : 's' ?>:</p>
77        <table>
78            <thead>
79            <tr>
80                <th>#</th>
81                <th>Command</th>
82                <th>Status</th>
83                <th>Key</th>
84                <th>Value</th>
85                <th title="Time To Live in seconds">TTL</th>
86                <th>Expires At</th>
87                <th title="Seconds">Time</th>
88            </tr>
89            </thead>
90            <tbody>
91            <?php foreach ($this->getData() as $index => $data): ?>
92                <tr>
93                    <td><?= $index + 1 ?></td>
94                    <td><?= \htmlentities($data['command']) ?></td>
95                    <td><?= \htmlentities($data['status']) ?></td>
96                    <td><?= \htmlentities($data['key'] ?? '') ?></td>
97                    <td>
98                        <?php if (isset($data['value'])): ?>
99                            <pre><code class="language-php"><?= \htmlentities($data['value']) ?></code></pre>
100                        <?php endif ?>
101                    </td>
102                    <td><?= \htmlentities((string) ($data['ttl'] ?? '')) ?></td>
103                    <td><?php
104                        if (isset($data['ttl'])) {
105                            $ttl = $data['start'] + $data['ttl'];
106                            echo \date('Y-m-d H:i:s', (int) $ttl);
107                        } ?></td>
108                    <td><?= \round($data['end'] - $data['start'], 6) ?></td>
109                </tr>
110            <?php endforeach ?>
111            </tbody>
112        </table>
113        <?php
114        return \ob_get_clean(); // @phpstan-ignore-line
115    }
116
117    protected function getHandler() : string
118    {
119        foreach ([
120            'files' => FilesCache::class,
121            'memcached' => MemcachedCache::class,
122            'redis' => RedisCache::class,
123        ] as $name => $class) {
124            if ($this->info['class'] === $class) {
125                return $name;
126            }
127        }
128        return $this->info['class'];
129    }
130}