Completed
Push — master ( 3a0f53...56ae52 )
by Tobias
20:55
created

__TPL_CLASS__::save()   A

Complexity

Conditions 1
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 9
Ratio 100 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 9
loc 9
rs 9.6666
cc 1
eloc 6
nc 2
nop 1
1
<?php
2
3
/*
4
 * This file is part of php-cache\cache-bundle package.
5
 *
6
 * (c) 2015-2015 Aaron Scherer <[email protected]>, Tobias Nyholm <[email protected]>
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
use Cache\CacheBundle\DataCollector\CacheProxy;
13
use Cache\CacheBundle\DataCollector\TraceableAdapterEvent;
14
use Psr\Cache\CacheItemInterface;
15
16
class __TPL_CLASS__ extends __TPL_EXTENDS__ implements CacheProxy
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
17
{
18
    private $__name;
19
    private $__calls = [];
20
21
    public function getItem($key)
22
    {
23
        $event = $this->start(__FUNCTION__, $key);
24
        try {
25
            $item = parent::getItem($key);
26
        } finally {
27
            $event->end = microtime(true);
28
        }
29
        if ($item->isHit()) {
30
            ++$event->hits;
31
        } else {
32
            ++$event->misses;
33
        }
34
        $event->result = $item->get();
35
36
        return $item;
37
    }
38
39
    public function hasItem($key)
40
    {
41
        $event = $this->start(__FUNCTION__, $key);
42
        try {
43
            $event->result = parent::hasItem($key);
44
        } finally {
45
            $event->end = microtime(true);
46
        }
47
48
        if (!$event->result) {
49
            ++$event->misses;
50
        }
51
52
        return $event->result;
53
    }
54
55 View Code Duplication
    public function deleteItem($key)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
56
    {
57
        $event = $this->start(__FUNCTION__, $key);
58
        try {
59
            return $event->result = parent::deleteItem($key);
60
        } finally {
61
            $event->end = microtime(true);
62
        }
63
    }
64
65 View Code Duplication
    public function save(CacheItemInterface $item)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
66
    {
67
        $event = $this->start(__FUNCTION__, $item);
68
        try {
69
            return $event->result = parent::save($item);
70
        } finally {
71
            $event->end = microtime(true);
72
        }
73
    }
74
75 View Code Duplication
    public function saveDeferred(CacheItemInterface $item)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
76
    {
77
        $event = $this->start(__FUNCTION__, $item);
78
        try {
79
            return $event->result = parent::saveDeferred($item);
80
        } finally {
81
            $event->end = microtime(true);
82
        }
83
    }
84
85
    public function getItems(array $keys = [])
86
    {
87
        $event = $this->start(__FUNCTION__, $keys);
88
        try {
89
            $result = parent::getItems($keys);
90
        } finally {
91
            $event->end = microtime(true);
92
        }
93
        $f = function () use ($result, $event) {
94
            $event->result = [];
95
            foreach ($result as $key => $item) {
96
                if ($item->isHit()) {
97
                    ++$event->hits;
98
                } else {
99
                    ++$event->misses;
100
                }
101
                $event->result[$key] = $item->get();
102
                yield $key => $item;
103
            }
104
        };
105
106
        return $f();
107
    }
108
109 View Code Duplication
    public function clear()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
110
    {
111
        $event = $this->start(__FUNCTION__);
112
        try {
113
            return $event->result = parent::clear();
114
        } finally {
115
            $event->end = microtime(true);
116
        }
117
    }
118
119 View Code Duplication
    public function deleteItems(array $keys)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
120
    {
121
        $event = $this->start(__FUNCTION__, $keys);
122
        try {
123
            return $event->result = parent::deleteItems($keys);
124
        } finally {
125
            $event->end = microtime(true);
126
        }
127
    }
128
129 View Code Duplication
    public function commit()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
130
    {
131
        $event = $this->start(__FUNCTION__);
132
        try {
133
            return $event->result = parent::commit();
134
        } finally {
135
            $event->end = microtime(true);
136
        }
137
    }
138
139 View Code Duplication
    public function invalidateTag($tag)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
140
    {
141
        $event = $this->start(__FUNCTION__, $tag);
142
        try {
143
            return $event->result = parent::invalidateTag($tag);
144
        } finally {
145
            $event->end = microtime(true);
146
        }
147
    }
148
149 View Code Duplication
    public function invalidateTags(array $tags)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
150
    {
151
        $event = $this->start(__FUNCTION__, $tags);
152
        try {
153
            return $event->result = parent::invalidateTags($tags);
154
        } finally {
155
            $event->end = microtime(true);
156
        }
157
    }
158
159
    public function __getCalls()
160
    {
161
        return $this->__calls;
162
    }
163
164
    public function __setName($name)
165
    {
166
        $this->__name = $name;
167
    }
168
169
    private function start($name, $argument = null)
170
    {
171
        $this->__calls[]   = $event   = new TraceableAdapterEvent();
172
        $event->name       = $name;
173
        $event->argument   = $argument;
174
        $event->start      = microtime(true);
175
176
        return $event;
177
    }
178
}
179