• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

fnagel / t3extblog / 23823603081

31 Mar 2026 11:05PM UTC coverage: 37.437% (-12.6%) from 50.045%
23823603081

push

github

fnagel
[FEATURE] Add YAML lint to composer scripts and CI

Using the new TYPO3 14.2 built in YAML linter.

https://docs.typo3.org/c/typo3/cms-core/main/en-us/Changelog/13.3/Feature-104973-ActivateLintYamlExecutableForTYPO3.html

1256 of 3355 relevant lines covered (37.44%)

3.11 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

3.08
/Classes/Service/CommentNotificationService.php
1
<?php
2

3
namespace FelixNagel\T3extblog\Service;
4

5
/**
6
 * This file is part of the "t3extblog" Extension for TYPO3 CMS.
7
 *
8
 * For the full copyright and license information, please read the
9
 * LICENSE.txt file that was distributed with this source code.
10
 */
11

12
use FelixNagel\T3extblog\Domain\Model\Comment;
13
use FelixNagel\T3extblog\Domain\Model\PostSubscriber;
14
use FelixNagel\T3extblog\Domain\Repository\CommentRepository;
15
use FelixNagel\T3extblog\Domain\Repository\PostSubscriberRepository;
16
use FelixNagel\T3extblog\Event;
17
use FelixNagel\T3extblog\Utility\SiteUtility;
18
use TYPO3\CMS\Core\Utility\GeneralUtility;
19

20
/**
21
 * Handles all notification mails for new or changed comments.
22
 */
