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.
Completed
Push — master ( 64890e...b6740e )
by Ruben
01:22 queued 11s
created

ComponentPayload   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 139
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 4

Importance

Changes 0
Metric Value
wmc 19
lcom 2
cbo 4
dl 0
loc 139
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 property() 0 8 1
A dateTimeProperty() 0 12 2
A textProperty() 0 11 2
A uriProperty() 0 10 3
A subComponent() 0 8 2
A when() 0 8 2
A getType() 0 4 1
A getProperties() 0 4 1
A getProperty() 0 17 2
A getSubComponents() 0 4 1
1
<?php
2
3
namespace Spatie\IcalendarGenerator;
4
5
use Closure;
6
use DateTimeInterface;
7
use Exception;
8
use Spatie\IcalendarGenerator\Components\Component;
9
use Spatie\IcalendarGenerator\PropertyTypes\DateTimePropertyType;
10
use Spatie\IcalendarGenerator\PropertyTypes\PropertyType;
11
use Spatie\IcalendarGenerator\PropertyTypes\TextPropertyType;
12
use Spatie\IcalendarGenerator\PropertyTypes\UriPropertyType;
13
14
final class ComponentPayload
15
{
16
    /** @var string */
17
    private $type;
18
19
    /** @var array */
20
    private $properties = [];
21
22
    /** @var array */
23
    private $subComponents = [];
24
25
    public static function create(string $type): ComponentPayload
26
    {
27
        return new self($type);
28
    }
29
30
    public function __construct(string $type)
31
    {
32
        $this->type = $type;
33
    }
34
35
    public function property(PropertyType $property, array $parameters = null): ComponentPayload
36
    {
37
        $property->addParameters($parameters ?? []);
38
39
        $this->properties[] = $property;
40
41
        return $this;
42
    }
43
44
    /**
45
     * @param array|string $names
46
     * @param \DateTimeInterface|null $value
47
     * @param bool $withTime
48
     * @param bool $withTimeZone
49
     *
50
     * @return \Spatie\IcalendarGenerator\ComponentPayload
51
     */
52
    public function dateTimeProperty(
53
        $names,
54
        ?DateTimeInterface $value,
55
        bool $withTime = false,
56
        bool $withTimeZone = false
57
    ): ComponentPayload {
58
        if ($value === null) {
59
            return $this;
60
        }
61
62
        return $this->property(new DateTimePropertyType($names, $value, $withTime, $withTimeZone));
63
    }
64
65
    /**
66
     * @param array|string $names
67
     * @param string|null $value
68
     *
69
     * @param bool $disableEscaping
70
     *
71
     * @return \Spatie\IcalendarGenerator\ComponentPayload
72
     */
73
    public function textProperty(
74
        $names,
75
        ?string $value,
76
        bool $disableEscaping = false
77
    ): ComponentPayload {
78
        if ($value === null) {
79
            return $this;
80
        }
81
82
        return $this->property(new TextPropertyType($names, $value, $disableEscaping));
83
    }
84
85
    /**
86
     * @param array|string $names
87
     * @param string|null $value
88
     *
89
     * @return \Spatie\IcalendarGenerator\ComponentPayload
90
     */
91
    public function uriProperty(
92
        $names,
93
        ?string $value
94
    ): ComponentPayload {
95
        if ($value === null) {
96
            return $this;
97
        }
98
99
        return filter_var($value, FILTER_VALIDATE_URL) ? $this->property(new UriPropertyType($names, $value)) : $this;
100
    }
101
102
    public function subComponent(Component ...$components): ComponentPayload
103
    {
104
        foreach ($components as $component) {
105
            $this->subComponents[] = $component;
106
        }
107
108
        return $this;
109
    }
110
111
    public function when(bool $condition, Closure $closure): ComponentPayload
112
    {
113
        if ($condition) {
114
            $closure($this);
115
        }
116
117
        return $this;
118
    }
119
120
    public function getType(): string
121
    {
122
        return $this->type;
123
    }
124
125
    public function getProperties(): array
126
    {
127
        return $this->properties;
128
    }
129
130
    public function getProperty(string $name): PropertyType
131
    {
132
        $filteredProperties = array_filter(
133
            $this->properties,
134
            function (PropertyType $property) use ($name) {
135
                return in_array($name, $property->getNames());
136
            }
137
        );
138
139
        $properties = array_values($filteredProperties);
140
141
        if (count($properties) === 0) {
142
            throw new Exception("Property `{$name}` does not exist in the payload");
143
        }
144
145
        return $properties[0];
146
    }
147
148
    public function getSubComponents(): array
149
    {
150
        return $this->subComponents;
151
    }
152
}
153