Completed
Push — in-memory-cache2 ( de4787 )
by André
21:05
created

ContentLanguageHandler::loadListByLanguageCodes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15

Duplication

Lines 5
Ratio 33.33 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 5
loc 15
rs 9.7666
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 AbstractHandler implements ContentLanguageHandlerInterface
19
{
20
    /**
21
     * {@inheritdoc}
22
     */
23
    public function create(CreateStruct $struct)
24
    {
25
        $this->logger->logCall(__METHOD__, array('struct' => $struct));
26
        $this->cache->deleteItem('ez-language-list');
27
28
        return $this->persistenceHandler->contentLanguageHandler()->create($struct);
29
    }
30
31
    /**
32
     * {@inheritdoc}
33
     */
34
    public function update(Language $struct)
35
    {
36
        $this->logger->logCall(__METHOD__, array('struct' => $struct));
37
        $return = $this->persistenceHandler->contentLanguageHandler()->update($struct);
38
39
        $this->cache->invalidateTags(['language-' . $struct->id]);
40
41
        return $return;
42
    }
43
44
    /**
45
     * {@inheritdoc}
46
     */
47 View Code Duplication
    public function load($id)
48
    {
49
        $cacheItem = $this->cache->getItem('ez-language-' . $id);
50
        if ($cacheItem->isHit()) {
51
            return $cacheItem->get();
52
        }
53
54
        $this->logger->logCall(__METHOD__, array('language' => $id));
55
        $language = $this->persistenceHandler->contentLanguageHandler()->load($id);
56
57
        $cacheItem->set($language);
58
        $cacheItem->tag('language-' . $language->id);
59
        $this->cache->save($cacheItem);
60
61
        return $language;
62
    }
63
64
    /**
65
     * {@inheritdoc}
66
     */
67
    public function loadList(array $ids): iterable
68
    {
69
        return $this->getMultipleCacheItems(
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->getMultipl...' . $language->id); }); (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...
70
            $ids,
71
            'ez-language-',
72 View Code Duplication
            function (array $cacheMissIds) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
73
                $this->logger->logCall(__CLASS__ . '::loadList', ['languages' => $cacheMissIds]);
74
75
                return $this->persistenceHandler->contentLanguageHandler()->loadList($cacheMissIds);
76
            },
77
            function (Language $language) {
78
                return ['language-' . $language->id];
79
            }
80
        );
81
    }
82
83
    /**
84
     * {@inheritdoc}
85
     */
86 View Code Duplication
    public function loadByLanguageCode($languageCode)
87
    {
88
        $cacheItem = $this->cache->getItem('ez-language-code-' . $languageCode);
89
        if ($cacheItem->isHit()) {
90
            return $cacheItem->get();
91
        }
92
93
        $this->logger->logCall(__METHOD__, array('language' => $languageCode));
94
        $language = $this->persistenceHandler->contentLanguageHandler()->loadByLanguageCode($languageCode);
95
96
        $cacheItem->set($language);
97
        $cacheItem->tag('language-' . $language->id);
98
        $this->cache->save($cacheItem);
99
100
        return $language;
101
    }
102
103
    /**
104
     * {@inheritdoc}
105
     */
106
    public function loadListByLanguageCodes(array $languageCodes): iterable
107
    {
108
        return $this->getMultipleCacheItems(
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->getMultipl...' . $language->id); }); (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...
109
            $languageCodes,
110
            'ez-language-code-',
111 View Code Duplication
            function (array $cacheMissIds) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
112
                $this->logger->logCall(__CLASS__ . '::loadListByLanguageCodes', ['languages' => $cacheMissIds]);
113
114
                return $this->persistenceHandler->contentLanguageHandler()->loadListByLanguageCodes($cacheMissIds);
115
            },
116
            function (Language $language) {
117
                return ['language-' . $language->id];
118
            }
119
        );
120
    }
121
122
    /**
123
     * {@inheritdoc}
124
     */
125
    public function loadAll()
126
    {
127
        $cacheItem = $this->cache->getItem('ez-language-list');
128
        if ($cacheItem->isHit()) {
129
            return $cacheItem->get();
130
        }
131
132
        $this->logger->logCall(__METHOD__);
133
        $languages = $this->persistenceHandler->contentLanguageHandler()->loadAll();
134
135
        $cacheTags = [];
136
        foreach ($languages as $language) {
137
            $cacheTags[] = 'language-' . $language->id;
138
        }
139
140
        $cacheItem->set($languages);
141
        $cacheItem->tag($cacheTags);
142
        $this->cache->save($cacheItem);
143
144
        return $languages;
145
    }
146
147
    /**
148
     * {@inheritdoc}
149
     */
150
    public function delete($id)
151
    {
152
        $this->logger->logCall(__METHOD__, array('language' => $id));
153
        $return = $this->persistenceHandler->contentLanguageHandler()->delete($id);
154
155
        $this->cache->invalidateTags(['language-' . $id]);
156
157
        return $return;
158
    }
159
}
160