|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* This file is part of the Explicit Architecture POC, |
|
7
|
|
|
* which is created on top of the Symfony Demo application. |
|
8
|
|
|
* |
|
9
|
|
|
* (c) Herberto Graça <[email protected]> |
|
10
|
|
|
* |
|
11
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
12
|
|
|
* file that was distributed with this source code. |
|
13
|
|
|
*/ |
|
14
|
|
|
|
|
15
|
|
|
namespace Acme\App\Infrastructure\Notification\Strategy\Sms; |
|
16
|
|
|
|
|
17
|
|
|
use Acme\App\Core\Component\User\Application\Repository\UserRepositoryInterface; |
|
18
|
|
|
use Acme\App\Core\Port\Notification\Client\Sms\SmsNotifierInterface; |
|
19
|
|
|
use Acme\App\Core\Port\Notification\NotificationInterface; |
|
20
|
|
|
use Acme\App\Infrastructure\Notification\NotificationType; |
|
21
|
|
|
use Acme\App\Infrastructure\Notification\Settings\NotificationSettingsServiceInterface; |
|
22
|
|
|
use Acme\App\Infrastructure\Notification\Strategy\AbstractNotificationStrategy; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @author Coen Moij |
|
26
|
|
|
* @author Herberto Graca <[email protected]> |
|
27
|
|
|
* @author Nicolae Nichifor |
|
28
|
|
|
*/ |
|
29
|
|
|
final class SmsNotificationStrategy extends AbstractNotificationStrategy |
|
30
|
|
|
{ |
|
31
|
|
|
/** |
|
32
|
|
|
* @var NotificationType |
|
33
|
|
|
*/ |
|
34
|
|
|
private $type; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @var SmsNotifierInterface |
|
38
|
|
|
*/ |
|
39
|
|
|
private $smsNotifier; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @var NotificationSettingsServiceInterface |
|
43
|
|
|
*/ |
|
44
|
|
|
private $smsNotificationSettingsService; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @var UserRepositoryInterface |
|
48
|
|
|
*/ |
|
49
|
|
|
private $userRepository; |
|
50
|
|
|
|
|
51
|
|
|
public function __construct( |
|
52
|
|
|
SmsNotifierInterface $smsNotifier, |
|
53
|
|
|
NotificationSettingsServiceInterface $smsNotificationSettingsService, |
|
54
|
|
|
UserRepositoryInterface $userRepository |
|
55
|
|
|
) { |
|
56
|
|
|
$this->type = NotificationType::sms(); |
|
57
|
|
|
$this->smsNotifier = $smsNotifier; |
|
58
|
|
|
$this->smsNotificationSettingsService = $smsNotificationSettingsService; |
|
59
|
|
|
$this->userRepository = $userRepository; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
public function getType(): NotificationType |
|
63
|
|
|
{ |
|
64
|
|
|
return $this->type; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
public function notify(NotificationInterface $notification): void |
|
68
|
|
|
{ |
|
69
|
|
|
$this->smsNotifier->sendNotification($this->generateNotificationMessage($notification)); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
public function canHandleNotification(NotificationInterface $notification): bool |
|
73
|
|
|
{ |
|
74
|
|
|
$userId = $notification->getDestinationUserId(); |
|
75
|
|
|
$user = $this->userRepository->findOneById($userId); |
|
76
|
|
|
|
|
77
|
|
|
return $this->smsNotificationSettingsService->hasNotificationsEnabled($userId) |
|
78
|
|
|
&& $user->hasMobile() |
|
79
|
|
|
&& parent::canHandleNotification($notification); |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
|