Completed
Push — master ( 617cd0...5e87a2 )
by Gareth
08:23
created

FieldURIManager::getDictionaryURIFields()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 5
ccs 0
cts 0
cp 0
rs 9.4285
cc 1
eloc 3
nc 1
nop 0
crap 2
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
    /**
15
     * @deprecated This will be made protected in 0.9
16
     */
17 5
    public static function setupFieldUris()
18
    {
19 5
        if (!empty(self::$dictionaryFieldURIs) && !empty(self::$dictionaryFieldURIs)) {
20 5
            return;
21
        }
22
23 1
        self::$unIndexedFieldURIs = self::getFieldUrisFromClass(UnindexedFieldURIType::class);
0 ignored issues
show
Deprecated Code introduced by
The method garethp\ews\API\FieldURI...getFieldUrisFromClass() has been deprecated with message: This will be made protected in 0.9

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
24 1
        self::$dictionaryFieldURIs = self::getFieldUrisFromClass(DictionaryURIType::class);
0 ignored issues
show
Deprecated Code introduced by
The method garethp\ews\API\FieldURI...getFieldUrisFromClass() has been deprecated with message: This will be made protected in 0.9

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
25
    }
26
27
    public static function getDictionaryURIFields()
28
    {
29
        self::setupFieldUris();
0 ignored issues
show
Deprecated Code introduced by
The method garethp\ews\API\FieldURIManager::setupFieldUris() has been deprecated with message: This will be made protected in 0.9

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
30
        return self::$dictionaryFieldURIs;
31
    }
32
33 1
    /**
34
     * @deprecated This will be made protected in 0.9
35
     *
36 1
     * @param $className
37 1
     * @return array
38 1
     */
39
    public static function getFieldUrisFromClass($className)
40
    {
41 1
        //So, since we have to pass in URI's of everything we update, we need to fetch them
42 1
        $reflection = new \ReflectionClass($className);
43 1
        $constants = $reflection->getConstants();
44 1
        $constantsFound = array();
45
46
        //Loop through all URI's to list them in an array
47
        foreach ($constants as $constant) {
48
            $exploded = explode(":", strtolower($constant));
49 1
            if (count($exploded) == 1) {
50 1
                $exploded = ['item', $exploded[0]];
51 1
            }
52
53 1
            //It starts in order ['contacts', 'physicaladdress', 'city], we want it in
54 1
            //['physicaladdress', 'contacts', 'city]
55
            $temp = $exploded[0];
56
            $exploded[0] = $exploded[1];
57
            $exploded[1] = $temp;
58
59 1
            $depth = count($exploded) - 1;
60 1
            $current = &$constantsFound;
61 1
62
            //Use the exploded array as a way to create a multidimensional array. For example,
63
            //['contacts', 'physicaladdress', 'city'] becomes
64 1
            //['contacts' => ['physicaladdress' => ['city' => 'contacts:PhysicalAddress:City']]]
65 1
            foreach ($exploded as $count => $key) {
66 1
                if ($count < $depth && !isset($current[$key])) {
67
                    $current[$key] = array();
68
                }
69 1
70
                if ($count < $depth) {
71
                    $current = &$current[$key];
72
                    continue;
73 1
                }
74
75
                $current[$key] = $constant;
76 5
            }
77
        }
78 5
79 5
        return $constantsFound;
80 5
    }
81
82 5
    public static function getFieldUriByName($fieldName, $preference = 'item')
83 1
    {
84
        self::setupFieldUris();
0 ignored issues
show
Deprecated Code introduced by
The method garethp\ews\API\FieldURIManager::setupFieldUris() has been deprecated with message: This will be made protected in 0.9

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
85
        $fieldName = strtolower($fieldName);
86 5
        $preference = strtolower($preference);
87 1
88
        if (!isset(self::$unIndexedFieldURIs[$fieldName])) {
89
            return false;
90 5
        }
91
92
        if (!isset(self::$unIndexedFieldURIs[$fieldName][$preference])) {
93
            $preference = 'item';
94 5
        }
95
96
        if (!isset(self::$unIndexedFieldURIs[$fieldName][$preference])) {
97 1
            throw new ExchangeException("Could not find uri $preference:$fieldName");
98
        }
99 1
100 1
        return self::$unIndexedFieldURIs[$fieldName][$preference];
101 1
    }
102
103 1
    public static function getIndexedFieldUriByName($fieldName, $preference = 'item', $entryKey = false)
104
    {
105
        self::setupFieldUris();
0 ignored issues
show
Deprecated Code introduced by
The method garethp\ews\API\FieldURIManager::setupFieldUris() has been deprecated with message: This will be made protected in 0.9

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
106
        $fieldName = strtolower($fieldName);
107 1
        $preference = strtolower($preference);
108
109
        if (!isset(self::$dictionaryFieldURIs[$fieldName])) {
110
            return false;
111 1
        }
112 1
113 1
        if (!isset(self::$dictionaryFieldURIs[$fieldName][$preference])) {
114
            throw new ExchangeException("Could not find uri $preference:$fieldName");
115
        }
116 1
117
        $fieldUri = self::$dictionaryFieldURIs[$fieldName][$preference];
118
        if (!is_array($fieldUri)) {
119
            return $fieldUri;
120 1
        }
121
122
        if (!$entryKey || !isset($fieldUri[$entryKey])) {
123
            throw new ExchangeException("Could not find FieldURI");
124
        }
125
126
        return $fieldUri[$entryKey];
127
    }
128
}
129