Completed
Push — master ( 045dfa...29039c )
by Gareth
04:14
created

FieldURIManager   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 120
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 87.04%

Importance

Changes 3
Bugs 0 Features 1
Metric Value
wmc 21
c 3
b 0
f 1
lcom 1
cbo 1
dl 0
loc 120
ccs 47
cts 54
cp 0.8704
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A setupFieldUris() 0 9 3
C getFieldUrisFromClass() 0 42 7
A getFieldUriByName() 0 20 4
A getDictionaryURIFields() 0 5 1
B getIndexedFieldUriByName() 0 25 6
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
    /**
34
     * @deprecated This will be made protected in 0.9
35
     *
36
     * @param $className
37
     * @return array
38
     */
39 1
    public static function getFieldUrisFromClass($className)
40
    {
41
        //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 1
        foreach ($constants as $constant) {
48 1
            $exploded = explode(":", strtolower($constant));
49 1
            if (count($exploded) == 1) {
50 1
                $exploded = ['item', $exploded[0]];
51
            }
52
53
            //It starts in order ['contacts', 'physicaladdress', 'city], we want it in
54
            //['physicaladdress', 'contacts', 'city]
55 1
            $temp = $exploded[0];
56 1
            $exploded[0] = $exploded[1];
57 1
            $exploded[1] = $temp;
58
59 1
            $depth = count($exploded) - 1;
60 1
            $current = &$constantsFound;
61
62
            //Use the exploded array as a way to create a multidimensional array. For example,
63
            //['contacts', 'physicaladdress', 'city'] becomes
64
            //['contacts' => ['physicaladdress' => ['city' => 'contacts:PhysicalAddress:City']]]
65 1
            foreach ($exploded as $count => $key) {
66 1
                if ($count < $depth && !isset($current[$key])) {
67 1
                    $current[$key] = array();
68
                }
69
70 1
                if ($count < $depth) {
71 1
                    $current = &$current[$key];
72 1
                    continue;
73
                }
74
75 1
                $current[$key] = $constant;
76
            }
77
        }
78
79 1
        return $constantsFound;
80
    }
81
82 5
    public static function getFieldUriByName($fieldName, $preference = 'item')
83
    {
84 5
        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 5
        $fieldName = strtolower($fieldName);
86 5
        $preference = strtolower($preference);
87
88 5
        if (!isset(self::$unIndexedFieldURIs[$fieldName])) {
89 1
            return false;
90
        }
91
92 5
        if (!isset(self::$unIndexedFieldURIs[$fieldName][$preference])) {
93 1
            $preference = 'item';
94
        }
95
96 5
        if (!isset(self::$unIndexedFieldURIs[$fieldName][$preference])) {
97
            throw new ExchangeException("Could not find uri $preference:$fieldName");
98
        }
99
100 5
        return self::$unIndexedFieldURIs[$fieldName][$preference];
101
    }
102
103 1
    public static function getIndexedFieldUriByName($fieldName, $preference = 'item', $entryKey = false)
104
    {
105 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...
106 1
        $fieldName = strtolower($fieldName);
107 1
        $preference = strtolower($preference);
108
109 1
        if (!isset(self::$dictionaryFieldURIs[$fieldName])) {
110
            return false;
111
        }
112
113 1
        if (!isset(self::$dictionaryFieldURIs[$fieldName][$preference])) {
114
            throw new ExchangeException("Could not find uri $preference:$fieldName");
115
        }
116
117 1
        $fieldUri = self::$dictionaryFieldURIs[$fieldName][$preference];
118 1
        if (!is_array($fieldUri)) {
119 1
            return $fieldUri;
120
        }
121
122 1
        if (!$entryKey || !isset($fieldUri[$entryKey])) {
123
            throw new ExchangeException("Could not find FieldURI");
124
        }
125
126 1
        return $fieldUri[$entryKey];
127
    }
128
}
129