Passed
Push — develop ( 0a248f...2e4521 )
by Nikolay
05:19
created

ManagedCacheProvider   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 28
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 2
eloc 15
dl 0
loc 28
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A register() 0 21 2
1
<?php
2
/*
3
 * Copyright (C) MIKO LLC - All Rights Reserved
4
 * Unauthorized copying of this file, via any medium is strictly prohibited
5
 * Proprietary and confidential
6
 * Written by Nikolay Beketov, 8 2020
7
 *
8
 */
9
10
declare(strict_types=1);
11
/**
12
 * Copyright (C) MIKO LLC - All Rights Reserved
13
 * Unauthorized copying of this file, via any medium is strictly prohibited
14
 * Proprietary and confidential
15
 * Written by Nikolay Beketov, 4 2020
16
 *
17
 */
18
19
namespace MikoPBX\Common\Providers;
20
21
22
use Phalcon\Cache;
23
use Phalcon\Cache\Adapter\Stream;
24
use Phalcon\Di\DiInterface;
25
use Phalcon\Di\ServiceProviderInterface;
26
use Phalcon\Storage\SerializerFactory;
27
28
29
/**
30
 * Main database connection is created based in the parameters defined in the configuration file
31
 */
32
class ManagedCacheProvider implements ServiceProviderInterface
33
{
34
    /**
35
     * Register Models metadata service provider
36
     *
37
     * @param \Phalcon\Di\DiInterface $di
38
     */
39
    public function register(DiInterface $di): void
40
    {
41
        if (posix_getuid() === 0) {
42
            $tempDir = $di->getShared('config')->path('core.managedCacheDir');
43
        } else {
44
            $tempDir = $di->getShared('config')->path('www.managedCacheDir');
45
        }
46
        $di->setShared(
47
            'managedCache',
48
            function () use ($tempDir){
49
                $serializerFactory = new SerializerFactory();
50
51
                $options = [
52
                    'defaultSerializer' => 'Php',
53
                    'lifetime'          => 7200,
54
                    'storageDir'        => $tempDir,
55
                ];
56
57
                $adapter = new Stream ($serializerFactory, $options);
58
59
                return new Cache($adapter);
60
            }
61
        );
62
    }
63
}