buildCollection()   A
last analyzed

Complexity

Conditions 3
Paths 5

Size

Total Lines 22
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 14
nc 5
nop 1
dl 0
loc 22
rs 9.7998
c 0
b 0
f 0
1
<?php
2
/**
3
 * Copyright 2017 SURFnet bv
4
 *
5
 * Licensed under the Apache License, Version 2.0 (the "License");
6
 * you may not use this file except in compliance with the License.
7
 * You may obtain a copy of the License at
8
 *
9
 *     http://www.apache.org/licenses/LICENSE-2.0
10
 *
11
 * Unless required by applicable law or agreed to in writing, software
12
 * distributed under the License is distributed on an "AS IS" BASIS,
13
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
 * See the License for the specific language governing permissions and
15
 * limitations under the License.
16
 */
0 ignored issues
show
Coding Style introduced by
Missing @link tag in file comment
Loading history...
17
18
namespace Surfnet\StepupMiddleware\MiddlewareBundle\Service;
19
20
use Assert\Assertion;
21
use DateTime;
22
use Exception;
23
use InvalidArgumentException;
24
use Psr\Log\LoggerInterface;
25
use Surfnet\StepupMiddleware\ApiBundle\Identity\Repository\IdentityRepository;
26
use Surfnet\StepupMiddleware\ApiBundle\Identity\Repository\VerifiedSecondFactorRepository;
27
use Surfnet\StepupMiddleware\ApiBundle\Identity\Value\VerifiedTokenInformation;
28
29
/**
30
 * Retrieves tokens (second factors) that got registered at a given date. That data is then used to send a reminder
31
 * message to the identity the token belongs to, informing them to vet the token.
32
 */
0 ignored issues
show
Coding Style introduced by
Missing @category tag in class comment
Loading history...
Coding Style introduced by
Missing @package tag in class comment
Loading history...
Coding Style introduced by
Missing @author tag in class comment
Loading history...
Coding Style introduced by
Missing @license tag in class comment
Loading history...
Coding Style introduced by
Missing @link tag in class comment
Loading history...
33
class VerifiedSecondFactorReminderService
34
{
35
    public function __construct(
36
        private readonly VerifiedSecondFactorRepository $verifiedRepository,
37
        private readonly IdentityRepository $identityRepository,
38
        private readonly VerifiedSecondFactorReminderMailService $mailService,
39
        private readonly LoggerInterface $logger,
40
    ) {
41
    }
42
43
    public function sendReminders(DateTime $date, bool $dryRun): void
44
    {
45
        $this->logger->info(
46
            sprintf(
47
                'Sending reminders for date: %s. dry run mode is %s',
48
                $date->format('Y-m-d'),
49
                ($dryRun ? 'enabled' : 'disabled'),
50
            ),
51
        );
52
53
        $totalNumberSent = 0;
54
55
        $tokenInformationCollection = $this->buildCollection($date);
56
57
        if ($tokenInformationCollection !== []) {
58
            $this->logger->info(sprintf('%d token reminder(s) will be sent', count($tokenInformationCollection)));
59
60
            foreach ($tokenInformationCollection as $tokenInformation) {
61
                try {
62
                    $this->mailService->sendReminder($tokenInformation);
63
                    $wasSent = 1;
64
                } catch (Exception) {
65
                    $wasSent = 0;
66
                }
67
                $numberSent = $dryRun ? 1 : $wasSent;
68
69
                $this->logger->info(
70
                    sprintf(
71
                        'Message %s sent %sto "%s" with token id "%s" of type "%s"',
72
                        ($numberSent === 1 ? 'successfully' : 'was not'),
73
                        ($dryRun ? 'in dry run mode ' : ''),
74
                        $tokenInformation->getEmail(),
75
                        $tokenInformation->getTokenId(),
76
                        $tokenInformation->getTokenType(),
77
                    ),
78
                );
79
                $totalNumberSent += $numberSent;
80
            }
81
        }
82
83
        $this->logger->info(
84
            sprintf(
85
                '%d reminders %s been sent',
86
                $totalNumberSent,
87
                ($dryRun ? 'would have' : 'have'),
88
            ),
89
        );
90
    }
91
92
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $date should have a doc-comment as per coding-style.
Loading history...
93
     * @return VerifiedTokenInformation[]
94
     */
95
    private function buildCollection(DateTime $date): array
0 ignored issues
show
Coding Style introduced by
Private method name "VerifiedSecondFactorReminderService::buildCollection" must be prefixed with an underscore
Loading history...
96
    {
97
        $collection = [];
98
99
        foreach ($this->verifiedRepository->findByDate($date) as $token) {
100
            try {
101
                $identity = $this->identityRepository->find($token->identityId);
102
                Assertion::isObject(
103
                    $identity,
104
                    sprintf(
105
                        'Identity not found with id "%s" for second factor token "%s"',
106
                        $token->identityId,
107
                        $token->id,
108
                    ),
109
                );
110
                $collection[] = VerifiedTokenInformation::fromEntity($token, $identity);
111
            } catch (InvalidArgumentException $e) {
112
                $this->logger->alert($e->getMessage());
113
            }
114
        }
115
116
        return $collection;
117
    }
118
}
119