CloudEvent::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 9
dl 0
loc 19
ccs 10
cts 10
cp 1
rs 9.9666
c 0
b 0
f 0
cc 1
nc 1
nop 8
crap 1

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace PascalDeVink\CloudEvents\V03;
6
7
use DateTimeImmutable;
8
use League\Uri\Uri;
9
use PascalDeVink\CloudEvents\Extension\DistributedTracing;
10
use Webmozart\Assert\Assert;
11
12
class CloudEvent
13
{
14
    private EventId     $eventId;
15
16
    private Source      $source;
17
18
    private SpecVersion $specVersion;
19
20
    private EventType   $eventType;
21
22
    private ?SchemaUrl  $schemaUrl;
23
24
    private ?Subject    $subject;
25
26
    private ?EventTime  $eventTime;
27
28
    private ?Extensions $extensions;
29
30
    private ?Data       $data;
31
32 24
    public function __construct(
33
        EventId $eventId,
34
        Source $source,
35
        EventType $eventType,
36
        ?SchemaUrl $schemaUrl = null,
37
        ?Subject $subject = null,
38
        ?EventTime $eventTime = null,
39
        ?Extensions $extensions = null,
40
        ?Data $data = null
41
    ) {
42 24
        $this->eventId     = $eventId;
43 24
        $this->source      = $source;
44 24
        $this->specVersion = new SpecVersion('0.3');
45 24
        $this->eventType   = $eventType;
46 24
        $this->subject     = $subject;
47 24
        $this->eventTime   = $eventTime;
48 24
        $this->schemaUrl   = $schemaUrl;
49 24
        $this->extensions  = $extensions;
50 24
        $this->data        = $data;
51 24
    }
52
53 3
    public static function fromArray(array $eventData) : self
54
    {
55 3
        Assert::keyExists($eventData, 'type');
56 3
        Assert::keyExists($eventData, 'source');
57 3
        Assert::keyExists($eventData, 'id');
58
59 3
        $data = null;
60
61 3
        if (isset($eventData['data']) === true) {
62 3
            Assert::keyExists($eventData, 'datacontenttype');
63 3
            Assert::keyExists($eventData, 'datacontentencoding');
64
65 3
            $data = JsonData::fromArray(
66 3
                $eventData['data'],
67 3
                new ContentEncoding($eventData['datacontentencoding'])
68
            );
69
        }
70
71 3
        $extensions = null;
72
73 3
        if (isset($eventData['DistributedTracingExtension']) === true) {
74 3
            $extensions = new Extensions(
75 3
                new DistributedTracing(
76 3
                    $eventData['DistributedTracingExtension']['traceparent'],
77 3
                    $eventData['DistributedTracingExtension']['tracestate'],
78
                )
79
            );
80
        }
81
82 3
        return new self(
83 3
            new EventId($eventData['id']),
84 3
            new Source(Uri::createFromString($eventData['source'])),
85 3
            new EventType($eventData['type']),
86 3
            isset($eventData['schemaurl']) ? new SchemaUrl(Uri::createFromString($eventData['schemaurl'])) : null,
87 3
            isset($eventData['subject']) ? new Subject($eventData['subject']) : null,
88 3
            isset($eventData['time']) ? new EventTime(new DateTimeImmutable($eventData['time'])) : null,
89
            $extensions,
90
            $data
91
        );
92
    }
93
94 6
    public function getEventId() : EventId
95
    {
96 6
        return $this->eventId;
97
    }
98
99 6
    public function getSource() : Source
100
    {
101 6
        return $this->source;
102
    }
103
104 6
    public function getSpecVersion() : SpecVersion
105
    {
106 6
        return $this->specVersion;
107
    }
108
109 6
    public function getEventType() : EventType
110
    {
111 6
        return $this->eventType;
112
    }
113
114 3
    public function getSchemaUrl() : ?SchemaUrl
115
    {
116 3
        return $this->schemaUrl;
117
    }
118
119 3
    public function getSubject() : ?Subject
120
    {
121 3
        return $this->subject;
122
    }
123
124 3
    public function getEventTime() : ?EventTime
125
    {
126 3
        return $this->eventTime;
127
    }
128
129 3
    public function getExtensions() : ?Extensions
130
    {
131 3
        return $this->extensions;
132
    }
133
134 3
    public function getData() : ?Data
135
    {
136 3
        return $this->data;
137
    }
138
139 3
    public function toArray() : array
140
    {
141 3
        $extensions = [];
142
143 3
        if ($this->extensions !== null) {
144 3
            $extensions = $this->extensions->getKeyValuePairs();
145
        }
146
147 3
        return array_merge(
148
            [
149 3
                'specversion'         => (string)$this->specVersion,
150 3
                'type'                => (string)$this->eventType,
151 3
                'source'              => (string)$this->source,
152 3
                'subject'             => $this->subject ? (string)$this->subject : null,
153 3
                'id'                  => (string)$this->eventId,
154 3
                'schemaurl'           => $this->schemaUrl ? (string)$this->schemaUrl : null,
155 3
                'time'                => $this->data ? (string)$this->eventTime : null,
156 3
                'datacontenttype'     => $this->data ? (string)$this->data->getContentType() : null,
157 3
                'datacontentencoding' => $this->data ? (string)$this->data->getContentEncoding() : null,
158 3
                'data'                => $this->data ? $this->data->getData() : null,
159
            ],
160
            $extensions
161
        );
162
    }
163
}
164