CacheItemAwareTrait::setItemFactory()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
/**
3
 * Phossa Project
4
 *
5
 * PHP version 5.4
6
 *
7
 * @category  Library
8
 * @package   Phossa2\Cache
9
 * @copyright Copyright (c) 2016 phossa.com
10
 * @license   http://mit-license.org/ MIT License
11
 * @link      http://www.phossa.com/
12
 */
13
/*# declare(strict_types=1); */
14
15
namespace Phossa2\Cache\Traits;
16
17
use Phossa2\Cache\CacheItem;
18
use Psr\Cache\CacheItemInterface;
19
use Phossa2\Cache\Message\Message;
20
use Phossa2\Cache\Exception\InvalidArgumentException;
21
use Phossa2\Cache\Interfaces\CacheItemExtendedInterface;
22
23
/**
24
 * CacheItemAwareTrait
25
 *
26
 * @package Phossa2\Cache
27
 * @author  Hong Zhang <[email protected]>
28
 * @version 2.0.0
29
 * @since   2.0.0 added
30
 */
31
trait CacheItemAwareTrait
32
{
33
    /**
34
     * item factory method, signatures as follows
35
     *
36
     *     function(
37
     *          string $key,
38
     *          CachePool $driver,
39
     *          array $properties = []
40
     *     ): CacheItemInterface
41
     *
42
     * @var    callable
43
     * @access protected
44
     */
45
    protected $item_factory;
46
47
    /**
48
     * Local cache of generated CacheItemInterface
49
     *
50
     * @var    CacheItemExtendedInterface[]
51
     * @access protected
52
     */
53
    protected $item_cache;
54
55
    /**
56
     * Use local cache for generated item
57
     *
58
     * @var    bool
59
     * @access protected
60
     */
61
    protected $use_item_cache = false;
62
63
    /**
64
     * Set cache item factory callable
65
     *
66
     * @param  callable $itemFactory
67
     * @return $this
68
     * @access public
69
     */
70
    public function setItemFactory(callable $itemFactory = null)
71
    {
72
        $this->item_factory = $itemFactory;
73
        return $this;
74
    }
75
76
    /**
77
     * Get a cache item
78
     *
79
     * @param  string $key
80
     * @return CacheItemExtendedInterface
81
     * @throws InvalidArgumentException if $key is invalid
82
     * @access protected
83
     */
84
    protected function getCacheItem(/*# string */ $key)/*# : CacheItemExtendedInterface */
85
    {
86
        // validate key first
87
        $this->validateKey($key);
88
89
        // try local cache
90
        if ($this->use_item_cache) {
91
            if (!isset($this->item_cache[$key[0]][$key])) {
92
                $this->item_cache[$key[0]][$key] = $this->createCacheItem($key);
93
            }
94
            return $this->item_cache[$key[0]][$key];
95
        } else {
96
            return $this->createCacheItem($key);
97
        }
98
    }
99
100
    /**
101
     * Create a cache item on the fly
102
     *
103
     * @param  string $key
104
     * @return CacheItemExtendedInterface
105
     * @access protected
106
     */
107
    protected function createCacheItem(/*# string */ $key)/*# : CacheItemExtendedInterface */
108
    {
109
        if (is_callable($this->item_factory)) {
110
            $func = $this->item_factory;
111
            $item = $func($key, $this);
112
        } else {
113
            $item = new CacheItem($key, $this);
114
        }
115
116
        return $item;
117
    }
118
119
    /**
120
     * Validate key string
121
     *
122
     * @param  string &$key key to check
123
     * @return void
124
     * @throws InvalidArgumentException
125
     * @access protected
126
     */
127
    protected function validateKey(/*# string */ &$key)
128
    {
129
        // validate key
130
        if (is_string($key)) {
131
            $key = trim($key);
132
            return;
133
        }
134
135
        // throw exception
136
        throw new InvalidArgumentException(
137
            Message::get(Message::CACHE_INVALID_KEY, $key),
138
            Message::CACHE_INVALID_KEY
139
        );
140
    }
141
}
142