Completed
Branch Gutenberg/master (b3a823)
by
unknown
73:52 queued 60:28
created

CachingLoader::setIdentifier()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace EventEspresso\core\services\loaders;
4
5
use EventEspresso\core\domain\values\FullyQualifiedName;
6
use EventEspresso\core\exceptions\InvalidDataTypeException;
7
use EventEspresso\core\services\collections\CollectionInterface;
8
use InvalidArgumentException;
9
10
/**
11
 * Class CachingLoader
12
 * caches objects returned by the decorated loader
13
 *
14
 * @package       Event Espresso
15
 * @author        Brent Christensen
16
 */
17
class CachingLoader extends CachingLoaderDecorator
18
{
19
20
    /**
21
     * @var string $identifier
22
     */
23
    protected $identifier;
24
25
    /**
26
     * @var CollectionInterface $cache
27
     */
28
    protected $cache;
29
30
    /**
31
     * @var ObjectIdentifier
32
     */
33
    private $object_identifier;
34
35
36
    /**
37
     * CachingLoader constructor.
38
     *
39
     * @param LoaderDecoratorInterface $loader
40
     * @param CollectionInterface      $cache
41
     * @param ObjectIdentifier         $object_identifier
42
     * @param string                   $identifier
43
     * @throws InvalidDataTypeException
44
     */
45
    public function __construct(
46
        LoaderDecoratorInterface $loader,
47
        CollectionInterface $cache,
48
        ObjectIdentifier $object_identifier,
49
        $identifier = ''
50
    ) {
51
        parent::__construct($loader);
52
        $this->cache       = $cache;
53
        $this->object_identifier = $object_identifier;
54
        $this->setIdentifier($identifier);
55
        if ($this->identifier !== '') {
56
            // to only clear this cache, and assuming an identifier has been set, simply do the following:
57
            // do_action('AHEE__EventEspresso_core_services_loaders_CachingLoader__resetCache__IDENTIFIER');
58
            // where "IDENTIFIER" = the string that was set during construction
59
            add_action(
60
                "AHEE__EventEspresso_core_services_loaders_CachingLoader__resetCache__{$identifier}",
61
                array($this, 'reset')
62
            );
63
        }
64
        // to clear ALL caches, simply do the following:
65
        // do_action('AHEE__EventEspresso_core_services_loaders_CachingLoader__resetCache');
66
        add_action(
67
            'AHEE__EventEspresso_core_services_loaders_CachingLoader__resetCache',
68
            array($this, 'reset')
69
        );
70
    }
71
72
73
    /**
74
     * @return string
75
     */
76
    public function identifier()
77
    {
78
        return $this->identifier;
79
    }
80
81
82
    /**
83
     * @param string $identifier
84
     * @throws InvalidDataTypeException
85
     */
86
    private function setIdentifier($identifier)
87
    {
88
        if (! is_string($identifier)) {
89
            throw new InvalidDataTypeException('$identifier', $identifier, 'string');
90
        }
91
        $this->identifier = $identifier;
92
    }
93
94
95
    /**
96
     * @param FullyQualifiedName|string $fqcn
97
     * @param mixed                     $object
98
     * @return bool
99
     * @throws InvalidArgumentException
100
     */
101
    public function share($fqcn, $object)
102
    {
103
        if ($object instanceof $fqcn) {
104
            return $this->cache->add($object, md5($fqcn));
105
        }
106
        throw new InvalidArgumentException(
107
            sprintf(
108
                esc_html__(
109
                    'The supplied class name "%1$s" must match the class of the supplied object.',
110
                    'event_espresso'
111
                ),
112
                $fqcn
113
            )
114
        );
115
    }
116
117
118
    /**
119
     * @param FullyQualifiedName|string $fqcn
120
     * @param array                     $arguments
121
     * @param bool                      $shared
122
     * @param array                     $interfaces
123
     * @return mixed
124
     */
125
    public function load($fqcn, $arguments = array(), $shared = true, array $interfaces = array())
126
    {
127
        $fqcn = ltrim($fqcn, '\\');
128
        // caching can be turned off via the following code:
129
        // add_filter('FHEE__EventEspresso_core_services_loaders_CachingLoader__load__bypass_cache', '__return_true');
130
        if (apply_filters(
131
            'FHEE__EventEspresso_core_services_loaders_CachingLoader__load__bypass_cache',
132
            false,
133
            $this
134
        )) {
135
            // even though $shared might be true, caching could be bypassed for whatever reason,
136
            // so we don't want the core loader to cache anything, therefore caching is turned off
137
            return $this->loader->load($fqcn, $arguments, false);
138
        }
139
        $object_identifier = $this->object_identifier->getIdentifier($fqcn, $arguments);
140
        if ($this->cache->has($object_identifier)) {
141
            return $this->cache->get($object_identifier);
142
        }
143
        $object = $this->loader->load($fqcn, $arguments, $shared);
144
        if ($object instanceof $fqcn) {
145
            $this->cache->add($object, $object_identifier);
146
        }
147
        return $object;
148
    }
149
150
151
    /**
152
     * empties cache and calls reset() on loader if method exists
153
     */
154
    public function reset()
155
    {
156
        $this->clearCache();
157
        $this->loader->reset();
158
    }
159
160
161
    /**
162
     * unsets and detaches ALL objects from the cache
163
     *
164
     * @since $VID:$
165
     */
166
    public function clearCache()
167
    {
168
        $this->cache->trashAndDetachAll();
169
    }
170
}
171