Passed
Push — master ( 5fc9e5...74fbb5 )
by Nicolas
02:10
created

CapSniffer::writeCalendar()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 8
rs 9.4285
cc 1
eloc 5
nc 1
nop 3
1
<?php
2
3
namespace Cp;
4
5
use Cocur\Slugify\Slugify;
6
use Cp\Calendar\Builder\CalendarBuilder;
7
use Cp\DomainObject\Configuration;
8
use Cp\Provider\PlanProvider;
9
use Cp\Provider\TypeProvider;
10
11
/**
12
 * Class CapSniffer
13
 */
14
class CapSniffer
15
{
16
    /**
17
     * @var TypeProvider
18
     */
19
    private $typeProvider;
20
21
    /**
22
     * @var PlanProvider
23
     */
24
    private $planProvider;
25
26
    /**
27
     * @var Slugify
28
     */
29
    private $slug;
30
31
    /**
32
     * @var CalendarBuilder
33
     */
34
    private $calendarBuilder;
35
36
    /**
37
     * CapSniffer constructor.
38
     *
39
     * @param TypeProvider    $typeProvider
40
     * @param PlanProvider    $planProvider
41
     * @param CalendarBuilder $calendarBuilder
42
     * @param Slugify         $slug
43
     */
44
    public function __construct(
45
        TypeProvider $typeProvider,
46
        PlanProvider $planProvider,
47
        CalendarBuilder $calendarBuilder,
48
        Slugify $slug
49
    ) {
50
        $this->typeProvider = $typeProvider;
51
        $this->planProvider = $planProvider;
52
        $this->calendarBuilder = $calendarBuilder;
53
        $this->slug = $slug;
54
    }
55
56
    /**
57
     * @param string $typeName
58
     * @param string $week
59
     * @param string $seance
60
     *
61
     * @return string
62
     */
63
    public function generateCalendar($typeName, $week, $seance)
64
    {
65
        return $this->calendarBuilder->exportCalendar($this->getPlan($typeName, $week, $seance));
66
    }
67
68
    /**
69
     * @param string $typeName
70
     * @param string $week
71
     * @param string $seance
72
     */
73
    public function writeCalendar($typeName, $week, $seance)
74
    {
75
        $plan = $this->getPlan($typeName, $week, $seance);
76
        file_put_contents(
77
            __DIR__.'/../../'.$this->slug->slugify($plan->getName()).'.ics',
78
            $this->generateCalendar($typeName, $week, $seance)
79
        );
80
    }
81
82
    /**
83
     * @param string $typeName
84
     * @param string $week
85
     * @param string $seance
86
     *
87
     * @return Plan
88
     */
89
    public function getPlan($typeName, $week, $seance)
90
    {
91
        $configuration = $this->createConfiguration($typeName, $week, $seance);
92
        $plan = $this->planProvider->getPlanByConfiguration($configuration);
93
        $plan->setConfiguration($configuration);
94
95
        return $plan;
96
    }
97
98
    /**
99
     * @param $typeName
100
     * @param $week
101
     * @param $seance
102
     *
103
     * @return Configuration
104
     */
105
    private function createConfiguration($typeName, $week, $seance)
106
    {
107
        $type = $this->typeProvider->getType($typeName);
108
109
        $configuration = new Configuration();
110
        $configuration->setType($type);
111
        $configuration->setNumberOfWeek($week);
112
        $configuration->setNumberOfSeance($seance);
113
114
        return $configuration;
115
    }
116
}
117