setSensitiveData()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Copyright 2018 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\Event;
20
21
use Surfnet\Stepup\Identity\AuditLog\Metadata;
22
use Surfnet\Stepup\Identity\Value\CommonName;
23
use Surfnet\Stepup\Identity\Value\Email;
24
use Surfnet\Stepup\Identity\Value\IdentityId;
25
use Surfnet\Stepup\Identity\Value\Institution;
26
use Surfnet\Stepup\Identity\Value\NameId;
27
use Surfnet\StepupMiddleware\CommandHandlingBundle\SensitiveData\Forgettable;
28
use Surfnet\StepupMiddleware\CommandHandlingBundle\SensitiveData\RightToObtainDataInterface;
29
use Surfnet\StepupMiddleware\CommandHandlingBundle\SensitiveData\SensitiveData;
30
31
class RegistrationAuthorityRetractedForInstitutionEvent extends IdentityEvent implements
32
    Forgettable,
33
    RightToObtainDataInterface
34
{
35
    /**
36
     * @var string[]
37
     */
38
    private array $allowlist = [
39
        'identity_id',
40
        'identity_institution',
41
        'name_id',
42
        'ra_institution',
43
        'email',
44
        'common_name',
45
    ];
46
47
    public function __construct(
48
        IdentityId $identityId,
49
        Institution $institution,
50
        public NameId $nameId,
51
        public CommonName $commonName,
52
        public Email $email,
53
        public Institution $raInstitution,
54
    ) {
55
        parent::__construct($identityId, $institution);
56
    }
57
58
    public function getAuditLogMetadata(): Metadata
59
    {
60
        $metadata = new Metadata();
61
        $metadata->identityId = $this->identityId;
62
        $metadata->identityInstitution = $this->identityInstitution;
63
64
        return $metadata;
65
    }
66
67
    public static function deserialize(array $data): self
68
    {
69
        return new self(
70
            new IdentityId($data['identity_id']),
71
            new Institution($data['identity_institution']),
72
            new NameId($data['name_id']),
73
            CommonName::unknown(),
74
            Email::unknown(),
75
            new Institution($data['ra_institution']),
76
        );
77
    }
78
79
    /**
80
     * The data ending up in the event_stream, be careful not to include sensitive data here!
81
     *
82
     * @return array<string, mixed>
83
     */
84
    public function serialize(): array
85
    {
86
        return [
87
            'identity_id' => (string)$this->identityId,
88
            'identity_institution' => (string)$this->identityInstitution,
89
            'name_id' => (string)$this->nameId,
90
            'ra_institution' => (string)$this->raInstitution,
91
        ];
92
    }
93
94
    public function getSensitiveData(): SensitiveData
95
    {
96
        return (new SensitiveData)
0 ignored issues
show
Coding Style introduced by
Parentheses must be used when instantiating a new class
Loading history...
97
            ->withCommonName($this->commonName)
98
            ->withEmail($this->email);
99
    }
100
101
    public function setSensitiveData(SensitiveData $sensitiveData): void
102
    {
103
        $this->email = $sensitiveData->getEmail();
104
        $this->commonName = $sensitiveData->getCommonName();
105
    }
106
107
    public function obtainUserData(): array
108
    {
109
        $serializedPublicUserData = $this->serialize();
110
        $serializedSensitiveUserData = $this->getSensitiveData()->serialize();
111
        return array_merge($serializedPublicUserData, $serializedSensitiveUserData);
112
    }
113
114
    /**
115
     * @return string[]
116
     */
117
    public function getAllowlist(): array
118
    {
119
        return $this->allowlist;
120
    }
121
}
122