Completed
Push — 7.5 ( f79096...9972f5 )
by
unknown
27:34
created

URLWildcardService::match()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 2
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * File containing the eZ\Publish\Core\Repository\URLWildcardService 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\Repository;
10
11
use eZ\Publish\API\Repository\URLWildcardService as URLWildcardServiceInterface;
12
use eZ\Publish\API\Repository\Repository as RepositoryInterface;
13
use eZ\Publish\SPI\Persistence\Content\UrlWildcard\Handler;
14
use eZ\Publish\API\Repository\Values\Content\URLWildcard;
15
use eZ\Publish\API\Repository\Values\Content\URLWildcardTranslationResult;
16
use eZ\Publish\SPI\Persistence\Content\UrlWildcard as SPIUrlWildcard;
17
use eZ\Publish\Core\Base\Exceptions\InvalidArgumentException;
18
use eZ\Publish\Core\Base\Exceptions\ContentValidationException;
19
use eZ\Publish\Core\Base\Exceptions\UnauthorizedException;
20
use Exception;
21
22
/**
23
 * URLAlias service.
24
 *
25
 * @example Examples/urlalias.php
26
 */
27
class URLWildcardService implements URLWildcardServiceInterface
28
{
29
    /**
30
     * @var \eZ\Publish\API\Repository\Repository
31
     */
32
    protected $repository;
33
34
    /**
35
     * @var \eZ\Publish\SPI\Persistence\Content\UrlWildcard\Handler
36
     */
37
    protected $urlWildcardHandler;
38
39
    /**
40
     * @var array
41
     */
42
    protected $settings;
43
44
    /**
45
     * Setups service with reference to repository object that created it & corresponding handler.
46
     *
47
     * @param \eZ\Publish\API\Repository\Repository $repository
48
     * @param \eZ\Publish\SPI\Persistence\Content\UrlWildcard\Handler $urlWildcardHandler
49
     * @param array $settings
50
     */
51
    public function __construct(RepositoryInterface $repository, Handler $urlWildcardHandler, array $settings = array())
52
    {
53
        $this->repository = $repository;
54
        $this->urlWildcardHandler = $urlWildcardHandler;
55
        $this->settings = $settings;
56
    }
57
58
    /**
59
     * Creates a new url wildcard.
60
     *
61
     * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if the $sourceUrl pattern already exists
62
     * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to create url wildcards
63
     * @throws \eZ\Publish\API\Repository\Exceptions\ContentValidationException if the number of "*" patterns in $sourceUrl and
64
     *         the numbers in {\d} placeholders in $destinationUrl does not match.
65
     *
66
     * @param string $sourceUrl
67
     * @param string $destinationUrl
68
     * @param bool $forward
69
     *
70
     * @return \eZ\Publish\API\Repository\Values\Content\UrlWildcard
71
     */
72
    public function create($sourceUrl, $destinationUrl, $forward = false): URLWildcard
73
    {
74
        if ($this->repository->hasAccess('content', 'urltranslator') !== true) {
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repository\Repository::hasAccess() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::hasAccess() instead.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
75
            throw new UnauthorizedException('content', 'urltranslator');
76
        }
77
78
        $sourceUrl = $this->cleanUrl($sourceUrl);
79
        $destinationUrl = $this->cleanUrl($destinationUrl);
80
81
        if ($this->urlWildcardHandler->exactSourceUrlExists($this->cleanPath($sourceUrl))) {
82
            throw new InvalidArgumentException(
83
                '$sourceUrl',
84
                'Pattern already exists'
85
            );
86
        }
87
88
        preg_match_all('(\\*)', $sourceUrl, $patterns);
89
        preg_match_all('(\{(\d+)\})', $destinationUrl, $placeholders);
90
91
        $patterns = array_map('intval', $patterns[0]);
92
        $placeholders = array_map('intval', $placeholders[1]);
93
94
        if (!empty($placeholders) && max($placeholders) > count($patterns)) {
95
            throw new ContentValidationException('Placeholders are not matching with wildcards.');
96
        }
97
98
        $this->repository->beginTransaction();
99
        try {
100
            $spiUrlWildcard = $this->urlWildcardHandler->create(
101
                $sourceUrl,
102
                $destinationUrl,
103
                $forward
104
            );
105
            $this->repository->commit();
106
        } catch (Exception $e) {
107
            $this->repository->rollback();
108
            throw $e;
109
        }
110
111
        return $this->buildUrlWildcardDomainObject($spiUrlWildcard);
112
    }
113
114
    /**
115
     * Removes an url wildcard.
116
     *
117
     * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to remove url wildcards
118
     *
119
     * @param \eZ\Publish\API\Repository\Values\Content\UrlWildcard $urlWildcard the url wildcard to remove
120
     */
121 View Code Duplication
    public function remove(URLWildcard $urlWildcard): void
122
    {
123
        if (!$this->repository->canUser('content', 'urltranslator', $urlWildcard)) {
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repository\Repository::canUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::canUser() instead. Indicates if the current user is allowed to perform an action given by the function on the given
objects. Example: canUser( 'content', 'edit', $content, $location ); This will check edit permission on content given the specific location, if skipped if will check on all locations. Example2: canUser( 'section', 'assign', $content, $section ); Check if user has access to assign $content to $section.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
124
            throw new UnauthorizedException('content', 'urltranslator');
125
        }
126
127
        $this->repository->beginTransaction();
128
        try {
129
            $this->urlWildcardHandler->remove(
130
                $urlWildcard->id
131
            );
132
            $this->repository->commit();
133
        } catch (Exception $e) {
134
            $this->repository->rollback();
135
            throw $e;
136
        }
137
    }
138
139
    /**
140
     * Loads a url wild card.
141
     *
142
     * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException if the url wild card was not found
143
     *
144
     * @param mixed $id
145
     *
146
     * @return \eZ\Publish\API\Repository\Values\Content\UrlWildcard
147
     */
148
    public function load($id): URLWildcard
149
    {
150
        return $this->buildUrlWildcardDomainObject(
151
            $this->urlWildcardHandler->load($id)
152
        );
153
    }
154
155
    /**
156
     * Loads all url wild card (paged).
157
     *
158
     * @param int $offset
159
     * @param int $limit
160
     *
161
     * @return \eZ\Publish\API\Repository\Values\Content\UrlWildcard[]
162
     */
163
    public function loadAll($offset = 0, $limit = -1): array
164
    {
165
        $spiUrlWildcards = $this->urlWildcardHandler->loadAll(
166
            $offset,
167
            $limit
168
        );
169
170
        $urlWildcards = array();
171
        foreach ($spiUrlWildcards as $spiUrlWildcard) {
172
            $urlWildcards[] = $this->buildUrlWildcardDomainObject($spiUrlWildcard);
173
        }
174
175
        return $urlWildcards;
176
    }
177
178
    /**
179
     * Translates an url to an existing uri resource based on the
180
     * source/destination patterns of the url wildcard.
181
     *
182
     * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException if the url could not be translated
183
     *
184
     * @param mixed $url
185
     *
186
     * @return \eZ\Publish\API\Repository\Values\Content\URLWildcardTranslationResult
187
     */
188
    public function translate($url): URLWildcardTranslationResult
189
    {
190
        $spiWildcard = $this->urlWildcardHandler->translate($this->cleanPath($url));
191
192
        return new URLWildcardTranslationResult(
193
            [
194
                'uri' => $spiWildcard->destinationUrl,
195
                'forward' => $spiWildcard->forward,
196
            ]
197
        );
198
    }
199
200
    /**
201
     * Builds API UrlWildcard object from given SPI UrlWildcard object.
202
     *
203
     * @param \eZ\Publish\SPI\Persistence\Content\UrlWildcard $wildcard
204
     *
205
     * @return \eZ\Publish\API\Repository\Values\Content\URLWildcard
206
     */
207
    private function buildUrlWildcardDomainObject(SPIUrlWildcard $wildcard): URLWildcard
208
    {
209
        return new URLWildcard(
210
            array(
211
                'id' => $wildcard->id,
212
                'destinationUrl' => $wildcard->destinationUrl,
213
                'sourceUrl' => $wildcard->sourceUrl,
214
                'forward' => $wildcard->forward,
215
            )
216
        );
217
    }
218
219
    /**
220
     * Removes leading and trailing slashes and spaces.
221
     *
222
     * @param string $url
223
     *
224
     * @return string
225
     */
226
    private function cleanUrl(string $url): string
227
    {
228
        return '/' . trim($url, '/ ');
229
    }
230
231
    /**
232
     * Removes leading slash from given path.
233
     *
234
     * @param string $path
235
     *
236
     * @return string
237
     */
238
    private function cleanPath(string $path): string
239
    {
240
        return trim($path, '/ ');
241
    }
242
}
243