Failed Conditions
Pull Request — master (#272)
by
unknown
02:45
created

ModelsCacheProvider   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
dl 0
loc 33
rs 10
c 0
b 0
f 0
wmc 2
lcom 0
cbo 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A register() 0 27 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Canvas\Providers;
6
7
use function Canvas\Core\envValue;
8
use Phalcon\Cache\Backend\Memory;
9
use Phalcon\Cache\Backend\Redis;
10
use Phalcon\Cache\Frontend\Data;
11
use Phalcon\Cache\Frontend\None;
12
use Phalcon\DiInterface;
13
use Phalcon\Di\ServiceProviderInterface;
14
15
class ModelsCacheProvider implements ServiceProviderInterface
16
{
17
    /**
18
     * @param DiInterface $container
19
     */
20
    public function register(DiInterface $container)
21
    {
22
        $container->setShared(
23
            'modelsCache',
24
            function () use ($container) {
25
                if (!$container->getConfig()->app->production) {
26
                    $frontCache = new None();
27
                    $cache = new Memory($frontCache);
28
                } else {
29
                    $frontCache = new Data([
30
                        'lifetime' => envValue('MODELS_CACHE_LIFETIME', 86400),
31
                    ]);
32
33
                    $cache = new Redis(
34
                        $frontCache,
35
                        [
36
                            'host' => envValue('REDIS_HOST', '127.0.0.1'),
37
                            'port' => envValue('REDIS_PORT', 6379),
38
                            'prefix' => 'modelsCache',
39
                        ]
40
                    );
41
                }
42
43
                return $cache;
44
            }
45
        );
46
    }
47
}
48