ConfigurationManager::findConfiguration()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
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 11
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 $type
60
     * @param string $week
61
     * @param string $seance
62
     *
63
     * @return Configuration|null
64
     */
65 2
    public function findConfiguration($type, $week, $seance)
66
    {
67 2
        $configurationSearch = $this->createConfiguration($type, $week, $seance);
68 2
        foreach ($this->findConfigurationsByType($type) as $configuration) {
69 1
            if ($configurationSearch == $configuration) {
70 1
                return $configurationSearch;
71
            }
72 1
        }
73
74 1
        return null;
75
    }
76
77
    /**
78
     * @param string $type
79
     *
80
     * @return array
81
     */
82 3
    public function findConfigurationsByType($type)
83
    {
84 3
        $configurationForType = $this->memcache->fetch(self::CACHE_KEY.$type);
85
86 3
        if (false === $configurationForType) {
87 3
            $typeName = $this->typeProvider->getTypeByKey($type);
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.$type, $configurationForType, 3600);
98 3
        }
99
100 3
        $configurationList = [];
101 3
        foreach ($configurationForType as $conf) {
102 2
            $configurationList[] = $this
103 2
                ->createConfiguration(
104 2
                    $type,
105 2
                    $conf['week'],
106 2
                    $conf['seance']
107 2
                );
108 3
        }
109
110 3
        return $configurationList;
111
    }
112
113
    /**
114
     * @param string $type
115
     * @param int    $week
116
     * @param int    $seance
117
     *
118
     * @return Configuration
119
     */
120 4
    public function createConfiguration($type, $week, $seance)
121
    {
122 4
        $configuration = new Configuration();
123 4
        $configuration->setType($type);
124 4
        $configuration->setNumberOfWeek($week);
125 4
        $configuration->setNumberOfSeance($seance);
126
127 4
        return $configuration;
128
    }
129
}
130