23
class CommentNotificationService extends AbstractNotificationService
24
{
25
    /**
26
     * @var PostSubscriberRepository
27
     */
28
    protected $subscriberRepository;
29

30
    public function initializeObject()
8✔
31
    {
32
        parent::initializeObject();
8✔
33

34
        $this->subscriberRepository = GeneralUtility::makeInstance(PostSubscriberRepository::class);
8✔
35
        $this->subscriptionSettings = $this->settingsService->getTypoScriptByPath('subscriptionManager.comment');
8✔
36
    }
37

38
    /**
39
     * Process added comment (comment is already persisted to DB).
40
     *
41
     * @param Comment $comment Comment
42
     */
43
    public function processNewEntity($comment)
×
44
    {
45
        if (!($comment instanceof Comment)) {
×
46
            throw new \InvalidArgumentException('Object should be of type Comment!');
×
47
        }
48

49
        /** @var Event\Comment\Notification\CreateEvent $event */
50
        $event = $this->eventDispatcher->dispatch(
×
51
            new Event\Comment\Notification\CreateEvent($comment)
×
52
        );
×
53
        $comment = $event->getComment();
×
54

55
        $subscriber = null;
×
56
        if ($this->isNewSubscriptionValid($comment)) {
×
57
            $subscriber = $this->addSubscriber($comment);
×
58
        }
59

60
        if ($comment->isValid()) {
×
61
            if ($subscriber instanceof PostSubscriber) {
×
62
                $this->sendOptInMail($subscriber, $comment);
×
63
            }
64

65
            $this->notifySubscribers($comment);
×
66

67
            $this->persistToDatabase();
×
68
            $this->flushFrontendCache($comment);
×
69
        }
70
    }
71

72
    /**
73
     * Process changed status of a comment (comment is already persisted to DB).
74
     *
75
     * @param Comment $comment Comment
76
     */
77
    public function processChangedStatus($comment)
×
78
    {
79
        if (!($comment instanceof Comment)) {
×
80
            throw new \InvalidArgumentException('Object should be of type Comment!');
×
81
        }
82

83
        /** @var Event\Comment\Notification\CreateEvent $event */
84
        $event = $this->eventDispatcher->dispatch(
×
85
            new Event\Comment\Notification\ChangedEvent($comment)
×
86
        );
×
87
        $comment = $event->getComment();
×
88

89
        if ($comment->isValid()) {
×
90
            if (!empty($comment->getEmail())) {
×
91
                $subscriber = $this->subscriberRepository->findForSubscriptionMail($comment);
×
92
                if ($subscriber instanceof PostSubscriber) {
×
93
                    $this->sendOptInMail($subscriber, $comment);
×
94
                }
95
            }
96

97
            $this->notifySubscribers($comment);
×
98

99
            $this->persistToDatabase();
×
100
            $this->flushFrontendCache($comment);
×
101
        }
102
    }
103

104
    /**
105
     * Checks if a new subscription should be added.
106
     */
107
    protected function isNewSubscriptionValid(Comment $comment): bool
×
108
    {
109
        if (empty($comment->getEmail())) {
×
110
            return false;
×
111
        }
112

113
        if (!$this->settings['blogsystem']['comments']['subscribeForComments'] || !$comment->getSubscribe()) {
×
114
            return false;
×
115
        }
116

117
        // check if user already registered
118
        $subscribers = $this->subscriberRepository->findExistingSubscriptions(
×
119
            $comment->getPostId(),
×
120
            $comment->getEmail()
×
121
        );
×
122
        if (count($subscribers) > 0) {
×
123
            $this->getLog()->notice('Subscriber ['.$comment->getEmail().'] already registered.');
×
124

125
            return false;
×
126
        }
127

128
        return true;
×
129
    }
130

131
    /**
132
     * Send opt-in mail for subscriber.
133
     */
134
    protected function sendOptInMail(PostSubscriber $subscriber, Comment $comment)
×
135
    {
136
        $this->getLog()->dev('Send subscriber opt-in mail.');
×
137

138
        $post = $subscriber->getPost();
×
139

140
        $subscriber->updateAuth();
×
141
        $this->subscriberRepository->update($subscriber);
×
142

143
        $this->sendSubscriberEmail(
×
144
            $subscriber,
×
145
            $this->translate('subject.subscriber.comment.new', $post->getTitle(), SiteUtility::getLocale($subscriber)),
×
146
            $this->subscriptionSettings['subscriber']['template']['confirm'],
×
147
            [
×
148
                'post' => $post,
×
149
                'comment' => $comment,
×
150
            ]
×
151
        );
×
152
    }
153

154
    /**
155
     * Add a subscriber.
156
     */
157
    protected function addSubscriber(Comment $comment): PostSubscriber
×
158
    {
159
        /* @var $newSubscriber PostSubscriber */
160
        $newSubscriber = GeneralUtility::makeInstance(PostSubscriber::class, $comment->getPostId());
×
161
        $newSubscriber->setEmail($comment->getEmail());
×
162
        $newSubscriber->setName($comment->getAuthor());
×
163
        $newSubscriber->setPrivacyPolicyAccepted($comment->hasPrivacyPolicyAccepted());
×
164

165
        $this->subscriberRepository->add($newSubscriber);
×
166
        $this->persistToDatabase(true);
×
167

168
        $this->getLog()->dev('Added comment subscriber uid='.$newSubscriber->getUid());
×
169

170
        return $newSubscriber;
×
171
    }
172

173
    /**
174
     * Send comment notification mails.
175
     *
176
     * @var Comment $comment
177
     */
178
    public function notifySubscribers($comment): void
×
179
    {
180
        if (!($comment instanceof Comment)) {
×
181
            throw new \InvalidArgumentException('Object should be of type Comment!');
×
182
        }
183

184
        $settings = $this->subscriptionSettings['subscriber'];
×
185

186
        if (!$settings['enableNotifications']) {
×
187
            return;
×
188
        }
189

190
        if ($comment->getMailsSent()) {
×
191
            return;
×
192
        }
193

194
        $post = $comment->getPost();
×
195
        $subscribers = $this->subscriberRepository->findForNotification($post);
×
196
        $subject = $this->translate('subject.subscriber.comment.notify', $post->getTitle(), SiteUtility::getLocale($post));
×
197
        $variables = [
×
198
            'post' => $post,
×
199
            'comment' => $comment,
×
200
        ];
×
201

202
        /** @var Event\Comment\Notification\SubscribersEvent $event */
203
        $event = $this->eventDispatcher->dispatch(
×
204
            new Event\Comment\Notification\SubscribersEvent($comment, $subscribers, $subject, $variables)
×
205
        );
×
206
        $comment = $event->getComment();
×
207
        $subscribers = $event->getSubscribers();
×
208
        $subject = $event->getSubject();
×
209
        $variables = $event->getVariables();
×
210

211
        $this->getLog()->dev('Send post subscriber notification mails to '.count($subscribers).' users.');
×
212

213
        /* @var $subscriber PostSubscriber */
214
        foreach ($subscribers as $subscriber) {
×
215
            if (empty($subscriber->getEmail())) {
×
216
                continue;
×
217
            }
218

219
            // Make sure we do not notify the author of the triggering comment
220
            if ($comment->getEmail() === $subscriber->getEmail()) {
×
221
                continue;
×
222
            }
223

224
            $subscriber->updateAuth();
×
225
            $this->subscriberRepository->update($subscriber);
×
226

227
            $this->sendSubscriberEmail(
×
228
                $subscriber,
×
229
                $subject,
×
230
                $settings['template']['notification'],
×
231
                $variables
×
232
            );
×
233
        }
234

235
        $comment->setMailsSent(true);
×
236
        GeneralUtility::makeInstance(CommentRepository::class)->update($comment);
×
237
    }
238

239
    /**
240
     * Notify the blog admin.
241
     */
242
    public function notifyAdmin(Comment $comment): void
×
243
    {
244
        $settings = $this->subscriptionSettings['admin'];
×
245

246
        if (!$settings['enableNotifications']) {
×
247
            return;
×
248
        }
249

250
        if (!(is_array($settings['mailTo']) && strlen($settings['mailTo']['email']) > 0)) {
×
251
            // @extensionScannerIgnoreLine
252
            $this->getLog()->error('No admin email configured.', $settings['mailTo']);
×
253

254
            return;
×
255
        }
256

257
        $this->getLog()->dev('Send admin new comment notification mail.');
×
258

259
        $post = $comment->getPost();
×
260
        $language = SiteUtility::getLanguage($post->getPid(), $post->getSysLanguageUid());
×
261

262
        $this->sendEmail(
×
263
            [$settings['mailTo']['email'] => $settings['mailTo']['name']],
×
264
            $this->translate('subject.comment.admin.new', $post->getTitle(), $language->getLocale()),
×
265
            $settings['template'],
×
266
            $settings,
×
267
            [
×
268
                'post' => $post,
×
269
                'comment' => $comment,
×
270
            ],
×
271
            $language
×
272
        );
×
273
    }
274
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc