Completed
Pull Request — develop (#38)
by A.
02:23
created

ContactPersonListFactory::createContactPerson()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 10
rs 9.4286
cc 2
eloc 5
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\EmailAddress;
26
27
final class ContactPersonListFactory
28
{
29
    /**
30
     * @param array $data
31
     * @return ContactPersonList
32
     *
33
     */
34
    public static function create(array $data)
35
    {
36
        Assert::isArray($data, 'Contact list JSON structure must be an associative array, got %s');
37
        Assert::keyExists($data, 'contact_persons', 'Entity JSON structure must contain key "contact_persons"');
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 array $data
50
     * @return ContactPerson
51
     *
52
     * @SuppressWarnings(PHPMD.UnusedPrivateMethod)
53
     */
54
    private static function createContactPerson(array $data)
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
55
    {
56
        Assert::keyExists($data, 'contact_type', 'Contact person JSON structure must contain key "contact_type"');
57
58
        if (!array_key_exists('email_address', $data)) {
59
            return new ContactPerson(new ContactType($data['contact_type']));
60
        }
61
62
        return new ContactPerson(new ContactType($data['contact_type']), new EmailAddress($data['email_address']));
63
    }
64
}
65