NewCommentEmailGenerator   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
wmc 2
lcom 1
cbo 7
dl 0
loc 58
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A generate() 0 24 1
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\Core\Component\Blog\Application\Notification\NewComment\Email;
16
17
use Acme\App\Core\Component\Blog\Application\Notification\NewComment\NewCommentNotification;
18
use Acme\App\Core\Port\Notification\Client\Email\Email;
19
use Acme\App\Core\Port\Notification\Client\Email\EmailGenerator;
20
use Acme\App\Core\Port\Router\UrlGeneratorInterface;
21
use Acme\App\Core\Port\Router\UrlType;
22
use Acme\App\Core\Port\Translation\TranslatorInterface;
23
24
class NewCommentEmailGenerator
25
{
26
    // Unfortunately we need to make these public, otherwise we can't test it without reflection
27
    public const CAMPAIGN_NAME = 'dummy_email_to_make_a_campaign_on_-_but_fine_as_an_example';
28
    public const SUBJECT_TRANSLATION_KEY = 'blog.email.new_comment.subject';
29
    public const TEMPLATE_TXT = '@Core/Component/Blog/Application/Notification/NewComment/Email/NewCommentEmail.txt.twig';
30
    public const TEMPLATE_HTML = '@Core/Component/Blog/Application/Notification/NewComment/Email/NewCommentEmail.html.twig';
31
32
    /**
33
     * @var EmailGenerator
34
     */
35
    private $emailGenerator;
36
37
    /**
38
     * @var UrlGeneratorInterface
39
     */
40
    private $urlGenerator;
41
42
    /**
43
     * @var TranslatorInterface
44
     */
45
    private $translator;
46
47
    public function __construct(
48
        EmailGenerator $emailGenerator,
49
        UrlGeneratorInterface $urlGenerator,
50
        TranslatorInterface $translator
51
    ) {
52
        $this->emailGenerator = $emailGenerator;
53
        $this->urlGenerator = $urlGenerator;
54
        $this->translator = $translator;
55
    }
56
57
    public function generate(NewCommentNotification $newCommentNotification): Email
58
    {
59
        $linkToPost = $this->urlGenerator->generateUrl(
60
            'post',
61
            [
62
                'slug' => $newCommentNotification->getPostSlug(),
63
                '_fragment' => 'comment_' . $newCommentNotification->getCommentId(),
64
            ],
65
            UrlType::absoluteUrl()
66
        );
67
68
        $subject = $this->translator->translate(self::SUBJECT_TRANSLATION_KEY);
69
        $email = $this->emailGenerator->generateEmailMessage(
70
            $newCommentNotification->getPostAuthorEmail(),
71
            $subject,
72
            self::TEMPLATE_TXT,
73
            self::TEMPLATE_HTML,
74
            new NewCommentEmailViewModel($subject, $newCommentNotification->getPostTitle(), $linkToPost)
75
        );
76
77
        $email->setTrackingCampaign(self::CAMPAIGN_NAME);
78
79
        return $email;
80
    }
81
}
82