Completed
Push — master ( b05bb8...7c707d )
by Gareth
03:25
created

FieldURIManager::getFieldUriByName()   B

Complexity

Conditions 5
Paths 10

Size

Total Lines 23
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 5.009

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 23
ccs 13
cts 14
cp 0.9286
rs 8.5906
cc 5
eloc 12
nc 10
nop 2
crap 5.009
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 1
    public static function setupFieldUris()
18
    {
19 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...
20
21 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...
22 1
    }
23
24
    /**
25
     * @deprecated This will be made protected in 0.9
26
     *
27
     * @param $className
28
     * @return array
29
     */
30 1
    public 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(":", $constant);
40 1
            if (count($exploded) == 1) {
41 1
                $exploded = ['item', $exploded[0]];
42 1
            }
43
44 1
            $name = strtolower($exploded[1]);
45 1
            $category = strtolower($exploded[0]);
46
47 1
            if (!isset($constantsFound[$name])) {
48 1
                $constantsFound[$name] = array();
49 1
            }
50 1
            if (count($exploded) == 3) {
51 1
                $entry = strtolower($exploded[2]);
52 1
                if (!isset($constantsFound[$name])) {
53
                    $constantsFound[$name][$category] = array();
54
                }
55
56 1
                $constantsFound[$name][$category][$entry] = $constant;
57 1
            } else {
58 1
                $constantsFound[$name][$category] = $constant;
59
            }
60 1
        }
61
62 1
        return $constantsFound;
63
    }
64
65 5
    public static function getFieldUriByName($fieldName, $preference = 'item')
66
    {
67 5
        $fieldName = strtolower($fieldName);
68 5
        $preference = strtolower($preference);
69
70 5
        if (empty(self::$unIndexedFieldURIs)) {
71 1
            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...
72 1
        }
73
74 5
        if (!isset(self::$unIndexedFieldURIs[$fieldName])) {
75 1
            return false;
76
        }
77
78 5
        if (!isset(self::$unIndexedFieldURIs[$fieldName][$preference])) {
79 1
            $preference = 'item';
80 1
        }
81
82 5
        if (!isset(self::$unIndexedFieldURIs[$fieldName][$preference])) {
83
            throw new ExchangeException("Could not find uri $preference:$fieldName");
84
        }
85
86 5
        return self::$unIndexedFieldURIs[$fieldName][$preference];
87
    }
88
89 1
    public static function getIndexedFieldUriByName($fieldName, $preference = 'item', $entryKey = false)
90
    {
91 1
        $fieldName = strtolower($fieldName);
92 1
        $preference = strtolower($preference);
93
94 1
        if (empty(self::$dictionaryFieldURIs)) {
95
            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...
96
        }
97
98 1
        if (!isset(self::$dictionaryFieldURIs[$fieldName])) {
99
            return false;
100
        }
101
102 1
        if (!isset(self::$dictionaryFieldURIs[$fieldName][$preference])) {
103
            $preference = 'item';
104
        }
105
106 1
        if (!isset(self::$dictionaryFieldURIs[$fieldName][$preference])) {
107
            throw new ExchangeException("Could not find uri $preference:$fieldName");
108
        }
109
110 1
        $fieldUri = self::$dictionaryFieldURIs[$fieldName][$preference];
111 1
        if (is_array($fieldUri)) {
112 1
            if (!$entryKey) {
113
                throw new ExchangeException("Please enter a specific entry key for this fieldURI");
114
            }
115
116 1
            $entryKey = strtolower($entryKey);
117 1
            if (!isset($fieldUri[$entryKey])) {
118
                throw new ExchangeException("Could not find uri $preference:$fieldName:$entryKey");
119
            }
120
121 1
            $fieldUri = $fieldUri[$entryKey];
122 1
        }
123
124 1
        return $fieldUri;
125
    }
126
}
127