|
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
|
|
|
|