Test Failed
Push — master ( fa8c20...ab35ba )
by Nicolas
02:05
created

ConfigurationManager   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 4

Test Coverage

Coverage 100%

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A createConfiguration() 0 11 1
B findConfigurationsByType() 0 25 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 Doctrine\Common\Cache\MemcachedCache;
9
10
/**
11
 * Class ConfigurationManager
12
 */
13
class ConfigurationManager
14
{
15
    const CACHE_KEY = 'configuration.list';
16
17
    /**
18
     * @var TypeProvider
19
     */
20
    private $typeProvider;
21
22
    /**
23 1
     * @var ConfigurationParser
24
     */
25 1
    protected $configurationParser;
26 1
27
    /**
28
     * @var MemcachedCache
29
     */
30
    protected $memcache;
31
32
    /**
33
     * ConfigurationManager constructor.
34
     *
35 1
     * @param TypeProvider        $typeProvider
36
     * @param ConfigurationParser $configurationParser
37 1
     * @param MemcachedCache      $memcachedCache
38
     */
39 1
    public function __construct(
40 1
        TypeProvider $typeProvider,
41 1
        ConfigurationParser $configurationParser,
42 1
        MemcachedCache $memcachedCache
43
    ) {
44 1
        $this->typeProvider = $typeProvider;
45
        $this->configurationParser = $configurationParser;
46
        $this->memcache = $memcachedCache;
47
    }
48
49
    /**
50
     * @param string $typeName
51
     * @param int    $week
52
     * @param int    $seance
53
     *
54
     * @return Configuration
55
     */
56
    public function createConfiguration($typeName, $week, $seance)
57
    {
58
        $type = $this->typeProvider->getTypeByName($typeName);
59
60
        $configuration = new Configuration();
61
        $configuration->setType($type);
62
        $configuration->setNumberOfWeek($week);
63
        $configuration->setNumberOfSeance($seance);
64
65
        return $configuration;
66
    }
67
68
    /**
69
     * @param string $typeName
70
     *
71
     * @return array
72
     */
73
    public function findConfigurationsByType($typeName)
74
    {
75
        $configurationForType = $this->memcache->fetch(self::CACHE_KEY.$typeName);
76
77
        if (false === $configurationForType) {
78
            $configurationForType = json_decode(
79
                $this
80
                    ->configurationParser
81
                    ->parseToJson('/Users/perso/Workspace/cap-sniffer-api/web/mock/plan-10-home.html')
82
                , true);
83
            $this->memcache->save(self::CACHE_KEY.$typeName, $configurationForType, 3600);
84
        }
85
86
        $configurationList = [];
87
        foreach ($configurationForType as $conf) {
88
            $configuration = new Configuration();
89
            $configuration->setType($typeName);
90
            $configuration->setNumberOfSeance($conf['seance']);
91
            $configuration->setNumberOfWeek($conf['week']);
92
93
            $configurationList[] = $configuration;
94
        }
95
96
        return $configurationList;
97
    }
98
}
99