|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Shippinno\Notification\Domain\Model; |
|
4
|
|
|
|
|
5
|
|
|
use Faker\Factory; |
|
6
|
|
|
use Tanigami\ValueObjects\Web\EmailAddress; |
|
7
|
|
|
|
|
8
|
|
|
class NotificationBuilder |
|
9
|
|
|
{ |
|
10
|
|
|
/** |
|
11
|
|
|
* @var Destination |
|
12
|
|
|
*/ |
|
13
|
|
|
private $destination; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @var Subject |
|
17
|
|
|
*/ |
|
18
|
|
|
private $subject; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @var Body |
|
22
|
|
|
*/ |
|
23
|
|
|
private $body; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var null|DeduplicationKey |
|
27
|
|
|
*/ |
|
28
|
|
|
private $deduplicationKey; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var null|array |
|
32
|
|
|
*/ |
|
33
|
|
|
private $metadata; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @return void |
|
|
|
|
|
|
37
|
|
|
*/ |
|
38
|
8 |
|
private function __construct() |
|
39
|
|
|
{ |
|
40
|
8 |
|
$faker = Factory::create(); |
|
41
|
8 |
|
$this->destination = new EmailDestination([new EmailAddress($faker->email)]); |
|
42
|
8 |
|
$this->subject = new Subject($faker->sentence(5)); |
|
43
|
8 |
|
$this->body = new Body($faker->text(300)); |
|
44
|
8 |
|
$this->deduplicationKey = null; |
|
45
|
8 |
|
$this->metadata = null; |
|
46
|
8 |
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @return NotificationBuilder |
|
50
|
|
|
*/ |
|
51
|
8 |
|
public static function notification(): NotificationBuilder |
|
52
|
|
|
{ |
|
53
|
8 |
|
return new NotificationBuilder; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @param Destination $destination |
|
58
|
|
|
* @return NotificationBuilder |
|
59
|
|
|
*/ |
|
60
|
1 |
|
public function withDestination(Destination $destination): NotificationBuilder |
|
61
|
|
|
{ |
|
62
|
1 |
|
$this->destination = $destination; |
|
63
|
|
|
|
|
64
|
1 |
|
return $this; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @param Subject|string $subject |
|
69
|
|
|
* @return NotificationBuilder |
|
70
|
|
|
*/ |
|
71
|
|
|
public function withSubject($subject): NotificationBuilder |
|
72
|
|
|
{ |
|
73
|
|
|
if (is_string($subject)) { |
|
74
|
|
|
$subject = new Subject($subject); |
|
75
|
|
|
} |
|
76
|
|
|
$this->subject = $subject; |
|
77
|
|
|
|
|
78
|
|
|
return $this; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* @param Body|string $body |
|
83
|
|
|
* @return NotificationBuilder |
|
84
|
|
|
*/ |
|
85
|
|
|
public function withBody($body): NotificationBuilder |
|
86
|
|
|
{ |
|
87
|
|
|
if (is_string($body)) { |
|
88
|
|
|
$body = new Body($body); |
|
89
|
|
|
} |
|
90
|
|
|
$this->body = $body; |
|
91
|
|
|
|
|
92
|
|
|
return $this; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* @param DeduplicationKey|string $deduplicationKey |
|
97
|
|
|
* @return NotificationBuilder |
|
98
|
|
|
*/ |
|
99
|
|
|
public function withDeduplicationKey($deduplicationKey): NotificationBuilder |
|
100
|
|
|
{ |
|
101
|
|
|
if (is_string($deduplicationKey)) { |
|
102
|
|
|
$deduplicationKey = new DeduplicationKey($deduplicationKey); |
|
103
|
|
|
} |
|
104
|
|
|
$this->deduplicationKey = $deduplicationKey; |
|
105
|
|
|
|
|
106
|
|
|
return $this; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* @param array $metadata |
|
111
|
|
|
* @return NotificationBuilder |
|
112
|
|
|
*/ |
|
113
|
2 |
|
public function withMetadata(array $metadata): NotificationBuilder |
|
114
|
|
|
{ |
|
115
|
2 |
|
$this->metadata = $metadata; |
|
116
|
|
|
|
|
117
|
2 |
|
return $this; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* @return Notification |
|
122
|
|
|
*/ |
|
123
|
8 |
|
public function build(): Notification |
|
124
|
|
|
{ |
|
125
|
8 |
|
return new Notification( |
|
126
|
8 |
|
$this->destination, |
|
127
|
8 |
|
$this->subject, |
|
128
|
8 |
|
$this->body, |
|
129
|
8 |
|
$this->deduplicationKey, |
|
130
|
8 |
|
$this->metadata |
|
131
|
|
|
); |
|
132
|
|
|
} |
|
133
|
|
|
} |
|
134
|
|
|
|
Adding a
@returnannotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.