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

Handler::loadList()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 7
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
use eZ\Publish\Core\Base\Exceptions\NotFoundException;
15
use LogicException;
16
17
/**
18
 * Language Handler.
19
 */
20
class Handler implements BaseLanguageHandler
21
{
22
    /**
23
     * Language Gateway.
24
     *
25
     * @var \eZ\Publish\Core\Persistence\Legacy\Content\Language\Gateway
26
     */
27
    protected $languageGateway;
28
29
    /**
30
     * Language Mapper.
31
     *
32
     * @var \eZ\Publish\Core\Persistence\Legacy\Content\Language\Mapper
33
     */
34
    protected $languageMapper;
35
36
    /**
37
     * Creates a new Language Handler.
38
     *
39
     * @param \eZ\Publish\Core\Persistence\Legacy\Content\Language\Gateway $languageGateway
40
     * @param \eZ\Publish\Core\Persistence\Legacy\Content\Language\Mapper $languageMapper
41
     */
42
    public function __construct(Gateway $languageGateway, Mapper $languageMapper)
43
    {
44
        $this->languageGateway = $languageGateway;
45
        $this->languageMapper = $languageMapper;
46
    }
47
48
    /**
49
     * Create a new language.
50
     *
51
     * @param \eZ\Publish\SPI\Persistence\Content\Language\CreateStruct $struct
52
     *
53
     * @return \eZ\Publish\SPI\Persistence\Content\Language
54
     */
55
    public function create(CreateStruct $struct)
56
    {
57
        $language = $this->languageMapper->createLanguageFromCreateStruct(
58
            $struct
59
        );
60
        $language->id = $this->languageGateway->insertLanguage($language);
61
62
        return $language;
63
    }
64
65
    /**
66
     * Update language.
67
     *
68
     * @param \eZ\Publish\SPI\Persistence\Content\Language $language
69
     */
70
    public function update(Language $language)
71
    {
72
        $this->languageGateway->updateLanguage($language);
73
    }
74
75
    /**
76
     * Get language by id.
77
     *
78
     * @param mixed $id
79
     *
80
     * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException If language could not be found by $id
81
     *
82
     * @return \eZ\Publish\SPI\Persistence\Content\Language
83
     */
84 View Code Duplication
    public function load($id)
85
    {
86
        $languages = $this->languageMapper->extractLanguagesFromRows(
87
            $this->languageGateway->loadLanguageListData([$id])
88
        );
89
90
        if (count($languages) < 1) {
91
            throw new NotFoundException('Language', $id);
92
        }
93
94
        return reset($languages);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The expression reset($languages); of type eZ\Publish\SPI\Persistence\Content\Language|false adds false to the return on line 94 which is incompatible with the return type declared by the interface eZ\Publish\SPI\Persisten...\Language\Handler::load of type eZ\Publish\SPI\Persistence\Content\Language. It seems like you forgot to handle an error condition.
Loading history...
95
    }
96
97
    /**
98
     * {@inheritdoc}
99
     */
100
    public function loadList(array $ids): iterable
101
    {
102
        return $this->languageMapper->extractLanguagesFromRows(
103
            $this->languageGateway->loadLanguageListData($ids),
104
            'id'
105
        );
106
    }
107
108
    /**
109
     * Get language by Language Code (eg: eng-GB).
110
     *
111
     * @param string $languageCode
112
     *
113
     * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException If language could not be found by $languageCode
114
     *
115
     * @return \eZ\Publish\SPI\Persistence\Content\Language
116
     */
117 View Code Duplication
    public function loadByLanguageCode($languageCode)
118
    {
119
        $languages = $this->languageMapper->extractLanguagesFromRows(
120
            $this->languageGateway->loadLanguageListDataByLanguageCode([$languageCode])
121
        );
122
123
        if (count($languages) < 1) {
124
            throw new NotFoundException('Language', $languageCode);
125
        }
126
127
        return reset($languages);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The expression reset($languages); of type eZ\Publish\SPI\Persistence\Content\Language|false adds false to the return on line 127 which is incompatible with the return type declared by the interface eZ\Publish\SPI\Persisten...ler::loadByLanguageCode of type eZ\Publish\SPI\Persistence\Content\Language. It seems like you forgot to handle an error condition.
Loading history...
128
    }
129
130
    /**
131
     * {@inheritdoc}
132
     */
133
    public function loadListByLanguageCodes(array $languageCodes): iterable
134
    {
135
        return $this->languageMapper->extractLanguagesFromRows(
136
            $this->languageGateway->loadLanguageListDataByLanguageCode($languageCodes)
137
        );
138
    }
139
140
    /**
141
     * Get all languages.
142
     *
143
     * @return \eZ\Publish\SPI\Persistence\Content\Language[]
144
     */
145
    public function loadAll()
146
    {
147
        return $this->languageMapper->extractLanguagesFromRows(
148
            $this->languageGateway->loadAllLanguagesData()
149
        );
150
    }
151
152
    /**
153
     * Delete a language.
154
     *
155
     * @param mixed $id
156
     *
157
     * @throws LogicException If language could not be deleted
158
     */
159
    public function delete($id)
160
    {
161
        if (!$this->languageGateway->canDeleteLanguage($id)) {
162
            throw new LogicException("Deleting language logic error, some content still references that language and therefore it can't be deleted");
163
        }
164
165
        $this->languageGateway->deleteLanguage($id);
166
    }
167
}
168