Completed
Push — develop ( fac452...d8e37f )
by A.
06:03 queued 02:16
created

ConsentListFactory::createListFromMetadata()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 12
rs 9.4286
cc 1
eloc 6
nc 1
nop 1
1
<?php
2
3
/**
4
 * Copyright 2015 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 OpenConext\EngineBlockApiClientBundle\Value;
20
21
use DateTime;
22
use DateTimeImmutable;
23
use OpenConext\Profile\Assert;
24
use OpenConext\Profile\Value\Consent;
25
use OpenConext\Profile\Value\Consent\ServiceProvider;
26
use OpenConext\Profile\Value\ConsentList;
27
use OpenConext\Profile\Value\ConsentType;
28
use OpenConext\Profile\Value\DisplayName;
29
use OpenConext\Profile\Value\EmailAddress;
30
use OpenConext\Profile\Value\Entity;
31
use OpenConext\Profile\Value\EntityId;
32
use OpenConext\Profile\Value\EntityType;
33
use OpenConext\Profile\Value\Url;
34
35
/**
36
 * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
37
 */
38
final class ConsentListFactory
39
{
40
    /**
41
     * @param mixed $data
42
     * @return ConsentList
43
     */
44
    public static function createListFromMetadata($data)
45
    {
46
        Assert::isArray($data, 'Consent list JSON structure must be an associative array, got %s');
47
48
        // We cannot use self::class because translation extractions fails on that
49
        $consents = array_map(
50
            ['\OpenConext\EngineBlockApiClientBundle\Value\ConsentListFactory', 'createConsent'],
51
            $data
52
        );
53
54
        return new ConsentList($consents);
55
    }
56
57
    /**
58
     * @param mixed $data
59
     * @return Consent
60
     *
61
     * @SuppressWarnings(PHPMD.UnusedPrivateMethod)
62
     */
63
    private static function createConsent($data)
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
64
    {
65
        Assert::keyExists($data, 'service_provider', 'Consent JSON structure must contain key "service_provider"');
66
        Assert::keyExists($data, 'consent_given_on', 'Consent JSON structure must contain key "consent_given_on"');
67
        Assert::keyExists($data, 'last_used_on', 'Consent JSON structure must contain key "last_used_on"');
68
        Assert::keyExists($data, 'consent_type', 'Consent JSON structure must contain key "consent_type"');
69
70
        Assert::choice(
71
            $data['consent_type'],
72
            [ConsentType::TYPE_EXPLICIT, ConsentType::TYPE_IMPLICIT],
73
            '"%s" is not one of the valid ConsentTypes: %s'
74
        );
75
76
        $consentGivenOn = DateTimeImmutable::createFromFormat(DateTime::ATOM, $data['consent_given_on']);
77
        $lastUsedOn = DateTimeImmutable::createFromFormat(DateTime::ATOM, $data['last_used_on']);
78
79
        Assert::isInstanceOf(
80
            $consentGivenOn,
81
            // We cannot use DateTimeImmutable::class because translation extractions fails on that
82
            '\DateTimeImmutable',
83
            sprintf(
84
                'Consent given on date must be formatted according to the ISO8601 standard, got "%s"',
85
                $data['consent_given_on']
86
            )
87
        );
88
        Assert::isInstanceOf(
89
            $lastUsedOn,
90
            // We cannot use DateTimeImmutable::class because translation extractions fails on that
91
            '\DateTimeImmutable',
92
            sprintf(
93
                'Last used on date must be formatted according to the ISO8601 standard, got "%s"',
94
                $data['last_used_on']
95
            )
96
        );
97
98
        if ($data['consent_type'] === ConsentType::TYPE_EXPLICIT) {
99
            $consentType = ConsentType::explicit();
100
        } else {
101
            $consentType = ConsentType::implicit();
102
        }
103
104
        return new Consent(
105
            self::createServiceProvider($data['service_provider']),
106
            $consentGivenOn,
107
            $lastUsedOn,
108
            $consentType
109
        );
110
    }
111
112
    /**
113
     * @param mixed $data
114
     * @return ServiceProvider
115
     */
116
    private static function createServiceProvider($data)
117
    {
118
        Assert::keyExists($data, 'entity_id', 'Consent JSON structure must contain key "entity_id"');
119
        Assert::keyExists($data, 'display_name', 'Consent JSON structure must contain key "display_name"');
120
        Assert::keyExists($data, 'eula_url', 'Consent JSON structure must contain key "eula_url"');
121
        Assert::keyExists($data, 'support_email', 'Consent JSON structure must contain key "support_email"');
122
123
        $entity       = new Entity(new EntityId($data['entity_id']), EntityType::SP());
124
        $displayName  = new DisplayName($data['display_name']);
125
        $eulaUrl      = null;
126
        $supportEmail = null;
127
128
        if ($data['eula_url'] !== null) {
129
            $eulaUrl = new Url($data['eula_url']);
130
        }
131
132
        if ($data['support_email'] !== null) {
133
            $supportEmail = new EmailAddress($data['support_email']);
134
        }
135
136
        return new ServiceProvider($entity, $displayName, $eulaUrl, $supportEmail);
137
    }
138
}
139