1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ProjetNormandie\MessageBundle\Service; |
4
|
|
|
|
5
|
|
|
use ProjetNormandie\MessageBundle\Entity\Message; |
6
|
|
|
use ProjetNormandie\MessageBundle\Repository\MessageRepository; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Proxy to send a private message |
10
|
|
|
*/ |
11
|
|
|
class MessagerBuilder |
12
|
|
|
{ |
13
|
|
|
private string $type = 'DEFAULT'; |
14
|
|
|
private string $object; |
15
|
|
|
private string $message; |
16
|
|
|
private $sender; |
17
|
|
|
private $recipient; |
18
|
|
|
|
19
|
|
|
private MessageRepository $messageRepository; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Message constructor. |
23
|
|
|
* @param MessageRepository $messageRepository |
24
|
|
|
*/ |
25
|
|
|
public function __construct(MessageRepository $messageRepository) |
26
|
|
|
{ |
27
|
|
|
$this->messageRepository = $messageRepository; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @param string $type |
32
|
|
|
* @return MessagerBuilder |
33
|
|
|
*/ |
34
|
|
|
public function setType(string $type): MessagerBuilder |
35
|
|
|
{ |
36
|
|
|
$this->type = $type; |
37
|
|
|
return $this; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param string $object |
42
|
|
|
* @return MessagerBuilder |
43
|
|
|
*/ |
44
|
|
|
public function setObject(string $object): MessagerBuilder |
45
|
|
|
{ |
46
|
|
|
$this->object = $object; |
47
|
|
|
return $this; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param string $message |
52
|
|
|
* @return MessagerBuilder |
53
|
|
|
*/ |
54
|
|
|
public function setMessage(string $message): MessagerBuilder |
55
|
|
|
{ |
56
|
|
|
$this->message = $message; |
57
|
|
|
return $this; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param $sender |
62
|
|
|
* @return MessagerBuilder |
63
|
|
|
*/ |
64
|
|
|
public function setSender($sender): MessagerBuilder |
65
|
|
|
{ |
66
|
|
|
$this->sender = $sender; |
67
|
|
|
return $this; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @param $recipient |
72
|
|
|
* @return MessagerBuilder |
73
|
|
|
*/ |
74
|
|
|
public function setRecipient($recipient): MessagerBuilder |
75
|
|
|
{ |
76
|
|
|
$this->recipient = $recipient; |
77
|
|
|
return $this; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public function send() |
81
|
|
|
{ |
82
|
|
|
$message = new Message(); |
83
|
|
|
$message |
84
|
|
|
->setType($this->type) |
85
|
|
|
->setObject($this->object) |
86
|
|
|
->setMessage($this->message) |
87
|
|
|
->setSender($this->sender) |
88
|
|
|
->setRecipient($this->recipient) |
89
|
|
|
->setIsDeletedSender(true) |
90
|
|
|
; |
91
|
|
|
|
92
|
|
|
$this->messageRepository->save($message); |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|