Completed
Push — bulk_load_languages ( d98d09 )
by André
23:21
created

ContentLanguageHandler::loadList()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15

Duplication

Lines 15
Ratio 100 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 15
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
27
        return $this->persistenceHandler->contentLanguageHandler()->create($struct);
28
    }
29
30
    /**
31
     * {@inheritdoc}
32
     */
33
    public function update(Language $struct)
34
    {
35
        $this->logger->logCall(__METHOD__, array('struct' => $struct));
36
        $return = $this->persistenceHandler->contentLanguageHandler()->update($struct);
37
38
        $this->cache->invalidateTags(['language-' . $struct->id]);
39
40
        return $return;
41
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46 View Code Duplication
    public function load($id)
47
    {
48
        $cacheItem = $this->cache->getItem('ez-language-' . $id);
49
        if ($cacheItem->isHit()) {
50
            return $cacheItem->get();
51
        }
52
53
        $this->logger->logCall(__METHOD__, array('language' => $id));
54
        $language = $this->persistenceHandler->contentLanguageHandler()->load($id);
55
56
        $cacheItem->set($language);
57
        $cacheItem->tag('language-' . $language->id);
58
        $this->cache->save($cacheItem);
59
60
        return $language;
61
    }
62
63
    /**
64
     * {@inheritdoc}
65
     */
66 View Code Duplication
    public function loadList(array $ids): iterable
67
    {
68
        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...
69
            $ids,
70
            'ez-language-',
71
            function (array $cacheMissIds) {
72
                $this->logger->logCall(__CLASS__ . '::loadList', ['languages' => $cacheMissIds]);
73
74
                return $this->persistenceHandler->contentLanguageHandler()->loadList($cacheMissIds);
75
            },
76
            function (Language $language) {
77
                return ['language-' . $language->id];
78
            }
79
        );
80
    }
81
82
    /**
83
     * {@inheritdoc}
84
     */
85 View Code Duplication
    public function loadByLanguageCode($languageCode)
86
    {
87
        $cacheItem = $this->cache->getItem('ez-language-code-' . $languageCode);
88
        if ($cacheItem->isHit()) {
89
            return $cacheItem->get();
90
        }
91
92
        $this->logger->logCall(__METHOD__, array('language' => $languageCode));
93
        $language = $this->persistenceHandler->contentLanguageHandler()->loadByLanguageCode($languageCode);
94
95
        $cacheItem->set($language);
96
        $cacheItem->tag('language-' . $language->id);
97
        $this->cache->save($cacheItem);
98
99
        return $language;
100
    }
101
102
    /**
103
     * {@inheritdoc}
104
     */
105 View Code Duplication
    public function loadListByLanguageCodes(array $languageCodes): iterable
106
    {
107
        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...
108
            $languageCodes,
109
            'ez-language-code-',
110
            function (array $cacheMissIds) {
111
                $this->logger->logCall(__CLASS__ . '::loadListByLanguageCodes', ['languages' => $cacheMissIds]);
112
113
                return $this->persistenceHandler->contentLanguageHandler()->loadListByLanguageCodes($cacheMissIds);
114
            },
115
            function (Language $language) {
116
                return ['language-' . $language->id];
117
            }
118
        );
119
    }
120
121
    /**
122
     * {@inheritdoc}
123
     */
124
    public function loadAll()
125
    {
126
        $this->logger->logCall(__METHOD__);
127
128
        return $this->persistenceHandler->contentLanguageHandler()->loadAll();
129
    }
130
131
    /**
132
     * {@inheritdoc}
133
     */
134
    public function delete($id)
135
    {
136
        $this->logger->logCall(__METHOD__, array('language' => $id));
137
        $return = $this->persistenceHandler->contentLanguageHandler()->delete($id);
138
139
        $this->cache->invalidateTags(['language-' . $id]);
140
141
        return $return;
142
    }
143
}
144