Passed
Push — master ( 3fad3a...a50da5 )
by Nicolas
02:48
created

ConfigurationManager::findConfiguration()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 12
ccs 7
cts 7
cp 1
rs 9.4285
cc 3
eloc 6
nc 3
nop 3
crap 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 4
    public function __construct(
47
        TypeProvider $typeProvider,
48
        ConfigurationParser $configurationParser,
49
        MemcachedCache $memcachedCache,
50
        UrlTransformer $urlTransformer
51
    ) {
52 4
        $this->typeProvider = $typeProvider;
53 4
        $this->configurationParser = $configurationParser;
54 4
        $this->memcache = $memcachedCache;
55 4
        $this->urlTransformer = $urlTransformer;
56 4
    }
57
58
    /**
59
     * @param string $typeName
60
     * @param string $week
61
     * @param string $seance
62
     *
63
     * @return Configuration|null
64
     */
65 2
    public function findConfiguration($typeName, $week, $seance)
66
    {
67 2
        $configurationSearch = $this->createConfiguration($typeName, $week, $seance);
68
69 2
        foreach ($this->findConfigurationsByType($typeName) as $configuration) {
70 1
            if ($configurationSearch == $configuration) {
71 1
                return $configurationSearch;
72
            }
73 1
        }
74
75 1
        return null;
76
    }
77
78
    /**
79
     * @param string $typeName
80
     *
81
     * @return array
82
     */
83 3
    public function findConfigurationsByType($typeName)
84
    {
85 3
        $configurationForType = $this->memcache->fetch(self::CACHE_KEY.$typeName);
86
87 3
        if (false === $configurationForType) {
88 3
            $configurationForType = json_decode(
89 3
                $this->configurationParser->parseToJson(
90 3
                    $this->urlTransformer->transformType($typeName)
91 3
                ),
92
                true
93 3
            );
94
95 3
            $this
96
                ->memcache
97 3
                ->save(self::CACHE_KEY.$typeName, $configurationForType, 3600);
98 3
        }
99
100 3
        $configurationList = [];
101 3
        foreach ($configurationForType as $conf) {
102 2
            $configurationList[] = $this
103 2
                ->createConfiguration(
104 2
                    $typeName,
105 2
                    $conf['week'],
106 2
                    $conf['seance']
107 2
                );
108 3
        }
109
110 3
        return $configurationList;
111
    }
112
113
    /**
114
     * @param string $typeName
115
     * @param int    $week
116
     * @param int    $seance
117
     *
118
     * @return Configuration
119
     */
120 4
    public function createConfiguration($typeName, $week, $seance)
121
    {
122 4
        $type = $this->typeProvider->getTypeByName($typeName);
123 4
        $configuration = new Configuration();
124 4
        $configuration->setType($type);
125 4
        $configuration->setNumberOfWeek($week);
126 4
        $configuration->setNumberOfSeance($seance);
127
128 4
        return $configuration;
129
    }
130
}
131