AbstractAnalyzerBackend::logEntry()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 17
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 17
rs 9.4285
cc 2
eloc 13
nc 2
nop 3
1
<?php
2
/**
3
 * Abstract Analyse backend
4
 *
5
 * @package CacheCheck\Cache\Backend
6
 * @author  Tim Lochmüller
7
 */
8
9
namespace HDNET\CacheCheck\Cache\Backend;
10
11
use HDNET\CacheCheck\Domain\Repository\CacheRepository;
12
use TYPO3\CMS\Core\Cache\Backend\AbstractBackend;
13
use TYPO3\CMS\Core\Cache\Backend\BackendInterface;
14
use TYPO3\CMS\Core\Cache\Backend\FreezableBackendInterface;
15
use TYPO3\CMS\Core\Cache\Backend\PhpCapableBackendInterface;
16
use TYPO3\CMS\Core\Cache\Backend\TaggableBackendInterface;
17
use TYPO3\CMS\Core\Cache\Exception\InvalidBackendException;
18
use TYPO3\CMS\Core\Cache\Frontend\FrontendInterface;
19
use TYPO3\CMS\Core\Database\DatabaseConnection;
20
use TYPO3\CMS\Core\Utility\GeneralUtility;
21
22
/**
23
 * Class CacheAnalyzerBackend
24
 */
25
abstract class AbstractAnalyzerBackend extends AbstractBackend implements FreezableBackendInterface, PhpCapableBackendInterface, TaggableBackendInterface
26
{
27
28
    /**
29
     * Original Backend
30
     *
31
     * @var AbstractBackend
32
     */
33
    protected $originalBackend;
34
35
    /**
36
     * Options
37
     *
38
     * @var array
39
     */
40
    protected $options;
41
42
    /**
43
     * Build up the object
44
     *
45
     * @param string $context
46
     * @param array  $options
47
     */
48
    public function __construct($context, array $options = [])
49
    {
50
        $this->options = $options;
51
        parent::__construct($context, $options);
52
    }
53
54
    /**
55
     * Set the cache and init the original backend
56
     *
57
     * @param FrontendInterface $cache
58
     *
59
     * @throws InvalidBackendException
60
     */
61
    public function setCache(FrontendInterface $cache)
62
    {
63
        parent::setCache($cache);
64
65
        $cacheRepository = GeneralUtility::makeInstance(CacheRepository::class);
66
        $cacheObject = $cacheRepository->findByName($this->cacheIdentifier);
67
68
        $backendObjectName = '\\' . ltrim($cacheObject->getOriginalBackend(), '\\');
69
        $this->originalBackend = new $backendObjectName($this->context, $this->options);
70
        if (!$this->originalBackend instanceof BackendInterface) {
71
            throw new InvalidBackendException('"' . $backendObjectName . '" is not a valid cache backend object.', 1216304301);
72
        }
73
        $this->originalBackend->setCache($cache);
74
    }
75
76
    /**
77
     * Log one entry
78
     *
79
     * @param string $calledMethod
80
     * @param string $entryIdentifier
81
     * @param string $data
82
     */
83
    protected function logEntry($calledMethod, $entryIdentifier = '', $data = '')
84
    {
85
        static $requestHash = null;
86
        if ($requestHash === null) {
87
            $requestHash = uniqid();
88
        }
89
        $fieldsValues = [
90
            'timestamp'        => GeneralUtility::milliseconds(),
91
            'request_hash'     => $requestHash,
92
            'cache_name'       => $this->cacheIdentifier,
93
            'called_method'    => $calledMethod,
94
            'entry_identifier' => $entryIdentifier,
95
            'entry_size'       => strlen($data),
96
        ];
97
        $this->getDatabaseConnection()
98
            ->exec_INSERTquery('tx_cachecheck_domain_model_log', $fieldsValues);
99
    }
100
101
    /**
102
     * Get the database connection
103
     *
104
     * @return DatabaseConnection
105
     */
106
    protected function getDatabaseConnection()
107
    {
108
        return $GLOBALS['TYPO3_DB'];
109
    }
110
}
111