Completed
Push — master ( a7d441...9e9528 )
by Gareth
03:30
created

ItemUpdateBuilder::splitDictionaryUpdateEntries()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 25
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 5.0073

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 25
ccs 14
cts 15
cp 0.9333
rs 8.439
cc 5
eloc 14
nc 4
nop 1
crap 5.0073
1
<?php
2
3
namespace garethp\ews\API;
4
5
class ItemUpdateBuilder
6
{
7 4
    public static function buildUpdateItemChanges($itemType, $uriType, $changes)
8
    {
9 4
        $updateArray = [];
10
11 4
        if (isset($changes['deleteFields'])) {
12 1
            $updateArray['DeleteItemField'] = self::buildUpdateDeleteItemFields($uriType, $changes['deleteFields']);
13 1
            unset($changes['deleteFields']);
14 1
        }
15
16 4
        $updateArray['SetItemField'] = self::buildUpdateSetItemFields($itemType, $uriType, $changes);
17
18 4
        return $updateArray;
19
    }
20
21 1
    protected static function buildUpdateDeleteItemFields($uriType, $changes)
22
    {
23 1
        $deleteItemFields = [];
24 1
        foreach ($changes as $key) {
25 1
            if (strpos($key, 'PhysicalAddress:') === 0 && $uriType == "contacts") {
26
                $deleteItemFields =
27 1
                    self::deleteContactPhysicalAddressField($key, $deleteItemFields);
28 1
            } else {
29 1
                list($fieldUriType, $fieldKey) = self::getFieldURI($uriType, $key);
30 1
                $deleteItemFields[] = [$fieldUriType => $fieldKey];
31
            }
32 1
        }
33
34 1
        return $deleteItemFields;
35
    }
36
37 4
    protected static function buildUpdateSetItemFields($itemType, $uriType, $changes)
38
    {
39 4
        $setItemFields = [];
40 4
        foreach ($changes as $key => $valueArray) {
41 3
            $valueArray = self::splitDictionaryUpdateEntries($valueArray);
42 3
            if (!is_array($valueArray) || Type::arrayIsAssoc($valueArray)) {
43 3
                $valueArray = array($valueArray);
44 3
            }
45
46 3
            foreach ($valueArray as $value) {
47 3
                list ($fieldUriType, $fieldKey, $valueKey, $value) = self::getFieldURI($uriType, $key, $value);
48 3
                $setItemFields[] = array(
49 3
                    $fieldUriType => $fieldKey,
50 3
                    $itemType => [$valueKey => $value]
51 3
                );
52 3
            }
53 4
        }
54
55 4
        return $setItemFields;
56
    }
57
58 1
    protected static function deleteContactPhysicalAddressField($key, $deleteItemFields)
59
    {
60 1
        $key = explode(":", $key);
61 1
        $dictionaryFields = FieldURIManager::getDictionaryURIFields();
62 1
        if (count($key) == 2 && !isset($dictionaryFields['physicaladdress']['contacts'][strtolower($key[1])])) {
63 1
            foreach ($dictionaryFields['physicaladdress']['contacts'] as $uriKey => $uri) {
64 1
                $deleteItemFields[] = ['IndexedFieldURI' => ['FieldURI' => $uri, 'FieldIndex' => $key[1]]];
65 1
            }
66 1
        } elseif (count($key) == 3 && isset($dictionaryFields['physicaladdress']['contacts'][strtolower($key[1])])) {
67 1
            $uri = $dictionaryFields['physicaladdress']['contacts'][strtolower($key[1])];
68 1
            $deleteItemFields[] = ['IndexedFieldURI' => ['FieldURI' => $uri, 'FieldIndex' => $key[2]]];
69 1
        }
70
71 1
        return $deleteItemFields;
72
    }
73
74 3
    protected static function splitDictionaryUpdateEntries($value)
75
    {
76 3
        if (!is_array($value)) {
77 3
            return $value;
78
        }
79
80 1
        reset($value);
81 1
        $fieldKey = key($value);
82
83 1
        if (!is_array($value[$fieldKey]) || empty($value[$fieldKey]['Entry'])) {
84
            return $value;
85
        }
86
87 1
        $entryKey = $value[$fieldKey]['Entry']['Key'];
88 1
        unset($value[$fieldKey]['Entry']['Key']);
89
90 1
        $newValue = [];
91 1
        foreach ($value[$fieldKey]['Entry'] as $key => $updateValue) {
92 1
            $newValue[] = [$fieldKey => ['Entry' => ['Key' => $entryKey, $key => $updateValue]]];
93 1
        }
94
95 1
        $value = $newValue;
96
97 1
        return $value;
98
    }
99
100 4
    protected static function getFieldURI($uriType, $key = null, $value = null)
101
    {
102 4
        if (strpos($key, ':') !== false) {
103
            try {
104 1
                $fieldUriValue = substr($key, 0, strpos($key, ':'));
105
106 1
                list ($key, $index) = explode(':', $key);
107
108 1
                if (is_array($value)) {
109 1
                    $key = key($value);
110 1
                    $value = $value[$key];
111 1
                }
112
113 1
                if (is_array($value) && !empty($value['Entry']) && is_array($value['Entry'])) {
114 1
                    $entryKey = $value['Entry']['Key'];
115 1
                    unset($value['Entry']['Key']);
116 1
                    reset($value['Entry']);
117
118 1
                    $fieldKey = key($value['Entry']);
119 1
                    $value['Entry']['Key'] = $entryKey;
120 1
                    $fieldUri = FieldURIManager::getIndexedFieldUriByName($fieldUriValue, $uriType, $fieldKey);
121 1
                } else {
122
                    $fieldUri = FieldURIManager::getIndexedFieldUriByName($fieldUriValue, $uriType);
123
                }
124
125 1
                return ['IndexedFieldURI', ['FieldURI' => $fieldUri, 'FieldIndex' => $index], $key, $value];
126
            } catch (\Exception $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
127
            }
128
        }
129
130 4
        $fullName = FieldURIManager::getFieldUriByName($key, $uriType);
131
132 4
        return ['FieldURI', ['FieldURI' => $fullName], $key, $value];
133
    }
134
}
135