Completed
Push — qa-cumulative-ezp-31278-31279-... ( f57b9c...05fe70 )
by
unknown
14:35
created

Prefix::undecorate()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 3
nop 1
dl 0
loc 13
rs 9.8333
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
5
 * @license For full copyright and license information view LICENSE file distributed with this source code.
6
 */
7
declare(strict_types=1);
8
9
namespace eZ\Publish\Core\IO\UrlDecorator;
10
11
use eZ\Publish\Core\IO\Exception\InvalidBinaryPrefixException;
12
use eZ\Publish\Core\IO\IOConfigProvider;
13
use eZ\Publish\Core\IO\UrlDecorator;
14
15
/**
16
 * Prefixes the URI with a string. Ensures an initial / in the parameter.
17
 */
18
class Prefix implements UrlDecorator
19
{
20
    /** @var \eZ\Publish\Core\IO\IOConfigProvider */
21
    protected $ioConfigResolver;
22
23
    public function __construct(IOConfigProvider $IOConfigResolver)
24
    {
25
        $this->ioConfigResolver = $IOConfigResolver;
26
    }
27
28
    public function getPrefix(): string
29
    {
30
        $prefix = $this->ioConfigResolver->getLegacyUrlPrefix();
31
32
        return trim($prefix, '/') . '/';
33
    }
34
35
    public function decorate($id)
36
    {
37
        $prefix = $this->getPrefix();
38
        if (empty($prefix)) {
39
            return $id;
40
        }
41
42
        return $prefix . trim($id, '/');
43
    }
44
45
    /**
46
     * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
47
     */
48
    public function undecorate($url)
49
    {
50
        $prefix = $this->getPrefix();
51
        if (empty($prefix)) {
52
            return $url;
53
        }
54
55
        if (strpos($url, $prefix) !== 0) {
56
            throw new InvalidBinaryPrefixException($url, $prefix);
57
        }
58
59
        return trim(substr($url, strlen($prefix)), '/');
60
    }
61
}
62