Completed
Push — feature/second-factor-revocati... ( 715c87...f06494 )
by A.
03:32
created

RegistrationEmailProcessor::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 9
nc 1
nop 4
1
<?php
2
3
/**
4
 * Copyright 2016 SURFnet B.V.
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.
8
 * You may obtain a copy of the License at
9
 *
10
 *     http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 * See the License for the specific language governing permissions and
16
 * limitations under the License.
17
 */
18
19
namespace Surfnet\StepupMiddleware\CommandHandlingBundle\Processor;
20
21
use Broadway\Processor\Processor;
22
use Surfnet\Stepup\Configuration\Value\Institution;
23
use Surfnet\Stepup\Identity\Event\EmailVerifiedEvent;
24
use Surfnet\StepupMiddleware\ApiBundle\Configuration\Service\InstitutionConfigurationOptionsService;
25
use Surfnet\StepupMiddleware\ApiBundle\Configuration\Service\RaLocationService;
26
use Surfnet\StepupMiddleware\ApiBundle\Identity\Service\RaListingService;
27
use Surfnet\StepupMiddleware\ApiBundle\Identity\Value\RegistrationAuthorityCredentials;
28
use Surfnet\StepupMiddleware\CommandHandlingBundle\Identity\Service\RegistrationMailService;
29
30
final class RegistrationEmailProcessor extends Processor
31
{
32
    /**
33
     * @var RaLocationService
34
     */
35
    private $raLocationsService;
36
37
    /**
38
     * @var RegistrationMailService
39
     */
40
    private $registrationMailService;
41
42
    /**
43
     * @var InstitutionConfigurationOptionsService
44
     */
45
    private $institutionConfigurationOptionsService;
46
47
    /**
48
     * @var RaListingService
49
     */
50
    private $raListingService;
51
52
    public function __construct(
53
        RegistrationMailService $registrationMailService,
54
        RaListingService $raListingService,
55
        InstitutionConfigurationOptionsService $institutionConfigurationOptionsService,
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $institutionConfigurationOptionsService exceeds the maximum configured length of 30.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
56
        RaLocationService $raLocationsService
57
    ) {
58
        $this->registrationMailService                = $registrationMailService;
59
        $this->raListingService                       = $raListingService;
60
        $this->institutionConfigurationOptionsService = $institutionConfigurationOptionsService;
61
        $this->raLocationsService                     = $raLocationsService;
62
    }
63
64
    public function handleEmailVerifiedEvent(EmailVerifiedEvent $event)
65
    {
66
        $institution = new Institution($event->identityInstitution->getInstitution());
67
        $institutionConfigurationOptions = $this->institutionConfigurationOptionsService
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $institutionConfigurationOptions exceeds the maximum configured length of 30.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
68
            ->findInstitutionConfigurationOptionsFor($institution);
69
70
        if ($institutionConfigurationOptions->useRaLocationsOption->isEnabled()) {
71
            $this->sendRegistrationEmailWithRaLocations($event, $institution);
72
73
            return;
74
        }
75
76
        $ras = $this->raListingService->listRegistrationAuthoritiesFor($event->identityInstitution);
77
78
        if ($institutionConfigurationOptions->showRaaContactInformationOption->isEnabled()) {
79
            $this->sendRegistrationEmailWithRas($event, $ras);
80
81
            return;
82
        }
83
84
        $rasWithoutRaas = array_filter($ras, function (RegistrationAuthorityCredentials $ra) {
85
            return !$ra->isRaa();
86
        });
87
88
        $this->sendRegistrationEmailWithRas($event, $rasWithoutRaas);
89
    }
90
91
    /**
92
     * @param EmailVerifiedEvent $event
93
     * @param Institution $institution
94
     */
95
    private function sendRegistrationEmailWithRaLocations(EmailVerifiedEvent $event, Institution $institution)
96
    {
97
        $this->registrationMailService->sendRegistrationEmailWithRaLocations(
98
            (string)$event->preferredLocale,
99
            (string)$event->commonName,
100
            (string)$event->email,
101
            $event->registrationCode,
102
            $this->raLocationsService->listRaLocationsFor($institution)
103
        );
104
    }
105
106
    /**
107
     * @param EmailVerifiedEvent $event
108
     * @param RegistrationAuthorityCredentials[] $ras
109
     */
110
    private function sendRegistrationEmailWithRas(EmailVerifiedEvent $event, array $ras)
111
    {
112
        $this->registrationMailService->sendRegistrationEmailWithRas(
113
            (string)$event->preferredLocale,
114
            (string)$event->commonName,
115
            (string)$event->email,
116
            $event->registrationCode,
117
            $ras
118
        );
119
    }
120
}
121