Completed
Push — master ( f2dfe9...84d91c )
by André
35:37 queued 16:15
created

CachingHandler::loadListByLanguageCodes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * File containing the Language Handler 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\Legacy\Content\Language;
10
11
use eZ\Publish\SPI\Persistence\Content\Language;
12
use eZ\Publish\SPI\Persistence\Content\Language\Handler as BaseLanguageHandler;
13
use eZ\Publish\SPI\Persistence\Content\Language\CreateStruct;
14
15
/**
16
 * Language Handler.
17
 */
18
class CachingHandler implements BaseLanguageHandler
19
{
20
    /**
21
     * Inner Language handler.
22
     *
23
     * @var \eZ\Publish\Core\Persistence\Legacy\Content\Language\Handler
24
     */
25
    protected $innerHandler;
26
27
    /**
28
     * Language cache.
29
     *
30
     * @var \eZ\Publish\Core\Persistence\Legacy\Content\Language\Cache
31
     */
32
    protected $languageCache;
33
34
    /**
35
     * If the cache has already been initialized.
36
     *
37
     * @var bool
38
     */
39
    protected $isCacheInitialized = false;
40
41
    /**
42
     * Creates a caching handler around $innerHandler.
43
     *
44
     * @param \eZ\Publish\SPI\Persistence\Content\Language\Handler $innerHandler
45
     */
46
    public function __construct(BaseLanguageHandler $innerHandler, Cache $languageCache)
47
    {
48
        $this->innerHandler = $innerHandler;
0 ignored issues
show
Documentation Bug introduced by
$innerHandler is of type object<eZ\Publish\SPI\Pe...ntent\Language\Handler>, but the property $innerHandler was declared to be of type object<eZ\Publish\Core\P...ntent\Language\Handler>. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
49
        $this->languageCache = $languageCache;
50
    }
51
52
    /**
53
     * Initializes the cache if necessary.
54
     */
55
    protected function initializeCache()
56
    {
57
        if (false === $this->isCacheInitialized) {
58
            $languages = $this->innerHandler->loadAll();
59
            foreach ($languages as $language) {
60
                $this->languageCache->store($language);
61
            }
62
            $this->isCacheInitialized = true;
63
        }
64
    }
65
66
    /**
67
     * Create a new language.
68
     *
69
     * @param \eZ\Publish\SPI\Persistence\Content\Language\CreateStruct $struct
70
     *
71
     * @return \eZ\Publish\SPI\Persistence\Content\Language
72
     */
73
    public function create(CreateStruct $struct)
74
    {
75
        $this->initializeCache();
76
        $language = $this->innerHandler->create($struct);
77
        $this->languageCache->store($language);
78
79
        return $language;
80
    }
81
82
    /**
83
     * Update language.
84
     *
85
     * @param \eZ\Publish\SPI\Persistence\Content\Language $language
86
     */
87
    public function update(Language $language)
88
    {
89
        $this->initializeCache();
90
        $this->innerHandler->update($language);
91
        $this->languageCache->store($language);
92
    }
93
94
    /**
95
     * Get language by id.
96
     *
97
     * @param mixed $id
98
     *
99
     * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException If language could not be found by $id
100
     *
101
     * @return \eZ\Publish\SPI\Persistence\Content\Language
102
     */
103
    public function load($id)
104
    {
105
        $this->initializeCache();
106
107
        return $this->languageCache->getById($id);
108
    }
109
110
    /**
111
     * {@inheritdoc}
112
     */
113
    public function loadList(array $ids): iterable
114
    {
115
        $this->initializeCache();
116
117
        return $this->languageCache->getListById($ids);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->languageCache->getListById($ids); (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...
118
    }
119
120
    /**
121
     * Get language by Language Code (eg: eng-GB).
122
     *
123
     * @param string $languageCode
124
     *
125
     * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException If language could not be found by $languageCode
126
     *
127
     * @return \eZ\Publish\SPI\Persistence\Content\Language
128
     */
129
    public function loadByLanguageCode($languageCode)
130
    {
131
        $this->initializeCache();
132
133
        return $this->languageCache->getByLocale($languageCode);
134
    }
135
136
    /**
137
     * {@inheritdoc}
138
     */
139
    public function loadListByLanguageCodes(array $languageCodes): iterable
140
    {
141
        $this->initializeCache();
142
143
        return $this->languageCache->getListByLocale($languageCodes);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->languageCa...Locale($languageCodes); (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...
144
    }
145
146
    /**
147
     * Get all languages.
148
     *
149
     * @return \eZ\Publish\SPI\Persistence\Content\Language[]
150
     */
151
    public function loadAll()
152
    {
153
        $this->initializeCache();
154
155
        return $this->languageCache->getAll();
156
    }
157
158
    /**
159
     * Delete a language.
160
     *
161
     * @param mixed $id
162
     */
163
    public function delete($id)
164
    {
165
        $this->initializeCache();
166
        $this->innerHandler->delete($id);
167
        $this->languageCache->remove($id);
168
    }
169
170
    /**
171
     * Clear internal cache.
172
     */
173
    public function clearCache()
174
    {
175
        $this->isCacheInitialized = false;
176
        $this->languageCache->clearCache();
177
    }
178
}
179