GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

Calendar   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 140
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 17
lcom 1
cbo 5
dl 0
loc 140
rs 10
c 0
b 0
f 0

12 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 4 1
A __construct() 0 4 1
A getComponentType() 0 4 1
A getRequiredProperties() 0 7 1
A name() 0 6 1
A description() 0 6 1
A productIdentifier() 0 6 1
A event() 0 22 4
A withTimezone() 0 6 1
A refreshInterval() 0 6 1
A get() 0 4 1
A payload() 0 28 3
1
<?php
2
3
namespace Spatie\IcalendarGenerator\Components;
4
5
use Closure;
6
use DateInterval;
7
use Spatie\IcalendarGenerator\ComponentPayload;
8
use Spatie\IcalendarGenerator\PropertyTypes\DurationPropertyType;
9
use Spatie\IcalendarGenerator\PropertyTypes\Parameter;
10
11
final class Calendar extends Component
12
{
13
    /** @var array */
14
    private $events = [];
15
16
    /** @var string|null */
17
    private $name;
18
19
    /** @var string|null */
20
    private $description;
21
22
    /** @var bool */
23
    private $withTimezone = false;
24
25
    /** @var \DateInterval|null */
26
    private $refreshInterval;
27
28
    /** @var string|null */
29
    private $productIdentifier;
30
31
    public static function create(string $name = null): Calendar
32
    {
33
        return new self($name);
34
    }
35
36
    public function __construct(string $name = null)
37
    {
38
        $this->name = $name;
39
    }
40
41
    public function getComponentType(): string
42
    {
43
        return 'CALENDAR';
44
    }
45
46
    public function getRequiredProperties(): array
47
    {
48
        return [
49
            'VERSION',
50
            'PRODID',
51
        ];
52
    }
53
54
    public function name(string $name): Calendar
55
    {
56
        $this->name = $name;
57
58
        return $this;
59
    }
60
61
    public function description(string $description): Calendar
62
    {
63
        $this->description = $description;
64
65
        return $this;
66
    }
67
68
    public function productIdentifier(string $identifier): Calendar
69
    {
70
        $this->productIdentifier = $identifier;
71
72
        return $this;
73
    }
74
75
    /**
76
     * @param $event \Spatie\IcalendarGenerator\Components\Event|array|Closure
77
     *
78
     * @return \Spatie\IcalendarGenerator\Components\Calendar
79
     */
80
    public function event($event): Calendar
81
    {
82
        if (is_null($event)) {
83
            return $this;
84
        }
85
86
        $events = array_map(function ($eventToResolve) {
87
            if (! is_callable($eventToResolve)) {
88
                return $eventToResolve;
89
            }
90
91
            $newEvent = new Event();
92
93
            $eventToResolve($newEvent);
94
95
            return $newEvent;
96
        }, is_array($event) ? $event : [$event]);
97
98
        $this->events = array_merge($this->events, $events);
99
100
        return $this;
101
    }
102
103
    public function withTimezone(): Calendar
104
    {
105
        $this->withTimezone = true;
106
107
        return $this;
108
    }
109
110
    public function refreshInterval(int $minutes): Calendar
111
    {
112
        $this->refreshInterval = new DateInterval("PT{$minutes}M");
113
114
        return $this;
115
    }
116
117
    public function get(): string
118
    {
119
        return $this->toString();
120
    }
121
122
    protected function payload(): ComponentPayload
123
    {
124
        $events = $this->events;
125
126
        if ($this->withTimezone) {
127
            array_walk($events, function (Event $event) {
128
                $event->withTimezone();
129
            });
130
        }
131
132
        $payload = ComponentPayload::create($this->getComponentType())
133
            ->textProperty('VERSION', '2.0')
134
            ->textProperty('PRODID', $this->productIdentifier ?? 'spatie/icalendar-generator')
135
            ->textProperty(['NAME', 'X-WR-CALNAME'], $this->name)
136
            ->textProperty(['DESCRIPTION', 'X-WR-CALDESC'], $this->description)
137
            ->subComponent(...$events);
138
139
        if ($this->refreshInterval) {
140
            $payload
141
                ->property(
142
                    DurationPropertyType::create('REFRESH-INTERVAL', $this->refreshInterval)
143
                        ->addParameter(new Parameter('VALUE', 'DURATION'))
144
                )
145
                ->property(DurationPropertyType::create('X-PUBLISHED-TTL', $this->refreshInterval));
146
        }
147
148
        return $payload;
149
    }
150
}
151