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

Cache::remove()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 3
nop 1
dl 0
loc 9
rs 9.9666
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * File containing the Language Cache 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\Core\Base\Exceptions\NotFoundException;
13
14
/**
15
 * Language Cache.
16
 */
17
class Cache
18
{
19
    /**
20
     * Maps IDs to Language objects.
21
     *
22
     * @var \eZ\Publish\SPI\Persistence\Content\Language[]
23
     */
24
    protected $mapById = array();
25
26
    /**
27
     * Maps locales to Language objects.
28
     *
29
     * @var \eZ\Publish\SPI\Persistence\Content\Language[]
30
     */
31
    protected $mapByLocale = array();
32
33
    /**
34
     * Stores the $language into the cache.
35
     *
36
     * @param \eZ\Publish\SPI\Persistence\Content\Language $language
37
     */
38
    public function store(Language $language)
39
    {
40
        $this->mapById[$language->id] = $language;
41
        $this->mapByLocale[$language->languageCode] = $language;
42
    }
43
44
    /**
45
     * Removes the language with $id from the cache.
46
     *
47
     * @param mixed $id
48
     */
49
    public function remove($id)
50
    {
51
        unset($this->mapById[$id]);
52
        foreach ($this->mapByLocale as $languageCode => $language) {
53
            if ($language->id == $id) {
54
                unset($this->mapByLocale[$languageCode]);
55
            }
56
        }
57
    }
58
59
    /**
60
     * Returns the Language with $id from the cache.
61
     *
62
     * @param mixed $id
63
     *
64
     * @return \eZ\Publish\SPI\Persistence\Content\Language
65
     *
66
     * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException
67
     *         if the Language could not be found
68
     */
69
    public function getById($id)
70
    {
71
        if (!isset($this->mapById[$id])) {
72
            throw new NotFoundException('Language', $id);
73
        }
74
75
        return $this->mapById[$id];
76
    }
77
78
    /**
79
     * Returns Languages with $ids from the cache.
80
     *
81
     * @param int[] $ids
82
     *
83
     * @return \eZ\Publish\SPI\Persistence\Content\Language[]|iterable
84
     */
85 View Code Duplication
    public function getListById(array $ids): iterable
86
    {
87
        $languages = [];
88
        foreach ($ids as $id) {
89
            if (isset($this->mapById[$id])) {
90
                $languages[$id] = $this->mapById[$id];
91
            }
92
        }
93
94
        return $languages;
95
    }
96
97
    /**
98
     * Returns the Language with $languageCode from the cache.
99
     *
100
     * @param string $languageCode
101
     *
102
     * @return \eZ\Publish\SPI\Persistence\Content\Language
103
     *
104
     * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException
105
     *         if the Language could not be found
106
     */
107
    public function getByLocale($languageCode)
108
    {
109
        if (!isset($this->mapByLocale[$languageCode])) {
110
            throw new NotFoundException('Language', $languageCode);
111
        }
112
113
        return $this->mapByLocale[$languageCode];
114
    }
115
116
    /**
117
     * Returns Languages with $languageCodes from the cache.
118
     *
119
     * @param string[] $languageCodes
120
     *
121
     * @return \eZ\Publish\SPI\Persistence\Content\Language[]|iterable
122
     */
123 View Code Duplication
    public function getListByLocale(array $languageCodes): iterable
124
    {
125
        $languages = [];
126
        foreach ($languageCodes as $languageCode) {
127
            if (isset($this->mapByLocale[$languageCode])) {
128
                $languages[$languageCode] = $this->mapByLocale[$languageCode];
129
            }
130
        }
131
132
        return $languages;
133
    }
134
135
    /**
136
     * Returns all languages in the cache with locale as key.
137
     *
138
     * @return \eZ\Publish\SPI\Persistence\Content\Language[]
139
     */
140
    public function getAll()
141
    {
142
        return $this->mapByLocale;
143
    }
144
145
    /**
146
     * CLear language cache.
147
     */
148
    public function clearCache()
149
    {
150
        $this->mapByLocale = $this->mapById = array();
151
    }
152
}
153