Completed
Push — master ( 955b04...f00653 )
by André
38:12 queued 16:07
created

ContentLanguageHandler::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 4
dl 0
loc 16
rs 9.7333
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\Core\Persistence\Cache\InMemory\InMemoryCache;
12
use eZ\Publish\SPI\Persistence\Handler as PersistenceHandler;
13
use eZ\Publish\SPI\Persistence\Content\Language\Handler as ContentLanguageHandlerInterface;
14
use eZ\Publish\SPI\Persistence\Content\Language;
15
use eZ\Publish\SPI\Persistence\Content\Language\CreateStruct;
16
use Symfony\Component\Cache\Adapter\TagAwareAdapterInterface;
17
18
/**
19
 * @see \eZ\Publish\SPI\Persistence\Content\Language\Handler
20
 */
21
class ContentLanguageHandler extends AbstractInMemoryHandler implements ContentLanguageHandlerInterface
22
{
23
    /** @var callable */
24
    private $getTags;
25
26
    /** @var callable */
27
    private $getKeys;
28
29
    public function __construct(
30
        TagAwareAdapterInterface $cache,
31
        PersistenceHandler $persistenceHandler,
32
        PersistenceLogger $logger,
33
        InMemoryCache $inMemory
34
    ) {
35
        parent::__construct($cache, $persistenceHandler, $logger, $inMemory);
36
37
        $this->getTags = static function (Language $language) { return ['language-' . $language->id]; };
38
        $this->getKeys = static function (Language $language) {
39
            return [
40
                'ez-language-' . $language->id,
41
                'ez-language-code-' . $language->languageCode,
42
            ];
43
        };
44
    }
45
46
    /**
47
     * {@inheritdoc}
48
     */
49
    public function create(CreateStruct $struct)
50
    {
51
        $this->logger->logCall(__METHOD__, array('struct' => $struct));
52
        $this->deleteCache(['ez-language-list']);
53
54
        return $this->persistenceHandler->contentLanguageHandler()->create($struct);
55
    }
56
57
    /**
58
     * {@inheritdoc}
59
     */
60 View Code Duplication
    public function update(Language $struct)
61
    {
62
        $this->logger->logCall(__METHOD__, array('struct' => $struct));
63
        $return = $this->persistenceHandler->contentLanguageHandler()->update($struct);
64
65
        $this->deleteCache([
66
            'ez-language-list',
67
            'ez-language-' . $struct->id,
68
            'ez-language-code-' . $struct->languageCode,
69
        ]);
70
71
        return $return;
72
    }
73
74
    /**
75
     * {@inheritdoc}
76
     */
77 View Code Duplication
    public function load($id)
78
    {
79
        return $this->getCacheValue(
80
            $id,
81
            'ez-language-',
82
            function ($id) {
83
                return $this->persistenceHandler->contentLanguageHandler()->load($id);
84
            },
85
            $this->getTags,
86
            $this->getKeys
87
        );
88
    }
89
90
    /**
91
     * {@inheritdoc}
92
     */
93 View Code Duplication
    public function loadList(array $ids): iterable
94
    {
95
        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...
96
            $ids,
97
            'ez-language-',
98
            function (array $ids) {
99
                return $this->persistenceHandler->contentLanguageHandler()->loadList($ids);
100
            },
101
            $this->getTags,
102
            $this->getKeys
103
        );
104
    }
105
106
    /**
107
     * {@inheritdoc}
108
     */
109 View Code Duplication
    public function loadByLanguageCode($languageCode)
110
    {
111
        return $this->getCacheValue(
112
            $languageCode,
113
            'ez-language-code-',
114
            function ($languageCode) {
115
                return $this->persistenceHandler->contentLanguageHandler()->loadByLanguageCode($languageCode);
116
            },
117
            $this->getTags,
118
            $this->getKeys
119
        );
120
    }
121
122
    /**
123
     * {@inheritdoc}
124
     */
125 View Code Duplication
    public function loadListByLanguageCodes(array $languageCodes): iterable
126
    {
127
        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...
128
            $languageCodes,
129
            'ez-language-code-',
130
            function (array $languageCodes) {
131
                return $this->persistenceHandler->contentLanguageHandler()->loadListByLanguageCodes($languageCodes);
132
            },
133
            $this->getTags,
134
            $this->getKeys
135
        );
136
    }
137
138
    /**
139
     * {@inheritdoc}
140
     */
141
    public function loadAll()
142
    {
143
        return $this->getListCacheValue(
144
            'ez-language-list',
145
            function () {
146
                return $this->persistenceHandler->contentLanguageHandler()->loadAll();
147
            },
148
            $this->getTags,
149
            $this->getKeys
150
        );
151
    }
152
153
    /**
154
     * {@inheritdoc}
155
     */
156 View Code Duplication
    public function delete($id)
157
    {
158
        $this->logger->logCall(__METHOD__, array('language' => $id));
159
        $return = $this->persistenceHandler->contentLanguageHandler()->delete($id);
160
161
        // As we don't have locale we clear cache by tag invalidation
162
        $this->invalidateCache(['language-' . $id]);
163
164
        return $return;
165
    }
166
}
167