Completed
Push — master ( 901d9a...69b75e )
by Jacob
9s
created

CacheWarmer::clear()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 7
rs 9.4285
cc 2
eloc 4
nc 2
nop 1
1
<?php
2
3
namespace As3\Modlr\Metadata\Cache;
4
5
use As3\Modlr\Metadata\MetadataFactory;
6
7
/**
8
 * Warms up the metadata cache objects by placing all known entities into the cache source.
9
 * Only functions if the MetadataFactory has cache implemented.
10
 *
11
 * @author Jacob Bare <[email protected]>
12
 */
13
class CacheWarmer
14
{
15
    /**
16
     * The metadata factory for loading and caching metadata objects.
17
     *
18
     * @var MetadataFactory
19
     */
20
    private $mf;
21
22
    /**
23
     * Constructor.
24
     *
25
     * @param   MetadataFactory     $mf
26
     */
27
    public function __construct(MetadataFactory $mf)
28
    {
29
        $this->mf = $mf;
30
    }
31
32
    /**
33
     * Clears metadata objects from the cache.
34
     *
35
     * @param   string|array|null   $type
36
     * @return  array
37
     */
38
    public function clear($type = null)
39
    {
40
        if (false === $this->mf->hasCache()) {
41
            return [];
42
        }
43
        return $this->doClear($this->getTypes($type));
44
    }
45
46
    /**
47
     * Warms up metadata objects into the cache.
48
     *
49
     * @param   string|array|null   $type
50
     * @return  array
51
     */
52
    public function warm($type = null)
53
    {
54
        if (false === $this->mf->hasCache()) {
55
            return [];
56
        }
57
        return $this->doWarm($this->getTypes($type));
58
    }
59
60
    /**
61
     * Clears metadata objects for the provided model types.
62
     *
63
     * @param   array   $types
64
     * @return  array
65
     */
66
    private function doClear(array $types)
67
    {
68
        $cleared = [];
69
        $this->mf->enableCache(false);
70
71
        foreach ($types as $type) {
72
            $metadata = $this->mf->getMetadataForType($type);
73
            $this->mf->getCache()->evictMetadataFromCache($metadata);
74
            $cleared[] = $type;
75
        }
76
77
        $this->mf->enableCache(true);
78
        $this->mf->clearMemory();
79
        return $cleared;
80
    }
81
82
    /**
83
     * Warms up the metadata objects for the provided model types.
84
     *
85
     * @param   array   $types
86
     * @return  array
87
     */
88
    private function doWarm(array $types)
89
    {
90
        $warmed = [];
91
        $this->doClear($types);
92
        foreach ($types as $type) {
93
            $this->mf->getMetadataForType($type);
94
            $warmed[] = $type;
95
        }
96
        return $warmed;
97
    }
98
99
    /**
100
     * Gets the model types based on an array, string, or null type value.
101
     *
102
     * @param   string|array|null
103
     * @return  array
104
     */
105
    private function getTypes($type = null)
106
    {
107
        if (null === $type) {
108
            return $this->mf->getAllTypeNames();
109
        }
110
        return (array) $type;
111
    }
112
}
113