Completed
Push — inmemory-pool-decoration ( 469074...282551 )
by André
25:04
created

ContentLanguageHandler   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 147
Duplicated Lines 43.54 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

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

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 12 1
A create() 7 7 1
A update() 0 13 1
A load() 12 12 1
A loadList() 12 12 1
A loadByLanguageCode() 12 12 1
A loadListByLanguageCodes() 12 12 1
A loadAll() 0 11 1
A delete() 9 10 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
/**
4
 * File containing the LanguageHandler class.
5
 *
6
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
7
 * @license For full copyright and license information view LICENSE file distributed with this source code.
8
 */
9
namespace eZ\Publish\Core\Persistence\Cache;
10
11
use eZ\Publish\SPI\Persistence\Content\Language\Handler as ContentLanguageHandlerInterface;
12
use eZ\Publish\SPI\Persistence\Content\Language;
13
use eZ\Publish\SPI\Persistence\Content\Language\CreateStruct;
14
15
/**
16
 * @see \eZ\Publish\SPI\Persistence\Content\Language\Handler
17
 */
18
class ContentLanguageHandler extends AbstractInMemoryHandler implements ContentLanguageHandlerInterface
19
{
20
    /** @var callable */
21
    private $getTags;
22
23
    /** @var callable */
24
    private $getKeys;
25
26
    /**
27
     * Set callback functions for use in cache retrieval.
28
     *
29
     * {@inheritdoc}
30
     */
31
    public function __construct(...$params)
32
    {
33
        parent::__construct(...$params);
0 ignored issues
show
Bug introduced by
The call to AbstractInMemoryHandler::__construct() misses some required arguments starting with $cache.
Loading history...
34
35
        $this->getTags = static function (Language $language) { return ['language-' . $language->id]; };
36
        $this->getKeys = static function (Language $language) {
37
            return [
38
                'ez-language-' . $language->id,
39
                'ez-language-code-' . $language->languageCode,
40
            ];
41
        };
42
    }
43
44
    /**
45
     * {@inheritdoc}
46
     */
47 View Code Duplication
    public function create(CreateStruct $struct)
48
    {
49
        $this->logger->logCall(__METHOD__, array('struct' => $struct));
50
        $this->cache->deleteItems(['ez-language-list']);
51
52
        return $this->persistenceHandler->contentLanguageHandler()->create($struct);
53
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58
    public function update(Language $struct)
59
    {
60
        $this->logger->logCall(__METHOD__, array('struct' => $struct));
61
        $return = $this->persistenceHandler->contentLanguageHandler()->update($struct);
62
63
        $this->cache->deleteItems([
64
            'ez-language-list',
65
            'ez-language-' . $struct->id,
66
            'ez-language-code-' . $struct->languageCode,
67
        ]);
68
69
        return $return;
70
    }
71
72
    /**
73
     * {@inheritdoc}
74
     */
75 View Code Duplication
    public function load($id)
76
    {
77
        return $this->getCacheValue(
78
            $id,
79
            'ez-language-',
80
            function ($id) {
81
                return $this->persistenceHandler->contentLanguageHandler()->load($id);
82
            },
83
            $this->getTags,
84
            $this->getKeys
85
        );
86
    }
87
88
    /**
89
     * {@inheritdoc}
90
     */
91 View Code Duplication
    public function loadList(array $ids): iterable
92
    {
93
        return $this->getMultipleCacheValues(
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->getMultipl...tTags, $this->getKeys); (array) is incompatible with the return type declared by the interface eZ\Publish\SPI\Persisten...guage\Handler::loadList of type eZ\Publish\SPI\Persisten...ntent\Language\iterable.

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...
94
            $ids,
95
            'ez-language-',
96
            function (array $ids) {
97
                return $this->persistenceHandler->contentLanguageHandler()->loadList($ids);
98
            },
99
            $this->getTags,
100
            $this->getKeys
101
        );
102
    }
103
104
    /**
105
     * {@inheritdoc}
106
     */
107 View Code Duplication
    public function loadByLanguageCode($languageCode)
108
    {
109
        return $this->getCacheValue(
110
            $languageCode,
111
            'ez-language-code-',
112
            function ($languageCode) {
113
                return $this->persistenceHandler->contentLanguageHandler()->loadByLanguageCode($languageCode);
114
            },
115
            $this->getTags,
116
            $this->getKeys
117
        );
118
    }
119
120
    /**
121
     * {@inheritdoc}
122
     */
123 View Code Duplication
    public function loadListByLanguageCodes(array $languageCodes): iterable
124
    {
125
        return $this->getMultipleCacheValues(
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->getMultipl...tTags, $this->getKeys); (array) is incompatible with the return type declared by the interface eZ\Publish\SPI\Persisten...loadListByLanguageCodes of type eZ\Publish\SPI\Persisten...ntent\Language\iterable.

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
            $languageCodes,
127
            'ez-language-code-',
128
            function (array $languageCodes) {
129
                return $this->persistenceHandler->contentLanguageHandler()->loadListByLanguageCodes($languageCodes);
130
            },
131
            $this->getTags,
132
            $this->getKeys
133
        );
134
    }
135
136
    /**
137
     * {@inheritdoc}
138
     */
139
    public function loadAll()
140
    {
141
        return $this->getListCacheValue(
142
            'ez-language-list',
143
            function () {
144
                return $this->persistenceHandler->contentLanguageHandler()->loadAll();
145
            },
146
            $this->getTags,
147
            $this->getKeys
148
        );
149
    }
150
151
    /**
152
     * {@inheritdoc}
153
     */
154 View Code Duplication
    public function delete($id)
155
    {
156
        $this->logger->logCall(__METHOD__, array('language' => $id));
157
        $return = $this->persistenceHandler->contentLanguageHandler()->delete($id);
158
159
        // As we don't have locale we clear cache by tag invalidation
160
        $this->cache->invalidateTags(['language-' . $id]);
161
162
        return $return;
163
    }
164
}
165