Completed
Push — develop ( 7a755c...9656fb )
by
unknown
02:36 queued 10s
created

ConsentListFactory   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 91
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 12

Importance

Changes 0
Metric Value
wmc 6
lcom 0
cbo 12
dl 0
loc 91
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A createListFromMetadata() 0 12 1
B createConsent() 0 36 2
B createServiceProvider() 0 24 3
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\NameIdFormat;
30
use OpenConext\Profile\Value\ContactEmailAddress;
31
use OpenConext\Profile\Value\Entity;
32
use OpenConext\Profile\Value\EntityId;
33
use OpenConext\Profile\Value\EntityType;
34
use OpenConext\Profile\Value\Url;
35
36
/**
37
 * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
38
 */
39
final class ConsentListFactory
40
{
41
    /**
42
     * @param mixed $data
43
     * @return ConsentList
44
     */
45
    public static function createListFromMetadata($data)
46
    {
47
        Assert::isArray($data, 'Consent list JSON structure must be an associative array, got %s');
48
49
        // We cannot use self::class because translation extractions fails on that
50
        $consents = array_map(
51
            ['\OpenConext\EngineBlockApiClientBundle\Value\ConsentListFactory', 'createConsent'],
52
            $data
53
        );
54
55
        return new ConsentList($consents);
56
    }
57
58
    /**
59
     * @param mixed $data
60
     * @return Consent
61
     *
62
     * @SuppressWarnings(PHPMD.UnusedPrivateMethod)
63
     */
64
    private static function createConsent($data)
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
65
    {
66
        Assert::keyExists($data, 'service_provider', 'Consent JSON structure must contain key "service_provider"');
67
        Assert::keyExists($data, 'consent_given_on', 'Consent JSON structure must contain key "consent_given_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
78
        Assert::isInstanceOf(
79
            $consentGivenOn,
80
            // We cannot use DateTimeImmutable::class because translation extractions fails on that
81
            '\DateTimeImmutable',
82
            sprintf(
83
                'Consent given on date must be formatted according to the ISO8601 standard, got "%s"',
84
                $data['consent_given_on']
85
            )
86
        );
87
88
        if ($data['consent_type'] === ConsentType::TYPE_EXPLICIT) {
89
            $consentType = ConsentType::explicit();
90
        } else {
91
            $consentType = ConsentType::implicit();
92
        }
93
94
        return new Consent(
95
            self::createServiceProvider($data['service_provider']),
96
            $consentGivenOn,
97
            $consentType
98
        );
99
    }
100
101
    /**
102
     * @param mixed $data
103
     * @return ServiceProvider
104
     */
105
    private static function createServiceProvider($data)
106
    {
107
        Assert::keyExists($data, 'entity_id', 'Consent JSON structure must contain key "entity_id"');
108
        Assert::keyExists($data, 'display_name', 'Consent JSON structure must contain key "display_name"');
109
        Assert::keyExists($data, 'eula_url', 'Consent JSON structure must contain key "eula_url"');
110
        Assert::keyExists($data, 'support_email', 'Consent JSON structure must contain key "support_email"');
111
        Assert::keyExists($data, 'name_id_format', 'Consent JSON structure must contain key "name_id_format"');
112
113
        $entity       = new Entity(new EntityId($data['entity_id']), EntityType::SP());
114
        $displayName  = new DisplayName($data['display_name']);
115
        $nameIdFormat = new NameIdFormat($data['name_id_format']);
116
        $eulaUrl      = null;
117
        $supportEmail = null;
118
119
        if ($data['eula_url'] !== null) {
120
            $eulaUrl = new Url($data['eula_url']);
121
        }
122
123
        if ($data['support_email'] !== null) {
124
            $supportEmail = new ContactEmailAddress($data['support_email']);
125
        }
126
127
        return new ServiceProvider($entity, $displayName, $nameIdFormat, $eulaUrl, $supportEmail);
128
    }
129
}
130