IdentityRepository   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 17
dl 0
loc 37
rs 10
c 0
b 0
f 0
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 14 1
A obtainInformation() 0 15 4
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
 */
18
19
namespace Surfnet\Stepup\Identity\EventSourcing;
20
21
use Broadway\Domain\DomainMessage;
22
use Broadway\EventHandling\EventBus as EventBusInterface;
23
use Broadway\EventSourcing\AggregateFactory\AggregateFactory;
24
use Broadway\EventSourcing\EventSourcingRepository;
25
use Broadway\EventSourcing\EventStreamDecorator;
26
use Broadway\EventStore\EventStore as EventStoreInterface;
27
use Broadway\EventStore\EventStreamNotFoundException;
28
use Broadway\Repository\AggregateNotFoundException;
29
use Psr\Log\LoggerInterface;
30
use Surfnet\Stepup\Helper\UserDataFilterInterface;
31
use Surfnet\Stepup\Identity\Identity;
32
use Surfnet\Stepup\Identity\Value\IdentityId;
33
use Surfnet\StepupMiddleware\CommandHandlingBundle\SensitiveData\RightToObtainDataInterface;
34
35
class IdentityRepository extends EventSourcingRepository
36
{
37
    /**
38
     * @param EventStreamDecorator[] $eventStreamDecorators
39
     */
40
    public function __construct(
41
        protected EventStoreInterface $events,
42
        EventBusInterface $eventBus,
43
        AggregateFactory $aggregateFactory,
44
        private readonly UserDataFilterInterface $userDataFilter,
45
        protected LoggerInterface $logger,
46
        array $eventStreamDecorators = [],
47
    ) {
48
        parent::__construct(
49
            $this->events,
50
            $eventBus,
51
            Identity::class,
52
            $aggregateFactory,
53
            $eventStreamDecorators,
54
        );
55
    }
56
57
    public function obtainInformation(IdentityId $id): array
58
    {
59
        try {
60
            $domainEventStream = $this->events->load($id);
61
            $data = [];
62
            /** @var DomainMessage $domainMessage */
63
            foreach ($domainEventStream as $domainMessage) {
64
                if ($domainMessage->getPayload() instanceof RightToObtainDataInterface) {
65
                    $index = $domainMessage->getPlayhead() . '-' . $domainMessage->getType();
66
                    $data[$index] = $this->userDataFilter->filter($domainMessage->getPayload());
67
                }
68
            }
69
            return $data;
70
        } catch (EventStreamNotFoundException $e) {
71
            throw AggregateNotFoundException::create($id, $e);
72
        }
73
    }
74
}
75