Completed
Push — master ( c7e42b...9e6269 )
by Gareth
03:51
created

deleteContactPhysicalAddressField()   B

Complexity

Conditions 6
Paths 3

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

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