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

FieldURIManager::getIndexedFieldUriByName()   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 26
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 26
ccs 0
cts 15
cp 0
rs 8.439
cc 6
eloc 15
nc 5
nop 3
crap 42
1
<?php
2
3
namespace garethp\ews\API;
4
5
use garethp\ews\API\Enumeration\DictionaryURIType;
6
use garethp\ews\API\Enumeration\UnindexedFieldURIType;
7
use garethp\ews\API\Exception\ExchangeException;
8
9
class FieldURIManager
10
{
11
    protected static $unIndexedFieldURIs = [];
12
    protected static $dictionaryFieldURIs = [];
13
14
    protected static function setupFieldUris()
15
    {
16
        if (!empty(self::$dictionaryFieldURIs) && !empty(self::$dictionaryFieldURIs)) {
17
            return;
18
        }
19
20
        self::$unIndexedFieldURIs = self::getFieldUrisFromClass(UnindexedFieldURIType::class);
21
        self::$dictionaryFieldURIs = self::getFieldUrisFromClass(DictionaryURIType::class);
22
    }
23
24
    public static function getDictionaryURIFields()
25
    {
26
        self::setupFieldUris();
27
        return self::$dictionaryFieldURIs;
28
    }
29
30 1
    protected static function getFieldUrisFromClass($className)
31
    {
32
        //So, since we have to pass in URI's of everything we update, we need to fetch them
33 1
        $reflection = new \ReflectionClass($className);
34 1
        $constants = $reflection->getConstants();
35 1
        $constantsFound = array();
36
37
        //Loop through all URI's to list them in an array
38 1
        foreach ($constants as $constant) {
39 1
            $exploded = explode(":", strtolower($constant));
40 1
            if (count($exploded) == 1) {
41 1
                $exploded = ['item', $exploded[0]];
42 1
            }
43
44
            //It starts in order ['contacts', 'physicaladdress', 'city], we want it in
45
            //['physicaladdress', 'contacts', 'city]
46 1
            $temp = $exploded[0];
47 1
            $exploded[0] = $exploded[1];
48 1
            $exploded[1] = $temp;
49
50 1
            $depth = count($exploded) - 1;
51 1
            $current = &$constantsFound;
52
53
            //Use the exploded array as a way to create a multidimensional array. For example,
54
            //['contacts', 'physicaladdress', 'city'] becomes
55
            //['contacts' => ['physicaladdress' => ['city' => 'contacts:PhysicalAddress:City']]]
56 1
            foreach ($exploded as $count => $key) {
57 1
                if ($count < $depth && !isset($current[$key])) {
58 1
                    $current[$key] = array();
59 1
                }
60
61 1
                if ($count < $depth) {
62 1
                    $current = &$current[$key];
63 1
                    continue;
64
                }
65
66 1
                $current[$key] = $constant;
67 1
            }
68 1
        }
69
70 1
        return $constantsFound;
71
    }
72
73
    public static function getFieldUriByName($fieldName, $preference = 'item')
74
    {
75
        self::setupFieldUris();
76
        $fieldName = strtolower($fieldName);
77
        $preference = strtolower($preference);
78
79
        if (!isset(self::$unIndexedFieldURIs[$fieldName])) {
80
            return false;
81
        }
82
83
        if (!isset(self::$unIndexedFieldURIs[$fieldName][$preference])) {
84
            $preference = 'item';
85
        }
86
87
        if (!isset(self::$unIndexedFieldURIs[$fieldName][$preference])) {
88
            throw new ExchangeException("Could not find uri $preference:$fieldName");
89
        }
90
91
        return self::$unIndexedFieldURIs[$fieldName][$preference];
92
    }
93
94
    public static function getIndexedFieldUriByName($fieldName, $preference = 'item', $entryKey = false)
95
    {
96
        self::setupFieldUris();
97
        $fieldName = strtolower($fieldName);
98
        $preference = strtolower($preference);
99
        $entryKey = strtolower($entryKey);
100
101
        if (!isset(self::$dictionaryFieldURIs[$fieldName])) {
102
            return false;
103
        }
104
105
        if (!isset(self::$dictionaryFieldURIs[$fieldName][$preference])) {
106
            throw new ExchangeException("Could not find uri $preference:$fieldName");
107
        }
108
109
        $fieldUri = self::$dictionaryFieldURIs[$fieldName][$preference];
110
        if (!is_array($fieldUri)) {
111
            return $fieldUri;
112
        }
113
114
        if (!$entryKey || !isset($fieldUri[$entryKey])) {
115
            throw new ExchangeException("Could not find FieldURI");
116
        }
117
118
        return $fieldUri[$entryKey];
119
    }
120
}
121