Completed
Pull Request — develop (#152)
by A.
04:44
created

handleIdentityCreatedEvent()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 1
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\Configuration\Processor;
20
21
use Broadway\Processor\Processor;
22
use Rhumsaa\Uuid\Uuid;
23
use Surfnet\Stepup\Configuration\Value\Institution;
24
use Surfnet\Stepup\Identity\Event\IdentityCreatedEvent;
25
use Surfnet\Stepup\Identity\Event\InstitutionsAddedToWhitelistEvent;
26
use Surfnet\Stepup\Identity\Event\WhitelistCreatedEvent;
27
use Surfnet\Stepup\Identity\Event\WhitelistReplacedEvent;
28
use Surfnet\StepupMiddleware\ApiBundle\Configuration\Repository\ConfiguredInstitutionRepository;
29
use Surfnet\StepupMiddleware\CommandHandlingBundle\Configuration\Command\CreateInstitutionConfigurationCommand;
30
use Symfony\Component\DependencyInjection\ContainerInterface;
31
32
final class InstitutionConfigurationProcessor extends Processor
33
{
34
    /**
35
     * @var ConfiguredInstitutionRepository
36
     */
37
    private $configuredInstitutionRepository;
38
39
    /**
40
     * @var ContainerInterface
41
     */
42
    private $container;
43
44
    /**
45
     * The container needs to be called during runtime in order to prevent a circular reference
46
     * during container compilation.
47
     *
48
     * @param ConfiguredInstitutionRepository $configuredInstitutionRepository
49
     * @param ContainerInterface $container
50
     */
51
    public function __construct(
52
        ConfiguredInstitutionRepository $configuredInstitutionRepository,
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $configuredInstitutionRepository 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...
53
        ContainerInterface $container
54
    ) {
55
        $this->configuredInstitutionRepository = $configuredInstitutionRepository;
56
        $this->container                       = $container;
57
    }
58
59
    public function handleIdentityCreatedEvent(IdentityCreatedEvent $event)
60
    {
61
        $institution = new Institution($event->identityInstitution->getInstitution());
62
63
        if ($this->configuredInstitutionRepository->hasConfigurationFor($institution)) {
64
            return;
65
        }
66
67
        $this->createConfigurationFor($institution);
68
    }
69
70 View Code Duplication
    public function handleWhitelistCreatedEvent(WhitelistCreatedEvent $event)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
71
    {
72
        foreach ($event->whitelistedInstitutions as $whitelistedInstitution) {
73
            $institution = new Institution($whitelistedInstitution->getInstitution());
74
75
            if ($this->configuredInstitutionRepository->hasConfigurationFor($institution)) {
76
                continue;
77
            }
78
79
            $this->createConfigurationFor($institution);
80
        }
81
    }
82
83 View Code Duplication
    public function handleWhitelistReplacedEvent(WhitelistReplacedEvent $event)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
84
    {
85
        foreach ($event->whitelistedInstitutions as $whitelistedInstitution) {
86
            $institution = new Institution($whitelistedInstitution->getInstitution());
87
88
            if ($this->configuredInstitutionRepository->hasConfigurationFor($institution)) {
89
                continue;
90
            }
91
92
            $this->createConfigurationFor($institution);
93
        }
94
    }
95
96
    public function handleInstitutionsAddedToWhitelistEvent(InstitutionsAddedToWhitelistEvent $event)
97
    {
98
        foreach ($event->addedInstitutions as $addedInstitution) {
99
            $institution = new Institution($addedInstitution->getInstitution());
100
101
            if ($this->configuredInstitutionRepository->hasConfigurationFor($institution)) {
102
                continue;
103
            }
104
105
            $this->createConfigurationFor($institution);
106
        }
107
    }
108
109
    /**
110
     * @param Institution $institution
111
     */
112
    private function createConfigurationFor(Institution $institution)
113
    {
114
        $command              = new CreateInstitutionConfigurationCommand();
115
        $command->UUID        = (string) Uuid::uuid4();
116
        $command->institution = $institution->getInstitution();
117
118
        $this->container->get('pipeline')->process($command);
119
    }
120
}
121