ApplicationInboundEvent::int()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 4
nc 3
nop 2
dl 0
loc 7
ccs 4
cts 4
cp 1
crap 3
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace App\Infrastructure\Events;
4
5
use RuntimeException;
6
use Symfony\Component\Uid\Ulid;
7
8
/**
9
 * This is a base class for all Application Inbound Events.
10
 * An Inbound Event is an Event that has originated from some other, possibly remote Application/Module.
11
 *
12
 * In the Context of this Demo, each Inbound Event is paired with a corresponding Outbound Event.
13
 * The pairing is based on an Event Name (string).
14
 */
15
abstract class ApplicationInboundEvent
16
{
17
18
    private Ulid $_eventId;
19
20
    /**
21
     * @param array $data
22
     */
23
    public function __construct(
24 67
        private array  $data
25
    )
26
    {
27
        $this->_eventId = $this->data['_eventId'];
28
    }
29 67
30 67
    /**
31
     * @param string $fieldName
32
     * @param bool $nullable
33
     * @return Ulid|null
34
     */
35
    protected function ulid(string $fieldName, bool $nullable = false): ?Ulid
36
    {
37 46
        $value = $this->value($fieldName, $nullable);
38
        if ($value === null) {
39 46
            return null;
40 45
        }
41 1
        return $value instanceof Ulid ? $value : new Ulid($value);
42
    }
43 44
44
    /**
45
     * @param string $fieldName
46
     * @param bool $nullable
47
     * @return array|null
48
     */
49
    protected function array(string $fieldName, bool $nullable = false): ?array
50
    {
51 40
        $value = $this->value($fieldName, $nullable);
52
        if ($value === null) {
53 40
            return null;
54 39
        }
55 1
        return is_array($value) ? $value : json_decode($value, true);
56
    }
57 38
58
    /**
59
     * @param string $fieldName
60
     * @param bool $nullable
61
     * @return int|null
62
     */
63
    protected function int(string $fieldName, bool $nullable = false): ?int
64
    {
65 20
        $value = $this->value($fieldName, $nullable);
66
        if ($value === null) {
67 20
            return null;
68 19
        }
69 1
        return is_int($value) ? $value : intval($value);
70
    }
71 18
72
    /**
73
     * @param string $fieldName
74
     * @param bool $nullable
75
     * @return \DateTime|null
76
     */
77
    protected function dateTime(string $fieldName, bool $nullable = false): ?\DateTime
78
    {
79 37
        $value = $this->value($fieldName, $nullable);
80
        if ($value === null) {
81 37
            return null;
82 36
        }
83 1
        if ($value instanceof \DateTime) {
84
            return $value;
85 35
        } else {
86 1
            $dateTime = \DateTime::createFromFormat("Y-m-d H:i:s", $value);
87
            if (!$dateTime) {
88 34
                throw new RuntimeException(sprintf("Invalid Date Time: '%s'", $value));
89 34
            }
90
            return $dateTime;
91
        }
92 34
    }
93
94
    /**
95
     * @param string $fieldName
96
     * @param bool $nullable
97
     * @return string|null
98
     */
99
    protected function string(string $fieldName, bool $nullable = false): ?string
100
    {
101 42
        $value = $this->value($fieldName, $nullable);
102
        if ($value == null) {
103 42
            return null;
104 41
        }
105 1
        return is_string($value) ? $value : (string)$value;
106
    }
107 40
108
    /**
109
     * @param string $fieldName
110
     * @param bool $nullable
111
     * @return mixed
112
     */
113
    private function value(string $fieldName, bool $nullable = false): mixed
114
    {
115 66
        if (!isset($this->data[$fieldName])) {
116
            if ($nullable) {
117 66
                return null;
118 10
            }
119 5
            $name = call_user_func(get_called_class() . '::getName');
120
            throw new RuntimeException("Event $name requires a field '$fieldName' to be present");
121 5
        }
122
        return $this->data[$fieldName];
123 56
    }
124
125
    /**
126
     * @return Ulid
127
     */
128
    public function getEventId(): Ulid
129
    {
130
        return $this->_eventId;
131
    }
132
133
    /**
134
     * @return string
135
     */
136
    public function __toString(): string
137
    {
138
        return json_encode($this->data);
139
    }
140
141
    public static abstract function getName(): string;
142
143
144
}