Completed
Push — master ( 9b1cea...63073a )
by yuuki
11s
created

AbstractCache::detectCacheKeys()   B

Complexity

Conditions 6
Paths 8

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 19
rs 8.8571
c 0
b 0
f 0
cc 6
eloc 10
nc 8
nop 3
1
<?php
2
3
/**
4
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
5
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
6
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
7
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
8
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
9
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
10
 * THE SOFTWARE.
11
 *
12
 * This software consists of voluntary contributions made by many individuals
13
 * and is licensed under the MIT license.
14
 *
15
 * Copyright (c) 2015-2016 Yuuki Takezawa
16
 *
17
 */
18
namespace Ytake\LaravelAspect\Interceptor;
19
20
use Ray\Aop\MethodInvocation;
21
use Ray\Aop\MethodInterceptor;
22
use Illuminate\Contracts\Cache\Factory;
23
use Ytake\LaravelAspect\Annotation\AnnotationReaderTrait;
24
25
/**
26
 * Class AbstractCache
27
 */
28
abstract class AbstractCache implements MethodInterceptor
29
{
30
    use AnnotationReaderTrait;
31
32
    /** @var string */
33
    protected $join = ":";
34
35
    /** @var Factory */
36
    protected static $factory;
37
38
    /**
39
     * @param                  $name
40
     * @param MethodInvocation $invocation
41
     *
42
     * @return array
43
     */
44
    protected function generateCacheName($name, MethodInvocation $invocation)
45
    {
46
        if (is_array($name)) {
47
            throw new \InvalidArgumentException('Invalid argument');
48
        }
49
        if (is_null($name)) {
50
            $name = $invocation->getMethod()->name;
51
        }
52
53
        return [$name];
54
    }
55
56
    /**
57
     * @param MethodInvocation $invocation
58
     * @param                  $annotation
59
     * @param                  $keys
60
     *
61
     * @return array
62
     */
63
    protected function detectCacheKeys(MethodInvocation $invocation, $annotation, $keys)
64
    {
65
        $arguments = $invocation->getArguments();
66
        foreach ($invocation->getMethod()->getParameters() as $parameter) {
67
            // exclude object
68
            if (in_array('#' . $parameter->name, $annotation->key)) {
69
                if (isset($arguments[$parameter->getPosition()])) {
70
                    if (!is_object($arguments[$parameter->getPosition()])) {
71
                        $keys[] = $arguments[$parameter->getPosition()];
72
                    }
73
                }
74
                if (!isset($arguments[$parameter->getPosition()])) {
75
                    $keys[] = $parameter->getDefaultValue();
76
                }
77
            }
78
        }
79
80
        return $keys;
81
    }
82
83
    /**
84
     * @param $annotation
85
     *
86
     * @return \Illuminate\Contracts\Cache\Repository
87
     */
88
    protected function detectCacheRepository($annotation)
89
    {
90
        /** @var \Illuminate\Contracts\Cache\Repository $cache */
91
        $cache = self::$factory->store($annotation->driver);
92
        if (count($annotation->tags)) {
93
            $cache = $cache->tags($annotation->tags);
94
95
            return $cache;
96
        }
97
98
        return $cache;
99
    }
100
101
    /**
102
     * set cache instance
103
     *
104
     * @param Factory $factory
105
     */
106
    public function setCache(Factory $factory)
107
    {
108
        self::$factory = $factory;
109
    }
110
111
    /**
112
     * @param string $glue
113
     * @param array  $array
114
     * @return string
115
     */
116
    protected function recursiveImplode($glue, array $array)
117
    {
118
        $return = '';
119
        $index = 0;
120
        $count = count($array);
121
        foreach ($array as $row) {
122
            if (is_array($row)) {
123
                $return .= $this->recursiveImplode($glue, $row);
124
            } else {
125
                $return .= (is_object($row)) ? get_class($row) : $row;
126
            }
127
            if ($index < $count - 1) {
128
                $return .= $glue;
129
            }
130
            ++$index;
131
        }
132
133
        return $return;
134
    }
135
}
136