Completed
Push — master ( 185e5e...dc5aa9 )
by Alexander
03:43
created

Manager   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 43
rs 10
c 0
b 0
f 0
wmc 9
lcom 1
cbo 1

4 Methods

Rating   Name   Duplication   Size   Complexity  
A load() 0 15 4
A save() 0 7 2
A delete() 0 7 2
A key() 0 4 1
1
<?php
2
/**
3
 * @author Oleksandr Torosh <[email protected]>
4
 */
5
6
namespace Application\Cache;
7
8
use Phalcon\Mvc\User\Component;
9
10
class Manager extends Component
11
{
12
    /**
13
     * Get cache data. If data not exists, generate data and save to cache
14
     */
15
    public function load($key, $closure, $lifetime = 60)
16
    {
17
        if (is_array($key)) {
18
            $key = $this->key($key);
19
        }
20
        if ($lifetime == 0) {
21
            return $closure();
22
        }
23
        $data = $this->cache->get($key, $lifetime);
0 ignored issues
show
Documentation introduced by
The property cache does not exist on object<Application\Cache\Manager>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
24
        if (!$data) {
25
            $data = $closure();
26
            $this->cache->save($key, $data, $lifetime);
0 ignored issues
show
Documentation introduced by
The property cache does not exist on object<Application\Cache\Manager>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
27
        }
28
        return $data;
29
    }
30
31
    public function save($key, $data, $lifetime)
32
    {
33
        if (is_array($key)) {
34
            $key = $this->key($key);
35
        }
36
        $this->cache->save($key, $data, $lifetime);
0 ignored issues
show
Documentation introduced by
The property cache does not exist on object<Application\Cache\Manager>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
37
    }
38
39
    public function delete($key)
40
    {
41
        if (is_array($key)) {
42
            $key = $this->key($key);
43
        }
44
        $this->cache->delete($key);
0 ignored issues
show
Documentation introduced by
The property cache does not exist on object<Application\Cache\Manager>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
45
    }
46
47
    public function key(array $params = [])
48
    {
49
        return md5(serialize($params));
50
    }
51
52
}