Skip to content

Commit c924010

Browse files
Transfer sms sender component to Klipper
0 parents  commit c924010

File tree

71 files changed

+5279
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

71 files changed

+5279
-0
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/.php_cs.cache
2+
/vendor/

DelayedSmsEnvelope.php

+132
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Klipper package.
5+
*
6+
* (c) François Pluchino <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Klipper\Component\SmsSender;
13+
14+
use Klipper\Component\SmsSender\Exception\InvalidArgumentException;
15+
use Klipper\Component\SmsSender\Exception\LogicException;
16+
use Klipper\Component\SmsSender\Mime\Phone;
17+
use Klipper\Component\SmsSender\Mime\Sms;
18+
use Symfony\Component\Mime\Header\Headers;
19+
use Symfony\Component\Mime\Header\MailboxListHeader;
20+
use Symfony\Component\Mime\Message;
21+
use Symfony\Component\Mime\RawMessage;
22+
23+
/**
24+
* Delayed Sms envelope.
25+
*
26+
* @author François Pluchino <[email protected]>
27+
*/
28+
class DelayedSmsEnvelope extends SmsEnvelope
29+
{
30+
/**
31+
* @var bool
32+
*/
33+
private $senderSet = false;
34+
35+
/**
36+
* @var bool
37+
*/
38+
private $recipientsSet = false;
39+
40+
/**
41+
* @var Message
42+
*/
43+
private $message;
44+
45+
/**
46+
* Constructor.
47+
*
48+
* @param Message|RawMessage $message The message
49+
*/
50+
public function __construct(RawMessage $message)
51+
{
52+
if (!$message instanceof Message) {
53+
throw new InvalidArgumentException(sprintf(
54+
'A delayed SMS envelope requires an instance of %s ("%s" given).',
55+
Message::class,
56+
\get_class($message)
57+
));
58+
}
59+
60+
$this->message = $message;
61+
}
62+
63+
/**
64+
* {@inheritdoc}
65+
*/
66+
public function setFrom(Phone $from): void
67+
{
68+
parent::setFrom($from);
69+
70+
$this->senderSet = true;
71+
}
72+
73+
/**
74+
* {@inheritdoc}
75+
*/
76+
public function getFrom(): Phone
77+
{
78+
if ($this->senderSet) {
79+
return parent::getFrom();
80+
}
81+
82+
if ($this->message instanceof Sms && null !== $from = $this->message->getFrom()) {
83+
return $from;
84+
}
85+
86+
throw new LogicException('Unable to determine the sender of the message.');
87+
}
88+
89+
/**
90+
* {@inheritdoc}
91+
*/
92+
public function setRecipients(array $recipients): void
93+
{
94+
parent::setRecipients($recipients);
95+
96+
$this->recipientsSet = \count(parent::getRecipients()) > 0;
97+
}
98+
99+
/**
100+
* {@inheritdoc}
101+
*/
102+
public function getRecipients(): array
103+
{
104+
if ($this->recipientsSet) {
105+
return parent::getRecipients();
106+
}
107+
108+
return self::getRecipientsFromHeaders($this->message->getHeaders());
109+
}
110+
111+
/**
112+
* Get the recipient phones from the message header.
113+
*
114+
* @param Headers $headers The message headers
115+
*
116+
* @return Phone[]
117+
*/
118+
private static function getRecipientsFromHeaders(Headers $headers): array
119+
{
120+
$recipients = [];
121+
122+
/** @var MailboxListHeader $header */
123+
foreach ($headers->all('to') as $header) {
124+
/** @var Phone $phone */
125+
foreach ($header->getAddresses() as $phone) {
126+
$recipients[] = new Phone($phone->getPhone());
127+
}
128+
}
129+
130+
return $recipients;
131+
}
132+
}

Event/AbstractMessageEvent.php

+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Klipper package.
5+
*
6+
* (c) François Pluchino <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Klipper\Component\SmsSender\Event;
13+
14+
use Klipper\Component\SmsSender\SmsEnvelope;
15+
use Symfony\Component\Mime\RawMessage;
16+
use Symfony\Contracts\EventDispatcher\Event;
17+
18+
/**
19+
* @author François Pluchino <[email protected]>
20+
*/
21+
abstract class AbstractMessageEvent extends Event
22+
{
23+
/**
24+
* @var RawMessage
25+
*/
26+
private $message;
27+
28+
/**
29+
* @var SmsEnvelope
30+
*/
31+
private $envelope;
32+
33+
/**
34+
* Constructor.
35+
*
36+
* @param RawMessage $message The message
37+
* @param SmsEnvelope $envelope The envelope
38+
*/
39+
public function __construct(RawMessage $message, SmsEnvelope $envelope)
40+
{
41+
$this->message = $message;
42+
$this->envelope = $envelope;
43+
}
44+
45+
/**
46+
* Get the message.
47+
*
48+
* @return RawMessage
49+
*/
50+
public function getMessage(): RawMessage
51+
{
52+
return $this->message;
53+
}
54+
55+
/**
56+
* Set the message.
57+
*
58+
* @param RawMessage $message The message
59+
*/
60+
public function setMessage(RawMessage $message): void
61+
{
62+
$this->message = $message;
63+
}
64+
65+
/**
66+
* Get the envelope.
67+
*
68+
* @return SmsEnvelope
69+
*/
70+
public function getEnvelope(): SmsEnvelope
71+
{
72+
return $this->envelope;
73+
}
74+
75+
/**
76+
* Set the envelope.
77+
*
78+
* @param SmsEnvelope $envelope The envelope
79+
*/
80+
public function setEnvelope(SmsEnvelope $envelope): void
81+
{
82+
$this->envelope = $envelope;
83+
}
84+
}

Event/MessageEvent.php

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Klipper package.
5+
*
6+
* (c) François Pluchino <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Klipper\Component\SmsSender\Event;
13+
14+
/**
15+
* Allows the transformation of a Message.
16+
*
17+
* @author François Pluchino <[email protected]>
18+
*/
19+
class MessageEvent extends AbstractMessageEvent
20+
{
21+
}

Event/MessageResultEvent.php

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Klipper package.
5+
*
6+
* (c) François Pluchino <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Klipper\Component\SmsSender\Event;
13+
14+
use Klipper\Component\SmsSender\SmsEnvelope;
15+
use Klipper\Component\SmsSender\Transport\Result;
16+
use Symfony\Component\Mime\RawMessage;
17+
18+
/**
19+
* @author François Pluchino <[email protected]>
20+
*/
21+
class MessageResultEvent extends AbstractMessageEvent
22+
{
23+
/**
24+
* @var Result
25+
*/
26+
private $result;
27+
28+
/**
29+
* Constructor.
30+
*
31+
* @param RawMessage $message The message
32+
* @param SmsEnvelope $envelope The envelope
33+
* @param Result $result The result
34+
*/
35+
public function __construct(RawMessage $message, SmsEnvelope $envelope, Result $result)
36+
{
37+
parent::__construct($message, $envelope);
38+
39+
$this->result = $result;
40+
}
41+
42+
/**
43+
* Get the result.
44+
*
45+
* @return Result
46+
*/
47+
public function getResult(): Result
48+
{
49+
return $this->result;
50+
}
51+
}

0 commit comments

Comments
 (0)