Completed
Push — master ( cd4a4c...c82fdd )
by arto
02:06
created

GenericEventBuilder::setMandatoryName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
/**
3
 * @author stev leibelt <[email protected]>
4
 * @since: 2016-08-29
5
 */
6
namespace Net\Bazzline\Component\Event;
7
8
use DateTime;
9
use RuntimeException;
10
11
class GenericEventBuilder
12
{
13
    /** @var DateTime */
14
    private $emittedAt;
15
16
    /** @var string */
17
    private $name;
18
19
    /** @var string */
20
    private $source;
21
22
    /** @var array|object */
23
    private $subject;
24
25
    /**
26
     * @param string $name
27
     */
28
    public function setMandatoryName($name)
29
    {
30
        $this->name = $name;
31
    }
32
33
    /**
34
     * @param string $source
35
     */
36
    public function setMandatorySource($source)
37
    {
38
        $this->source = $source;
39
    }
40
41
    /**
42
     * @param array|object $subject
43
     */
44
    public function setMandatorySubject($subject)
45
    {
46
        $this->subject = $subject;
47
    }
48
49
    /**
50
     * @param DateTime $emittedAt
51
     */
52
    public function setOptionalEmittedAt(DateTime $emittedAt)
53
    {
54
        $this->emittedAt = $emittedAt;
55
    }
56
57
    /**
58
     * @return GenericEvent
59
     * @throws RuntimeException
60
     */
61
    public function build()
62
    {
63
        $this->throwRuntimeExceptionIfNotAllMandatoryPropertiesAreSet();
64
65
        $event = new GenericEvent(
66
            $this->emittedAt,
67
            $this->name,
68
            $this->source,
69
            $this->subject
70
        );
71
72
        $this->reset();
73
74
        return $event;
75
    }
76
77
    private function reset()
78
    {
79
        $this->emittedAt    = new DateTime();
80
        $this->name         = null;
81
        $this->source       = null;
82
        $this->subject      = null;
83
    }
84
85
    /**
86
     * @throws RuntimeException
87
     */
88
    private function throwRuntimeExceptionIfNotAllMandatoryPropertiesAreSet()
89
    {
90
        if (is_null($this->name)) {
91
            throw new RuntimeException('mandatory property >>name<< not provided');
92
        }
93
94
        if (is_null($this->source)) {
95
            throw new RuntimeException('mandatory property >>source<< not provided');
96
        }
97
98
        if (is_null($this->subject)) {
99
            throw new RuntimeException('mandatory property >>subject<< not provided');
100
        }
101
    }
102
}
103