Completed
Push — master ( 31d69b...c656e0 )
by Marcus
09:48
created

Cache   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 90
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 1
dl 0
loc 90
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getType() 0 4 1
A isEnabled() 0 4 3
A getTime() 0 4 1
A setConfiguration() 0 4 1
A getConfiguration() 0 4 1
1
<?php
2
3
/**
4
 * TechDivision\Import\Configuration\Jms\Configuration\Cache
5
 *
6
 * NOTICE OF LICENSE
7
 *
8
 * This source file is subject to the Open Software License (OSL 3.0)
9
 * that is available through the world-wide-web at this URL:
10
 * http://opensource.org/licenses/osl-3.0.php
11
 *
12
 * PHP version 5
13
 *
14
 * @author    Tim Wagner <[email protected]>
15
 * @copyright 2019 TechDivision GmbH <[email protected]>
16
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
17
 * @link      https://github.com/techdivision/import-configuration-jms
18
 * @link      http://www.techdivision.com
19
 */
20
21
namespace TechDivision\Import\Configuration\Jms\Configuration;
22
23
use JMS\Serializer\Annotation\Type;
24
use JMS\Serializer\Annotation\SerializedName;
25
use TechDivision\Import\Utils\CacheTypes;
26
use TechDivision\Import\Configuration\ConfigurationInterface;
27
use TechDivision\Import\Configuration\CacheConfigurationInterface;
28
29
/**
30
 * The cache configuration implementation.
31
 *
32
 * @author    Tim Wagner <[email protected]>
33
 * @copyright 2019 TechDivision GmbH <[email protected]>
34
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
35
 * @link      https://github.com/techdivision/import-configuration-jms
36
 * @link      http://www.techdivision.com
37
 */
38
class Cache implements CacheConfigurationInterface
39
{
40
41
    /**
42
     * The cache type.
43
     *
44
     * @var string
45
     * @Type("string")
46
     * @SerializedName("type")
47
     */
48
    protected $type;
49
50
    /**
51
     * The cache's TTL (null === cache item NEVER times out).
52
     *
53
     * @var integer
54
     * @Type("integer")
55
     */
56
    protected $time = null;
57
58
    /**
59
     * The event name the listener has to be registered.
60
     *
61
     * @var string
62
     * @Type("boolean")
63
     */
64
    protected $enabled = true;
65
66
    /**
67
     * A reference to the parent configuration instance.
68
     *
69
     * @var \TechDivision\Import\Configuration\ConfigurationInterface
70
     */
71
    protected $configuration;
72
73
    /**
74
     * Return's the cache type
75
     *
76
     * @return string The cache type
77
     */
78
    public function getType()
79
    {
80
        return $this->type;
81
    }
82
83
    /**
84
     * Return's the flag whether the cache is enabled or not.
85
     *
86
     * The cache type cache.static is ALWAYS enabled, which is necessary e. g.
87
     * for the registration processor that contains the global data.
88
     *
89
     * @return boolean TRUE if the cache is enabled, else FALSE
90
     */
91
    public function isEnabled()
92
    {
93
        return (($this->enabled && $this->configuration->isCacheEnabled()) || $this->type === CacheTypes::TYPE_STATIC);
94
    }
95
96
    /**
97
     * Return's the cache TTL in seconds.
98
     *
99
     * @return integer The TTL
100
     */
101
    public function getTime()
102
    {
103
        return $this->time;
104
    }
105
106
    /**
107
     * Set's the reference to the configuration instance.
108
     *
109
     * @param \TechDivision\Import\Configuration\ConfigurationInterface $configuration The configuration instance
110
     *
111
     * @return void
112
     */
113
    public function setConfiguration(ConfigurationInterface $configuration)
114
    {
115
        $this->configuration = $configuration;
116
    }
117
118
    /**
119
     * Return's the reference to the configuration instance.
120
     *
121
     * @return \TechDivision\Import\Configuration\ConfigurationInterface The configuration instance
122
     */
123
    public function getConfiguration()
124
    {
125
        return $this->configuration;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->configuration; (TechDivision\Import\Conf...\ConfigurationInterface) is incompatible with the return type declared by the interface TechDivision\Import\Conf...rface::getConfiguration of type TechDivision\Import\ConfigurationInterface.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
126
    }
127
}
128