Completed
Push — inmemory-pool-decoration ( 282551...25eaa5 )
by André
66:29 queued 45:47
created

ContentLanguageHandler::init()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
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 retrival.
28
     */
29
    protected function init(): void
30
    {
31
        $this->getTags = static function (Language $language) { return ['language-' . $language->id]; };
32
        $this->getKeys = static function (Language $language) {
33
            return [
34
                'ez-language-' . $language->id,
35
                'ez-language-code-' . $language->languageCode,
36
            ];
37
        };
38
    }
39
40
    /**
41
     * {@inheritdoc}
42
     */
43 View Code Duplication
    public function create(CreateStruct $struct)
44
    {
45
        $this->logger->logCall(__METHOD__, array('struct' => $struct));
46
        $this->cache->deleteItems(['ez-language-list']);
47
48
        return $this->persistenceHandler->contentLanguageHandler()->create($struct);
49
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54
    public function update(Language $struct)
55
    {
56
        $this->logger->logCall(__METHOD__, array('struct' => $struct));
57
        $return = $this->persistenceHandler->contentLanguageHandler()->update($struct);
58
59
        $this->cache->deleteItems([
60
            'ez-language-list',
61
            'ez-language-' . $struct->id,
62
            'ez-language-code-' . $struct->languageCode,
63
        ]);
64
65
        return $return;
66
    }
67
68
    /**
69
     * {@inheritdoc}
70
     */
71 View Code Duplication
    public function load($id)
72
    {
73
        return $this->getCacheValue(
74
            $id,
75
            'ez-language-',
76
            function ($id) {
77
                return $this->persistenceHandler->contentLanguageHandler()->load($id);
78
            },
79
            $this->getTags,
80
            $this->getKeys
81
        );
82
    }
83
84
    /**
85
     * {@inheritdoc}
86
     */
87 View Code Duplication
    public function loadList(array $ids): iterable
88
    {
89
        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...
90
            $ids,
91
            'ez-language-',
92
            function (array $ids) {
93
                return $this->persistenceHandler->contentLanguageHandler()->loadList($ids);
94
            },
95
            $this->getTags,
96
            $this->getKeys
97
        );
98
    }
99
100
    /**
101
     * {@inheritdoc}
102
     */
103 View Code Duplication
    public function loadByLanguageCode($languageCode)
104
    {
105
        return $this->getCacheValue(
106
            $languageCode,
107
            'ez-language-code-',
108
            function ($languageCode) {
109
                return $this->persistenceHandler->contentLanguageHandler()->loadByLanguageCode($languageCode);
110
            },
111
            $this->getTags,
112
            $this->getKeys
113
        );
114
    }
115
116
    /**
117
     * {@inheritdoc}
118
     */
119 View Code Duplication
    public function loadListByLanguageCodes(array $languageCodes): iterable
120
    {
121
        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...
122
            $languageCodes,
123
            'ez-language-code-',
124
            function (array $languageCodes) {
125
                return $this->persistenceHandler->contentLanguageHandler()->loadListByLanguageCodes($languageCodes);
126
            },
127
            $this->getTags,
128
            $this->getKeys
129
        );
130
    }
131
132
    /**
133
     * {@inheritdoc}
134
     */
135
    public function loadAll()
136
    {
137
        return $this->getListCacheValue(
138
            'ez-language-list',
139
            function () {
140
                return $this->persistenceHandler->contentLanguageHandler()->loadAll();
141
            },
142
            $this->getTags,
143
            $this->getKeys
144
        );
145
    }
146
147
    /**
148
     * {@inheritdoc}
149
     */
150 View Code Duplication
    public function delete($id)
151
    {
152
        $this->logger->logCall(__METHOD__, array('language' => $id));
153
        $return = $this->persistenceHandler->contentLanguageHandler()->delete($id);
154
155
        // As we don't have locale we clear cache by tag invalidation
156
        $this->cache->invalidateTags(['language-' . $id]);
157
158
        return $return;
159
    }
160
}
161