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