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.

ComponentPayload   A
last analyzed

Complexity

Total Complexity 20

Size/Duplication

Total Lines 141
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 4

Importance

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