Completed
Push — master ( 7c707d...617cd0 )
by Gareth
03:30
created

FieldURIManager::getIndexedFieldUriByName()   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 25
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 6.3541

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 25
ccs 11
cts 14
cp 0.7856
rs 8.439
cc 6
eloc 14
nc 5
nop 3
crap 6.3541
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
    /**
28
     * @deprecated This will be made protected in 0.9
29
     *
30
     * @param $className
31
     * @return array
32
     */
33 1
    public static function getFieldUrisFromClass($className)
34
    {
35
        //So, since we have to pass in URI's of everything we update, we need to fetch them
36 1
        $reflection = new \ReflectionClass($className);
37 1
        $constants = $reflection->getConstants();
38 1
        $constantsFound = array();
39
40
        //Loop through all URI's to list them in an array
41 1
        foreach ($constants as $constant) {
42 1
            $exploded = explode(":", strtolower($constant));
43 1
            if (count($exploded) == 1) {
44 1
                $exploded = ['item', $exploded[0]];
45
            }
46
47
            //It starts in order ['contacts', 'physicaladdress', 'city], we want it in
48
            //['physicaladdress', 'contacts', 'city]
49 1
            $temp = $exploded[0];
50 1
            $exploded[0] = $exploded[1];
51 1
            $exploded[1] = $temp;
52
53 1
            $depth = count($exploded) - 1;
54 1
            $current = &$constantsFound;
55
56
            //Use the exploded array as a way to create a multidimensional array. For example,
57
            //['contacts', 'physicaladdress', 'city'] becomes
1 ignored issue
show
Unused Code Comprehensibility introduced by
64% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
58
            //['contacts' => ['physicaladdress' => ['city' => 'contacts:PhysicalAddress:City']]]
1 ignored issue
show
Unused Code Comprehensibility introduced by
69% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
59 1
            foreach ($exploded as $count => $key) {
60 1
                if ($count < $depth && !isset($current[$key])) {
61 1
                    $current[$key] = array();
62
                }
63
64 1
                if ($count < $depth) {
65 1
                    $current = &$current[$key];
66 1
                    continue;
67
                }
68
69 1
                $current[$key] = $constant;
70
            }
71
        }
72
73 1
        return $constantsFound;
74
    }
75
76 5
    public static function getFieldUriByName($fieldName, $preference = 'item')
77
    {
78 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...
79 5
        $fieldName = strtolower($fieldName);
80 5
        $preference = strtolower($preference);
81
82 5
        if (!isset(self::$unIndexedFieldURIs[$fieldName])) {
83 1
            return false;
84
        }
85
86 5
        if (!isset(self::$unIndexedFieldURIs[$fieldName][$preference])) {
87 1
            $preference = 'item';
88
        }
89
90 5
        if (!isset(self::$unIndexedFieldURIs[$fieldName][$preference])) {
91
            throw new ExchangeException("Could not find uri $preference:$fieldName");
92
        }
93
94 5
        return self::$unIndexedFieldURIs[$fieldName][$preference];
95
    }
96
97 1
    public static function getIndexedFieldUriByName($fieldName, $preference = 'item', $entryKey = false)
98
    {
99 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...
100 1
        $fieldName = strtolower($fieldName);
101 1
        $preference = strtolower($preference);
102
103 1
        if (!isset(self::$dictionaryFieldURIs[$fieldName])) {
104
            return false;
105
        }
106
107 1
        if (!isset(self::$dictionaryFieldURIs[$fieldName][$preference])) {
108
            throw new ExchangeException("Could not find uri $preference:$fieldName");
109
        }
110
111 1
        $fieldUri = self::$dictionaryFieldURIs[$fieldName][$preference];
112 1
        if (!is_array($fieldUri)) {
113 1
            return $fieldUri;
114
        }
115
116 1
        if (!$entryKey || !isset($fieldUri[$entryKey])) {
117
            throw new ExchangeException("Could not find FieldURI");
118
        }
119
120 1
        return $fieldUri[$entryKey];
121
    }
122
}
123