Completed
Pull Request — develop (#302)
by Michiel
04:10 queued 02:06
created

AbstractBootstrapCommand::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 9.7333
c 0
b 0
f 0
cc 1
nc 1
nop 6
1
<?php
2
3
/**
4
 * Copyright 2020 SURFnet bv
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\MiddlewareBundle\Console\Command;
20
21
use Broadway\EventHandling\EventBusInterface;
22
use Rhumsaa\Uuid\Uuid;
23
use Surfnet\StepupMiddleware\CommandHandlingBundle\Command\Command as MiddlewareCommand;
24
use Surfnet\StepupMiddleware\CommandHandlingBundle\Command\Metadata;
25
use Surfnet\StepupMiddleware\CommandHandlingBundle\EventSourcing\MetadataEnricher;
26
use Surfnet\StepupMiddleware\CommandHandlingBundle\Identity\Command\VetSecondFactorCommand;
27
use Surfnet\StepupMiddleware\CommandHandlingBundle\Pipeline\Pipeline;
28
use Surfnet\StepupMiddleware\MiddlewareBundle\Service\DBALConnectionHelper;
29
use Surfnet\StepupMiddleware\MiddlewareBundle\Service\TokenBootstrapService;
30
use Symfony\Component\Console\Command\Command;
31
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
32
33
abstract class AbstractBootstrapCommand extends Command
34
{
35
    /** @var Pipeline  */
36
    private $pipeline;
37
    /** @var EventBusInterface  */
38
    private $eventBus;
39
    /** @var DBALConnectionHelper  */
40
    private $connection;
41
    /** @var TokenStorageInterface */
42
    protected $tokenStorage;
43
    /** @var MetadataEnricher */
44
    private $enricher;
45
    /** @var TokenBootstrapService */
46
    protected $tokenBootstrapService;
47
48
    public function __construct(
49
        Pipeline $pipeline,
50
        EventBusInterface $eventBus,
51
        DBALConnectionHelper $connection,
52
        MetadataEnricher $enricher,
53
        TokenStorageInterface $tokenStorage,
54
        TokenBootstrapService $tokenBootstrapService
55
    ) {
56
        $this->pipeline = $pipeline;
57
        $this->eventBus = $eventBus;
58
        $this->connection = $connection;
59
        $this->enricher = $enricher;
60
        $this->tokenStorage = $tokenStorage;
61
        $this->tokenBootstrapService = $tokenBootstrapService;
62
        parent::__construct();
63
    }
64
65
    protected function beginTransaction()
66
    {
67
        $this->connection->beginTransaction();
68
    }
69
70
    protected function finishTransaction()
71
    {
72
        $this->eventBus->flush();
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Broadway\EventHandling\EventBusInterface as the method flush() does only exist in the following implementations of said interface: Surfnet\StepupMiddleware...ndling\BufferedEventBus.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
73
        $this->connection->commit();
74
    }
75
76
    protected function rollback()
77
    {
78
        $this->connection->rollBack();
79
    }
80
81
    protected function process(MiddlewareCommand $command)
82
    {
83
        $this->pipeline->process($command);
84
    }
85
86
    protected function requiresMailVerification($institution)
87
    {
88
        $configuration = $this->tokenBootstrapService->findConfigurationOptionsFor($institution);
89
        if ($configuration) {
90
            return $configuration->verifyEmailOption->isEnabled();
91
        }
92
        return true;
93
    }
94
95
    protected function vetSecondFactor($tokenType, $actorId, $identity, $secondFactorId, $verifiedSecondFactor, $phoneNumber)
96
    {
97
        $command = new VetSecondFactorCommand();
98
        $command->UUID = (string) Uuid::uuid4();
99
        $command->authorityId = $actorId;
100
        $command->identityId = $identity->id;
101
        $command->secondFactorId = $secondFactorId;
102
        $command->registrationCode = $verifiedSecondFactor->registrationCode;
103
        $command->secondFactorType = $tokenType;
104
        $command->secondFactorIdentifier = $phoneNumber;
105
        $command->documentNumber = '123987';
106
        $command->identityVerified = true;
107
        $this->pipeline->process($command);
108
    }
109
110
    protected function enrichEventMetadata($actorId)
111
    {
112
        $actor = $this->tokenBootstrapService->findIdentityById($actorId);
113
        $metadata = new Metadata();
114
        $metadata->actorId = $actor->id;
115
        $metadata->actorInstitution = $actor->institution;
116
        $this->enricher->setMetadata($metadata);
117
    }
118
}
119