Completed
Push — feature/events-creating-instit... ( d6e8c9...6b4a02 )
by A.
04:39
created

applyWhitelistCreatedEvent()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 12
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 12
loc 12
rs 9.4285
cc 3
eloc 6
nc 3
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 Surfnet\StepupMiddleware\CommandHandlingBundle\Pipeline\Pipeline;
31
32
final class InstitutionConfigurationProcessor extends Processor
33
{
34
    /**
35
     * @var ConfiguredInstitutionRepository
36
     */
37
    private $configuredInstitutionRepository;
38
39
    /**
40
     * @var Pipeline
41
     */
42
    private $pipeline;
43
44
    public function __construct(ConfiguredInstitutionRepository $configuredInstitutionRepository, Pipeline $pipeline)
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...
45
    {
46
        $this->configuredInstitutionRepository = $configuredInstitutionRepository;
47
        $this->pipeline = $pipeline;
48
    }
49
50
    public function applyIdentityCreatedEvent(IdentityCreatedEvent $event)
51
    {
52
        $institution = new Institution($event->identityInstitution->getInstitution());
53
54
        if ($this->configuredInstitutionRepository->hasConfigurationFor($institution)) {
55
            return;
56
        }
57
58
        $this->createConfigurationFor($institution);
59
    }
60
61 View Code Duplication
    public function applyWhitelistCreatedEvent(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...
62
    {
63
        foreach ($event->whitelistedInstitutions as $whitelistedInstitution) {
64
            $institution = new Institution($whitelistedInstitution->getInstitution());
65
66
            if ($this->configuredInstitutionRepository->hasConfigurationFor($institution)) {
67
                continue;
68
            }
69
70
            $this->createConfigurationFor($institution);
71
        }
72
    }
73
74 View Code Duplication
    public function applyWhitelistReplacedEvent(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...
75
    {
76
        foreach ($event->whitelistedInstitutions as $whitelistedInstitution) {
77
            $institution = new Institution($whitelistedInstitution->getInstitution());
78
79
            if ($this->configuredInstitutionRepository->hasConfigurationFor($institution)) {
80
                continue;
81
            }
82
83
            $this->createConfigurationFor($institution);
84
        }
85
    }
86
87
    public function applyInstitutionsAddedToWhitelistEvent(InstitutionsAddedToWhitelistEvent $event)
88
    {
89
        foreach ($event->addedInstitutions as $addedInstitution) {
90
            $institution = new Institution($addedInstitution->getInstitution());
91
92
            if ($this->configuredInstitutionRepository->hasConfigurationFor($institution)) {
93
                continue;
94
            }
95
96
            $this->createConfigurationFor($institution);
97
        }
98
    }
99
100
    /**
101
     * @param Institution $institution
102
     */
103 View Code Duplication
    private function createConfigurationFor(Institution $institution)
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...
104
    {
105
        $command              = new CreateInstitutionConfigurationCommand();
106
        $command->UUID        = (string) Uuid::uuid4();
107
        $command->institution = $institution;
0 ignored issues
show
Documentation Bug introduced by
It seems like $institution of type object<Surfnet\Stepup\Co...tion\Value\Institution> is incompatible with the declared type string of property $institution.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
108
109
        $this->pipeline->process($command);
110
    }
111
}
112