ConfigService::getArray()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 23
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 16
nc 2
nop 0
dl 0
loc 23
rs 9.0856
c 0
b 0
f 0
1
<?php
2
/**
3
 * Copyright (c) 2017
4
 *
5
 * @package   Majima
6
 * @author    David Neustadt <[email protected]>
7
 * @copyright 2017 David Neustadt
8
 * @license   MIT
9
 */
10
11
namespace Majima\Services;
12
13
use Symfony\Component\Cache\Adapter\FilesystemAdapter;
14
use Symfony\Component\Cache\Adapter\PhpArrayAdapter;
15
use Symfony\Component\DependencyInjection\Container;
16
17
/**
18
 * Class ConfigService
19
 * @package Majima\Services
20
 */
21
class ConfigService implements ConfigServiceInterface
22
{
23
    /**
24
     * @var Container
25
     */
26
    private $container;
27
28
    /**
29
     * ConfigService constructor.
30
     * @param Container $container
31
     */
32
    public function __construct(Container $container)
33
    {
34
        $this->container = $container;
35
    }
36
37
    /**
38
     * @return array
39
     */
40
    public function getArray()
41
    {
42
        $securityContext = $this->container->get('security.authorization_checker');
43
        $router = $this->container->get('router');
44
        $config = [
45
            'admin' => $securityContext->isGranted('ROLE_ADMIN'),
46
            'base_url' => $router->getContext()->getBaseUrl(),
47
            'path_info' => $router->getContext()->getPathInfo()
48
        ];
49
50
        $cache = new PhpArrayAdapter(
51
            $this->container->getParameter('kernel.cache_dir') . '/majima.cache',
52
            new FilesystemAdapter('', 31536000, $this->container->getParameter('kernel.cache_dir'))
53
        );
54
        if ($cache->hasItem('majima.cache_buster')) {
55
            $cacheBuster = $cache->getItem('majima.cache_buster')->get();
56
        } else {
57
            $cacheBuster = time();
58
            $cache->warmUp(['majima.cache_buster' => $cacheBuster]);
59
        }
60
        $config['cache_buster'] = $cacheBuster;
61
62
        return $config;
63
    }
64
}