ContactPersonListFactory::createContactPerson()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.7998
c 0
b 0
f 0
cc 2
nc 2
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 OpenConext\Profile\Assert;
22
use OpenConext\Profile\Value\ContactPerson;
23
use OpenConext\Profile\Value\ContactPersonList;
24
use OpenConext\Profile\Value\ContactType;
25
use OpenConext\Profile\Value\ContactEmailAddress;
26
27
final class ContactPersonListFactory
28
{
29
    /**
30
     * @param mixed $data
31
     * @return ContactPersonList
32
     */
33
    public static function createListFromMetadata($data)
34
    {
35
        Assert::isArray($data, 'Metadata JSON structure must be an associative array, got %s');
36
        Assert::keyExists($data, 'contact_persons', 'Entity JSON structure must contain key "contact_persons"');
37
        Assert::isArray($data['contact_persons'], 'Contact persons JSON structure must be an associative array, got %s');
38
39
        // We cannot use self::class because translation extractions fails on that
40
        $contactPersons = array_map(
41
            ['\OpenConext\EngineBlockApiClientBundle\Value\ContactPersonListFactory', 'createContactPerson'],
42
            $data['contact_persons']
43
        );
44
45
        return new ContactPersonList($contactPersons);
46
    }
47
48
    /**
49
     * @param mixed $data
50
     * @return ContactPerson
51
     *
52
     * @SuppressWarnings(PHPMD.UnusedPrivateMethod)
53
     */
54
    private static function createContactPerson($data)
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
55
    {
56
        Assert::isArray($data, 'Contact person JSON structure must be an associative array, got %s');
57
        Assert::keyExists($data, 'contact_type', 'Contact person JSON structure must contain key "contact_type"');
58
59
        if (!array_key_exists('email_address', $data)) {
60
            return new ContactPerson(new ContactType($data['contact_type']));
61
        }
62
63
        return new ContactPerson(
64
            new ContactType($data['contact_type']),
65
            new ContactEmailAddress($data['email_address'])
66
        );
67
    }
68
}
69