CalendarBuilder::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 2
crap 1
1
<?php
2
3
namespace Cp\Calendar\Builder;
4
5
use Cp\DomainObject\Plan;
6
use Jsvrcek\ICS\CalendarExport;
7
use Jsvrcek\ICS\Model\Calendar;
8
9
/**
10
 * Class CalendarBuilder
11
 */
12
class CalendarBuilder
13
{
14
    /**
15
     * @var CalendarExport
16
     */
17
    private $calendarExport;
18
19
    /**
20
     * @var CalendarEventBuilder
21
     */
22
    private $calendarEventBuilder;
23
24
    /**
25
     * CalendarBuilder constructor.
26
     *
27
     * @param CalendarExport       $calendarExport
28
     * @param CalendarEventBuilder $calendarEventBuilder
29
     */
30 3
    public function __construct(CalendarExport $calendarExport, CalendarEventBuilder $calendarEventBuilder)
31
    {
32 3
        $this->calendarExport = $calendarExport;
33 3
        $this->calendarEventBuilder = $calendarEventBuilder;
34 3
    }
35
36
    /**
37
     * @param Plan $plan
38
     */
39 2
    public function build(Plan $plan)
40
    {
41 2
        $calendar = new Calendar();
42 2
        $calendar->setProdId($plan->getName());
43 2
        $initialDate = new \DateTime();
44 2
        $initialDate->modify('- '.$this->getRecoveryDay($plan->getConfiguration()->getNumberOfSeance())[0].' day');
45
46 2
        foreach ($plan->getWeeks() as $week) {
47 2
            $events = $this->calendarEventBuilder->build($week);
48 2
            foreach ($events as $key => $event) {
49 2
                $event->setStart(clone $initialDate->modify('+'.$this->getRecoveryDay(count($events))[$key].' day'));
50 2
                $calendar->addEvent($event);
51 2
            }
52 2
        }
53
54 2
        $this->calendarExport->addCalendar($calendar);
55 2
    }
56
57
    /**
58
     * @param int $numSeance
59
     *
60
     * @return array
0 ignored issues
show
Documentation introduced by
Should the return type not be integer[]|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
61
     */
62 3
    public function getRecoveryDay($numSeance)
63
    {
64
        switch ($numSeance) {
65 3
            case 3:
66 3
                return [3, 3, 3];
67 1
            case 4:
68 1
                return [2, 2, 2, 2];
69 2
            case 5:
70 1
                return [1, 2, 1, 2, 1];
71 1 View Code Duplication
            case 6:
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
72 1
                return [1, 1, 1, 1, 1, 1];
73 1 View Code Duplication
            case 7:
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
74 1
                return [1, 1, 1, 1, 1, 1, 1];
75
76 1
            default:
77 1
                return null;
78 1
        }
79
    }
80
81
    /**
82
     * @param Plan $plan
83
     *
84
     * @return string
85
     */
86 1
    public function exportCalendar(Plan $plan)
87
    {
88 1
        $this->build($plan);
89
90 1
        return $this->calendarExport->getStream();
91
    }
92
}
93