Completed
Pull Request — master (#66)
by marijn
05:49
created

ItemUpdateBuilder::splitDictionaryUpdateEntries()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 25
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

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