Completed
Push — ezp-30616 ( 9239a0...7bf8e8 )
by
unknown
57:53 queued 37:56
created

URLAliasService::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 2
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * URLAliasService 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\SignalSlot;
10
11
use eZ\Publish\API\Repository\URLAliasService as URLAliasServiceInterface;
12
use eZ\Publish\API\Repository\Values\Content\Location;
13
use eZ\Publish\Core\SignalSlot\Signal\LocationService\UpdateLocationSignal;
14
use eZ\Publish\Core\SignalSlot\Signal\URLAliasService\CreateUrlAliasSignal;
15
use eZ\Publish\Core\SignalSlot\Signal\URLAliasService\CreateGlobalUrlAliasSignal;
16
use eZ\Publish\Core\SignalSlot\Signal\URLAliasService\RemoveAliasesSignal;
17
18
/**
19
 * URLAliasService class.
20
 */
21
class URLAliasService implements URLAliasServiceInterface
22
{
23
    /**
24
     * Aggregated service.
25
     *
26
     * @var \eZ\Publish\API\Repository\URLAliasService
27
     */
28
    protected $service;
29
30
    /**
31
     * SignalDispatcher.
32
     *
33
     * @var \eZ\Publish\Core\SignalSlot\SignalDispatcher
34
     */
35
    protected $signalDispatcher;
36
37
    /**
38
     * Constructor.
39
     *
40
     * Construct service object from aggregated service and signal
41
     * dispatcher
42
     *
43
     * @param \eZ\Publish\API\Repository\URLAliasService $service
44
     * @param \eZ\Publish\Core\SignalSlot\SignalDispatcher $signalDispatcher
45
     */
46
    public function __construct(URLAliasServiceInterface $service, SignalDispatcher $signalDispatcher)
47
    {
48
        $this->service = $service;
49
        $this->signalDispatcher = $signalDispatcher;
50
    }
51
52
    /**
53
     * Create a user chosen $alias pointing to $location in $languageCode.
54
     *
55
     * This method runs URL filters and transformers before storing them.
56
     * Hence the path returned in the URLAlias Value may differ from the given.
57
     * $alwaysAvailable makes the alias available in all languages.
58
     *
59
     * @param \eZ\Publish\API\Repository\Values\Content\Location $location
60
     * @param string $path
61
     * @param string $languageCode the languageCode for which this alias is valid
62
     * @param bool $forwarding if true a redirect is performed
63
     * @param bool $alwaysAvailable
64
     *
65
     * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if the path already exists for the given language
66
     *
67
     * @return \eZ\Publish\API\Repository\Values\Content\URLAlias
68
     */
69
    public function createUrlAlias(Location $location, $path, $languageCode, $forwarding = false, $alwaysAvailable = false)
70
    {
71
        $returnValue = $this->service->createUrlAlias($location, $path, $languageCode, $forwarding, $alwaysAvailable);
72
        $this->signalDispatcher->emit(
73
            new CreateUrlAliasSignal(
74
                array(
75
                    'urlAliasId' => $returnValue->id,
76
                )
77
            )
78
        );
79
80
        return $returnValue;
81
    }
82
83
    /**
84
     * Create a user chosen $alias pointing to a resource in $languageCode.
85
     *
86
     * This method does not handle location resources - if a user enters a location target
87
     * the createCustomUrlAlias method has to be used.
88
     * This method runs URL filters and and transformers before storing them.
89
     * Hence the path returned in the URLAlias Value may differ from the given.
90
     *
91
     * $alwaysAvailable makes the alias available in all languages.
92
     *
93
     * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if the path already exists for the given
94
     *         language or if resource is not valid
95
     *
96
     * @param string $resource
97
     * @param string $path
98
     * @param string $languageCode
99
     * @param bool $forwarding
100
     * @param bool $alwaysAvailable
101
     *
102
     * @return \eZ\Publish\API\Repository\Values\Content\URLAlias
103
     */
104
    public function createGlobalUrlAlias($resource, $path, $languageCode, $forwarding = false, $alwaysAvailable = false)
105
    {
106
        $returnValue = $this->service->createGlobalUrlAlias($resource, $path, $languageCode, $forwarding, $alwaysAvailable);
107
        $this->signalDispatcher->emit(
108
            new CreateGlobalUrlAliasSignal(
109
                array(
110
                    'urlAliasId' => $returnValue->id,
111
                )
112
            )
113
        );
114
115
        return $returnValue;
116
    }
117
118
    /**
119
     * List of url aliases pointing to $location, sorted by language priority.
120
     *
121
     * @param \eZ\Publish\API\Repository\Values\Content\Location $location
122
     * @param bool $custom if true the user generated aliases are listed otherwise the autogenerated
123
     * @param string $languageCode filters those which are valid for the given language
124
     * @param bool|null $showAllTranslations Default false from config, include all alias as if they where always available.
125
     * @param array|null $prioritizedLanguageList By default taken from config, used as prioritized language codes for order of returned objects.
126
     *
127
     * @return \eZ\Publish\API\Repository\Values\Content\URLAlias[]
128
     */
129
    public function listLocationAliases(
130
        Location $location,
131
        $custom = true,
132
        $languageCode = null,
133
        bool $showAllTranslations = null,
134
        array $prioritizedLanguageList = null
135
    ) {
136
        return $this->service->listLocationAliases(
137
            $location,
138
            $custom,
139
            $languageCode,
140
            $showAllTranslations,
141
            $prioritizedLanguageList
142
        );
143
    }
144
145
    /**
146
     * List global aliases.
147
     *
148
     * @param string $languageCode filters those which are valid for the given language
149
     * @param int $offset
150
     * @param int $limit
151
     *
152
     * @return \eZ\Publish\API\Repository\Values\Content\URLAlias[]
153
     */
154
    public function listGlobalAliases($languageCode = null, $offset = 0, $limit = -1)
155
    {
156
        return $this->service->listGlobalAliases($languageCode, $offset, $limit);
157
    }
158
159
    /**
160
     * Removes urls aliases.
161
     *
162
     * This method does not remove autogenerated aliases for locations.
163
     *
164
     * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if alias list contains
165
     *         autogenerated alias
166
     *
167
     * @param \eZ\Publish\API\Repository\Values\Content\URLAlias[] $aliasList
168
     */
169
    public function removeAliases(array $aliasList)
170
    {
171
        $returnValue = $this->service->removeAliases($aliasList);
172
        $this->signalDispatcher->emit(
173
            new RemoveAliasesSignal(
174
                array(
175
                    'aliasList' => $aliasList,
176
                )
177
            )
178
        );
179
180
        return $returnValue;
181
    }
182
183
    /**
184
     * looks up the URLAlias for the given url.
185
     *
186
     * @param string $url
187
     * @param string $languageCode
188
     *
189
     * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException if the path does not exist or is not valid for the given language
190
     *
191
     * @return \eZ\Publish\API\Repository\Values\Content\URLAlias
192
     */
193
    public function lookup($url, $languageCode = null)
194
    {
195
        return $this->service->lookup($url, $languageCode);
196
    }
197
198
    /**
199
     * Returns the URL alias for the given location in the given language.
200
     *
201
     * If $languageCode is null the method returns the url alias in the most prioritized language.
202
     *
203
     * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException if no url alias exist for the given language
204
     *
205
     * @param \eZ\Publish\API\Repository\Values\Content\Location $location
206
     * @param string $languageCode
207
     * @param bool|null $showAllTranslations Default false from config, include all alias as if they where always available.
208
     * @param array|null $prioritizedLanguageList By default taken from config, used as prioritized language codes for order of returned objects.
209
     *
210
     * @return \eZ\Publish\API\Repository\Values\Content\URLAlias
211
     */
212
    public function reverseLookup(
213
        Location $location,
214
        $languageCode = null,
215
        bool $showAllTranslations = null,
216
        array $prioritizedLanguageList = null
217
    ) {
218
        return $this->service->reverseLookup(
219
            $location,
220
            $languageCode,
221
            $showAllTranslations,
222
            $prioritizedLanguageList
223
        );
224
    }
225
226
    /**
227
     * Loads URL alias by given $id.
228
     *
229
     * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException
230
     *
231
     * @param string $id
232
     *
233
     * @return \eZ\Publish\API\Repository\Values\Content\URLAlias
234
     */
235
    public function load($id)
236
    {
237
        return $this->service->load($id);
238
    }
239
240
    /**
241
     * Refresh all system URL aliases for the given Location (and historize outdated if needed).
242
     *
243
     * @param \eZ\Publish\API\Repository\Values\Content\Location $location
244
     */
245 View Code Duplication
    public function refreshSystemUrlAliasesForLocation(Location $location): void
246
    {
247
        $this->service->refreshSystemUrlAliasesForLocation($location);
248
249
        $this->signalDispatcher->emit(
250
            new UpdateLocationSignal(
251
                [
252
                    'contentId' => $location->contentId,
253
                    'locationId' => $location->id,
254
                    'parentLocationId' => $location->parentLocationId,
255
                ]
256
            )
257
        );
258
    }
259
260
    /**
261
     * Delete global, system or custom URL alias pointing to non-existent Locations.
262
     *
263
     * @return int Number of deleted URL aliases
264
     */
265
    public function deleteCorruptedUrlAliases(): int
266
    {
267
        return $this->service->deleteCorruptedUrlAliases();
268
    }
269
}
270