UserInvitedMailerSubscriber::handle()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 8
nc 1
nop 1
1
<?php
2
3
/*
4
 * This file is part of the BenGorUser package.
5
 *
6
 * (c) Beñat Espiña <[email protected]>
7
 * (c) Gorka Laucirica <[email protected]>
8
 *
9
 * For the full copyright and license information, please view the LICENSE
10
 * file that was distributed with this source code.
11
 */
12
13
namespace BenGorUser\User\Domain\Event;
14
15
use BenGorUser\User\Domain\Model\Event\UserEvent;
16
use BenGorUser\User\Domain\Model\Event\UserInvitationTokenRegenerated;
17
use BenGorUser\User\Domain\Model\Event\UserInvited;
18
use BenGorUser\User\Domain\Model\UserMailableFactory;
19
use BenGorUser\User\Domain\Model\UserMailer;
20
use BenGorUser\User\Domain\Model\UserUrlGenerator;
21
22
/**
23
 * User invited mailer subscriber class.
24
 *
25
 * @author Beñat Espiña <[email protected]>
26
 */
27
class UserInvitedMailerSubscriber implements UserEventSubscriber
28
{
29
    /**
30
     * The mailable factory.
31
     *
32
     * @var UserMailableFactory
33
     */
34
    private $mailableFactory;
35
36
    /**
37
     * The mailer.
38
     *
39
     * @var UserMailer
40
     */
41
    private $mailer;
42
43
    /**
44
     * The url generator.
45
     *
46
     * @var UserUrlGenerator
47
     */
48
    private $urlGenerator;
49
50
    /**
51
     * Constructor.
52
     *
53
     * @param UserMailer          $aMailer          The mailer
54
     * @param UserMailableFactory $aMailableFactory The mailable factory
55
     * @param UserUrlGenerator    $anUrlGenerator   The url generator
56
     */
57
    public function __construct(
58
        UserMailer $aMailer,
59
        UserMailableFactory $aMailableFactory,
60
        UserUrlGenerator $anUrlGenerator
61
    ) {
62
        $this->mailer = $aMailer;
63
        $this->mailableFactory = $aMailableFactory;
64
        $this->urlGenerator = $anUrlGenerator;
65
    }
66
67
    /**
68
     * {@inheritdoc}
69
     */
70
    public function handle(UserEvent $anEvent)
71
    {
72
        $url = $this->urlGenerator->generate(
73
            $anEvent->invitationToken()->token()
74
        );
75
76
        $mail = $this->mailableFactory->build(
77
            $anEvent->email(),
78
            [
79
                'email' => $anEvent->email(),
80
                'url'   => $url,
81
            ]
82
        );
83
84
        $this->mailer->mail($mail);
85
    }
86
87
    /**
88
     * {@inheritdoc}
89
     */
90
    public function isSubscribedTo(UserEvent $anEvent)
91
    {
92
        return $anEvent instanceof UserInvited || $anEvent instanceof UserInvitationTokenRegenerated;
93
    }
94
}
95