Passed
Push — master ( c5940e...cdf7b9 )
by Nicolas
04:36
created

PlanManager::findByType()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 24
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 24
ccs 0
cts 18
cp 0
rs 8.9713
c 1
b 0
f 0
cc 3
eloc 16
nc 3
nop 3
crap 12
1
<?php
2
3
namespace Cp\Manager;
4
5
use Cp\DomainObject\Plan;
6
use Cp\Exception\ConfigurationException;
7
use Cp\Parser\PlanParser;
8
use Cp\Transformer\UrlTransformer;
9
use Doctrine\Common\Cache\MemcachedCache;
10
use JMS\Serializer\Serializer;
11
12
/**
13
 * Class PlanManager
14
 */
15
class PlanManager
16
{
17
    /**
18
     * @var PlanParser
19
     */
20
    private $planParser;
21
22
    /**
23
     * @var UrlTransformer
24
     */
25
    private $urlTransformer;
26
27
    /**
28
     * @var Serializer
29
     */
30
    private $serializer;
31
32
    /**
33
     * @var MemcachedCache
34
     */
35
    private $memcache;
36
37
    /**
38
     * TrainingManager constructor.
39
     *
40
     * @param PlanParser     $planParser
41
     * @param UrlTransformer $urlTransformer
42
     * @param Serializer     $serializer
43
     * @param MemcachedCache $memcached
44
     */
45
    public function __construct(
46
        PlanParser $planParser,
47
        UrlTransformer $urlTransformer,
48
        Serializer $serializer,
49
        MemcachedCache $memcached
50
    ) {
51
        $this->planParser = $planParser;
52
        $this->urlTransformer = $urlTransformer;
53
        $this->serializer = $serializer;
54
        $this->memcache = $memcached;
55
    }
56
57
    /**
58
     * @param int    $week
59
     * @param int    $seance
60
     * @param string $type
61
     *
62
     * @return Plan
63
     * @throws ConfigurationException
64
     */
65
    public function findByType($week, $seance, $type)
66
    {
67
        $plan = $this->memcache->fetch($week.$seance.$type);
68
        if (false === $plan) {
69
            try {
70
                $jsonString = $this->planParser->parseToJson(
71
                    $this->urlTransformer->transformPlan($week, $seance, $type)
72
                );
73
            } catch (\Exception $e) {
74
                throw new ConfigurationException(
75
                    sprintf(
76
                        'Configuration with week: %s, seance: %s and type:%s is not available',
77
                        $week,
78
                        $seance,
79
                        $type
80
                    )
81
                );
82
            }
83
            $plan = $this->serializer->deserialize($jsonString, Plan::class, 'json');
84
            $this->memcache->save($week.$seance.$type, $plan, 3600);
85
        }
86
87
        return $plan;
88
    }
89
}
90