Completed
Branch FET/asset-manager (018c4e)
by
unknown
87:46 queued 74:14
created

ObjectIdentifier   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 113
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 113
rs 10
c 0
b 0
f 0
wmc 13
lcom 1
cbo 1

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A hasArguments() 0 7 1
A fqcnMatchesObjectIdentifier() 0 5 2
A getIdentifier() 0 14 3
B getIdentifierForArguments() 0 22 6
1
<?php
2
3
namespace EventEspresso\core\services\loaders;
4
5
use Closure;
6
7
defined('EVENT_ESPRESSO_VERSION') || exit;
8
9
10
11
/**
12
 * Class ObjectIdentifier
13
 * Builds a string representation of an object's FQCN and arguments
14
 * used for identifying whether two objects are functionally equivalent
15
 *
16
 * @package EventEspresso\core\services\loaders
17
 * @author  Brent Christensen
18
 * @since   $VID:$
19
 */
20
class ObjectIdentifier
21
{
22
23
    /**
24
     * used to separate the FQCN from the class's arguments identifier
25
     */
26
    const DELIMITER = '____';
27
28
    /**
29
     * @var ClassInterfaceCache $class_cache
30
     */
31
    private $class_cache;
32
33
34
    /**
35
     * ObjectIdentifier constructor.
36
     *
37
     * @param ClassInterfaceCache $class_cache
38
     */
39
    public function __construct(ClassInterfaceCache $class_cache)
40
    {
41
        $this->class_cache = $class_cache;
42
    }
43
44
45
    /**
46
     * Returns true if the supplied $object_identifier contains
47
     * the delimiter used to separate an fqcn from the arguments hash
48
     *
49
     * @param string $object_identifier
50
     * @return bool
51
     */
52
    public function hasArguments($object_identifier)
53
    {
54
        // type casting to bool instead of using strpos() !== false
55
        // because an object identifier should never begin with the delimiter
56
        // therefore the delimiter should NOT be found at position 0
57
        return (bool) strpos($object_identifier, ObjectIdentifier::DELIMITER);
58
    }
59
60
61
    /**
62
     * Returns true if the supplied FQCN equals the supplied $object_identifier
63
     * OR the supplied FQCN matches the FQCN portion of the supplied $object_identifier
64
     * AND that $object_identifier is for an object with arguments.
65
     * This allows a request for an object using a FQCN to match
66
     * a previously instantiated object with arguments
67
     * without having to know those arguments.
68
     *
69
     * @param string $fqcn
70
     * @param string $object_identifier
71
     * @return bool
72
     */
73
    public function fqcnMatchesObjectIdentifier($fqcn, $object_identifier)
74
    {
75
        return $fqcn === $object_identifier
76
               || strpos($object_identifier, $fqcn . ObjectIdentifier::DELIMITER) === 0;
77
    }
78
79
80
    /**
81
     * build a string representation of an object's FQCN and arguments
82
     *
83
     * @param string $fqcn
84
     * @param array  $arguments
85
     * @return string
86
     */
87
    public function getIdentifier($fqcn, array $arguments = array())
88
    {
89
        // only build identifier from arguments if class is not ReservedInstanceInterface
90
        $identifier = ! $this->class_cache->hasInterface(
91
            $fqcn,
92
            'EventEspresso\core\interfaces\ReservedInstanceInterface'
93
        )
94
            ? $this->getIdentifierForArguments($arguments)
95
            : '';
96
        if (! empty($identifier)) {
97
            $fqcn .= ObjectIdentifier::DELIMITER . md5($identifier);
98
        }
99
        return $fqcn;
100
    }
101
102
103
    /**
104
     * build a string representation of a object's arguments
105
     * (mostly because Closures can't be serialized)
106
     *
107
     * @param array $arguments
108
     * @return string
109
     */
110
    protected function getIdentifierForArguments(array $arguments)
111
    {
112
        if (empty($arguments)) {
113
            return '';
114
        }
115
        $identifier = '';
116
        foreach ($arguments as $argument) {
117
            switch (true) {
118
                case is_object($argument) :
119
                case $argument instanceof Closure :
0 ignored issues
show
Bug introduced by
The class Closure does not exist. Is this class maybe located in a folder that is not analyzed, or in a newer version of your dependencies than listed in your composer.lock/composer.json?
Loading history...
120
                    $identifier .= spl_object_hash($argument);
121
                    break;
122
                case is_array($argument) :
123
                    $identifier .= $this->getIdentifierForArguments($argument);
124
                    break;
125
                default :
126
                    $identifier .= $argument;
127
                    break;
128
            }
129
        }
130
        return $identifier;
131
    }
132
}