handleRemoveFromWhitelistCommand()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 1
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Copyright 2014 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
 */
0 ignored issues
show
Coding Style introduced by
Missing @link tag in file comment
Loading history...
18
19
namespace Surfnet\StepupMiddleware\CommandHandlingBundle\Identity\CommandHandler;
20
21
use Broadway\CommandHandling\SimpleCommandHandler;
22
use Broadway\Repository\AggregateNotFoundException;
23
use Broadway\Repository\Repository as RepositoryInterface;
24
use Surfnet\Stepup\Identity\Collection\InstitutionCollection;
25
use Surfnet\Stepup\Identity\Value\Institution;
26
use Surfnet\Stepup\Identity\Whitelist;
27
use Surfnet\StepupMiddleware\CommandHandlingBundle\Identity\Command\AddToWhitelistCommand;
28
use Surfnet\StepupMiddleware\CommandHandlingBundle\Identity\Command\RemoveFromWhitelistCommand;
29
use Surfnet\StepupMiddleware\CommandHandlingBundle\Identity\Command\ReplaceWhitelistCommand;
30
31
class WhitelistCommandHandler extends SimpleCommandHandler
0 ignored issues
show
Coding Style introduced by
Missing doc comment for class WhitelistCommandHandler
Loading history...
32
{
33
    public function __construct(
34
        private readonly RepositoryInterface $repository,
35
    ) {
36
    }
37
38
    public function handleReplaceWhitelistCommand(ReplaceWhitelistCommand $command): void
39
    {
40
        $whitelist = $this->getWhitelist();
41
42
        $institutions = $this->mapArrayToInstitutions($command->institutions);
43
        $whitelist->replaceAll(new InstitutionCollection($institutions));
44
45
        $this->repository->save($whitelist);
46
    }
47
48
    public function handleAddToWhitelistCommand(AddToWhitelistCommand $command): void
49
    {
50
        $whitelist = $this->getWhitelist();
51
52
        $institutions = $this->mapArrayToInstitutions($command->institutionsToBeAdded);
53
        $whitelist->add(new InstitutionCollection($institutions));
54
55
        $this->repository->save($whitelist);
56
    }
57
58
    public function handleRemoveFromWhitelistCommand(RemoveFromWhitelistCommand $command): void
59
    {
60
        $whitelist = $this->getWhitelist();
61
62
        $institutions = $this->mapArrayToInstitutions($command->institutionsToBeRemoved);
63
        $whitelist->remove(new InstitutionCollection($institutions));
64
65
        $this->repository->save($whitelist);
66
    }
67
68
    private function getWhitelist(): Whitelist
0 ignored issues
show
Coding Style introduced by
Private method name "WhitelistCommandHandler::getWhitelist" must be prefixed with an underscore
Loading history...
69
    {
70
        try {
71
            $whitelist = $this->repository->load(Whitelist::WHITELIST_AGGREGATE_ID);
72
            assert($whitelist instanceof Whitelist);
73
            return $whitelist;
74
        } catch (AggregateNotFoundException) {
75
            return Whitelist::create(new InstitutionCollection());
76
        }
77
    }
78
79
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $institutions should have a doc-comment as per coding-style.
Loading history...
80
     * @return Institution[]
81
     */
82
    private function mapArrayToInstitutions(array $institutions): array
0 ignored issues
show
Coding Style introduced by
Private method name "WhitelistCommandHandler::mapArrayToInstitutions" must be prefixed with an underscore
Loading history...
83
    {
84
        return array_map(fn($institutionName): Institution => new Institution($institutionName), $institutions);
85
    }
86
}
87