Test Failed
Push — master ( a85b44...a25a4c )
by Divine Niiquaye
11:17
created

CacheServiceProvider::boot()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 7
c 1
b 0
f 0
nc 4
nop 1
dl 0
loc 13
rs 9.6111
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of DivineNii opensource projects.
7
 *
8
 * PHP version 7.4 and above required
9
 *
10
 * @author    Divine Niiquaye Ibok <[email protected]>
11
 * @copyright 2019 DivineNii (https://divinenii.com/)
12
 * @license   https://opensource.org/licenses/BSD-3-Clause License
13
 *
14
 * For the full copyright and license information, please view the LICENSE
15
 * file that was distributed with this source code.
16
 */
17
18
namespace Rade\Provider;
19
20
use Biurad\Cache\AdapterFactory;
21
use Biurad\Cache\CacheItemPool;
22
use Biurad\Cache\LoggableStorage;
23
use Biurad\Cache\SimpleCache;
24
use Biurad\Cache\TagAwareCache;
25
use Cache\Adapter\Doctrine\DoctrineCachePool;
0 ignored issues
show
Bug introduced by
The type Cache\Adapter\Doctrine\DoctrineCachePool was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
26
use Doctrine\Common\Cache as DoctrineCache;
27
use Monolog\Logger;
28
use Rade\API\BootableProviderInterface;
29
use Rade\Application;
30
use Rade\DI\Container;
31
use Rade\DI\ServiceProviderInterface;
32
33
/**
34
 * Biurad Cache Provider
35
 *
36
 * @author Divine Niiquaye Ibok <[email protected]>
37
 */
38
class CacheServiceProvider implements ServiceProviderInterface, BootableProviderInterface
39
{
40
    /**
41
     * {@inheritdoc}
42
     */
43
    public function getName(): string
44
    {
45
        return 'cache';
46
    }
47
48
    /**
49
     * {@inheritdoc}
50
     */
51
    public function register(Container $app): void
52
    {
53
        $app['cache.doctrine'] = static function (Container $app): DoctrineCache\Cache {
54
            $adapter = $app['cache.doctrine_adapter'] ?? AdapterFactory::createHandler(\extension_loaded('apcu') ? 'apcu' : 'array');
55
56
            if ($app->parameters['debug'] && isset($app['logger'])) {
57
                return new LoggableStorage($adapter);
58
            }
59
60
            return $adapter;
61
        };
62
63
        if (\class_exists(DoctrineCachePool::class)) {
64
            $app['cache.psr6'] = $app->lazy(TagAwareCache::class);
65
        } else {
66
            $app['cache.psr6'] = $app->lazy(CacheItemPool::class);
67
        }
68
69
        $app['cache.psr16'] = $app->lazy(SimpleCache::class);
70
    }
71
72
    /**
73
     * {@inheritdoc}
74
     */
75
    public function boot(Application $app): void
76
    {
77
        if ($app->parameters['debug'] && isset($app['logger'])) {
78
            $cache = $app['cache.doctrine'];
79
80
            if (!$cache instanceof LoggableStorage) {
81
                return;
82
            }
83
84
            foreach ($cache->getCalls() as $call) {
85
                unset($call['data']);
86
87
                $app['logger']->log(Logger::DEBUG, 'Cache called "{method}" with a given "{key}"', $call);
88
            }
89
        }
90
    }
91
}
92