Completed
Push — feature/events-creating-instit... ( bda033 )
by A.
09:59
created

IdentityEventListener::handle()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 7

Duplication

Lines 13
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 13
loc 13
rs 9.4285
cc 2
eloc 7
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\ApiBundle\Configuration\EventListener;
20
21
use Broadway\Domain\DomainMessage;
22
use Broadway\EventHandling\EventListenerInterface;
23
use Rhumsaa\Uuid\Uuid;
24
use Surfnet\Stepup\Configuration\Value\Institution;
25
use Surfnet\Stepup\Identity\Event\IdentityCreatedEvent;
26
use Surfnet\StepupMiddleware\ApiBundle\Configuration\Repository\ConfiguredInstitutionRepository;
27
use Surfnet\StepupMiddleware\CommandHandlingBundle\Configuration\Command\CreateInstitutionConfigurationCommand;
28
use Surfnet\StepupMiddleware\CommandHandlingBundle\Pipeline\Pipeline;
29
30
final class IdentityEventListener implements EventListenerInterface
31
{
32
    /**
33
     * @var ConfiguredInstitutionRepository
34
     */
35
    private $configuredInstitutionRepository;
36
37
    /**
38
     * @var Pipeline
39
     */
40
    private $pipeline;
41
42
    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...
43
    {
44
        $this->configuredInstitutionRepository = $configuredInstitutionRepository;
45
        $this->pipeline                        = $pipeline;
46
    }
47
48
    /**
49
     * @param DomainMessage $domainMessage
50
     */
51 View Code Duplication
    public function handle(DomainMessage $domainMessage)
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...
52
    {
53
        $event  = $domainMessage->getPayload();
54
55
        $classParts = explode('\\', get_class($event));
56
        $method = 'apply' . end($classParts);
57
58
        if (!method_exists($this, $method)) {
59
            return;
60
        }
61
62
        $this->$method($event, $domainMessage);
63
    }
64
65
    public function applyIdentityCreatedEvent(IdentityCreatedEvent $event)
66
    {
67
        $institution = new Institution($event->identityInstitution->getInstitution());
68
69
        if ($this->configuredInstitutionRepository->hasConfigurationFor($institution)) {
70
            return;
71
        }
72
73
        $command              = new CreateInstitutionConfigurationCommand();
74
        $command->UUID        = (string) Uuid::uuid4();
75
        $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...
76
77
        $this->pipeline->process($command);
78
    }
79
}
80