Passed
Push — master ( c83602...d2aead )
by Nicolas
01:50
created

ConfigurationManager   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 97
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 5
c 2
b 0
f 1
lcom 1
cbo 5
dl 0
loc 97
rs 10
ccs 34
cts 34
cp 1

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 1
A createConfiguration() 0 10 1
B findConfigurationsByType() 0 29 3
1
<?php
2
3
namespace Cp\Manager;
4
5
use Cp\DomainObject\Configuration;
6
use Cp\Parser\ConfigurationParser;
7
use Cp\Provider\TypeProvider;
8
use Cp\Transformer\UrlTransformer;
9
use Doctrine\Common\Cache\MemcachedCache;
10
11
/**
12
 * Class ConfigurationManager
13
 */
14
class ConfigurationManager
15
{
16
    const CACHE_KEY = 'configuration.list';
17
18
    /**
19
     * @var TypeProvider
20
     */
21
    private $typeProvider;
22
23
    /**
24
     * @var ConfigurationParser
25
     */
26
    protected $configurationParser;
27
28
    /**
29
     * @var MemcachedCache
30
     */
31
    protected $memcache;
32
33
    /**
34
     * @var UrlTransformer
35
     */
36
    protected $urlTransformer;
37
38
    /**
39
     * ConfigurationManager constructor.
40
     *
41
     * @param TypeProvider        $typeProvider
42
     * @param ConfigurationParser $configurationParser
43
     * @param MemcachedCache      $memcachedCache
44
     * @param UrlTransformer      $urlTransformer
45
     */
46 2
    public function __construct(
47
        TypeProvider $typeProvider,
48
        ConfigurationParser $configurationParser,
49
        MemcachedCache $memcachedCache,
50
        UrlTransformer $urlTransformer
51
    ) {
52 2
        $this->typeProvider = $typeProvider;
53 2
        $this->configurationParser = $configurationParser;
54 2
        $this->memcache = $memcachedCache;
55 2
        $this->urlTransformer = $urlTransformer;
56 2
    }
57
58
    /**
59
     * @param string $typeName
60
     * @param int    $week
61
     * @param int    $seance
62
     *
63
     * @return Configuration
64
     */
65 2
    public function createConfiguration($typeName, $week, $seance)
66
    {
67 2
        $type = $this->typeProvider->getTypeByName($typeName);
68 2
        $configuration = new Configuration();
69 2
        $configuration->setType($type);
70 2
        $configuration->setNumberOfWeek($week);
71 2
        $configuration->setNumberOfSeance($seance);
72
73 2
        return $configuration;
74
    }
75
76
    /**
77
     * @param string $typeName
78
     *
79
     * @return array
80
     */
81 1
    public function findConfigurationsByType($typeName)
82
    {
83 1
        $configurationForType = $this->memcache->fetch(self::CACHE_KEY.$typeName);
84
85 1
        if (false === $configurationForType) {
86 1
            $configurationForType = json_decode(
87 1
                $this->configurationParser->parseToJson(
88 1
                    $this->urlTransformer->transformType($typeName)
89 1
                ),
90
                true
91 1
            );
92
93 1
            $this
94
                ->memcache
95 1
                ->save(self::CACHE_KEY.$typeName, $configurationForType, 3600);
96 1
        }
97
98 1
        $configurationList = [];
99 1
        foreach ($configurationForType as $conf) {
100 1
            $configurationList[] = $this
101 1
                ->createConfiguration(
102 1
                    $typeName,
103 1
                    $conf['week'],
104 1
                    $conf['seance']
105 1
                );
106 1
        }
107
108 1
        return $configurationList;
109
    }
110
}
111