Completed
Push — ezp-30616 ( 3c2fb4...b64679 )
by
unknown
30:04 queued 14:26
created

URLAliasService::createGlobalUrlAlias()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 31

Duplication

Lines 31
Ratio 100 %

Importance

Changes 0
Metric Value
cc 3
nc 3
nop 5
dl 31
loc 31
rs 9.424
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\Event;
10
11
use eZ\Publish\SPI\Repository\Decorator\URLAliasServiceDecorator;
12
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
13
use eZ\Publish\API\Repository\URLAliasService as URLAliasServiceInterface;
14
use eZ\Publish\API\Repository\Values\Content\Location;
15
use eZ\Publish\Core\Event\URLAlias\BeforeCreateGlobalUrlAliasEvent;
16
use eZ\Publish\Core\Event\URLAlias\BeforeCreateUrlAliasEvent;
17
use eZ\Publish\Core\Event\URLAlias\BeforeRefreshSystemUrlAliasesForLocationEvent;
18
use eZ\Publish\Core\Event\URLAlias\BeforeRemoveAliasesEvent;
19
use eZ\Publish\Core\Event\URLAlias\CreateGlobalUrlAliasEvent;
20
use eZ\Publish\Core\Event\URLAlias\CreateUrlAliasEvent;
21
use eZ\Publish\Core\Event\URLAlias\RefreshSystemUrlAliasesForLocationEvent;
22
use eZ\Publish\Core\Event\URLAlias\RemoveAliasesEvent;
23
use eZ\Publish\Core\Event\URLAlias\URLAliasEvents;
24
25
class URLAliasService extends URLAliasServiceDecorator implements URLAliasServiceInterface
26
{
27
    /**
28
     * @var \Symfony\Component\EventDispatcher\EventDispatcherInterface
29
     */
30
    protected $eventDispatcher;
31
32
    public function __construct(
33
        URLAliasServiceInterface $innerService,
34
        EventDispatcherInterface $eventDispatcher
35
    ) {
36
        parent::__construct($innerService);
37
38
        $this->eventDispatcher = $eventDispatcher;
39
    }
40
41 View Code Duplication
    public function createUrlAlias(
42
        Location $location,
43
        $path,
44
        $languageCode,
45
        $forwarding = false,
46
        $alwaysAvailable = false
47
    ) {
48
        $eventData = [
49
            $location,
50
            $path,
51
            $languageCode,
52
            $forwarding,
53
            $alwaysAvailable,
54
        ];
55
56
        $beforeEvent = new BeforeCreateUrlAliasEvent(...$eventData);
0 ignored issues
show
Bug introduced by
The call to BeforeCreateUrlAliasEvent::__construct() misses some required arguments starting with $path.
Loading history...
57
        if ($this->eventDispatcher->dispatch(URLAliasEvents::BEFORE_CREATE_URL_ALIAS, $beforeEvent)->isPropagationStopped()) {
58
            return $beforeEvent->getUrlAlias();
59
        }
60
61
        $urlAlias = $beforeEvent->hasUrlAlias()
62
            ? $beforeEvent->getUrlAlias()
63
            : parent::createUrlAlias($location, $path, $languageCode, $forwarding, $alwaysAvailable);
64
65
        $this->eventDispatcher->dispatch(
66
            URLAliasEvents::CREATE_URL_ALIAS,
67
            new CreateUrlAliasEvent($urlAlias, ...$eventData)
0 ignored issues
show
Bug introduced by
The call to CreateUrlAliasEvent::__construct() misses some required arguments starting with $path.
Loading history...
68
        );
69
70
        return $urlAlias;
71
    }
72
73 View Code Duplication
    public function createGlobalUrlAlias(
74
        $resource,
75
        $path,
76
        $languageCode,
77
        $forwarding = false,
78
        $alwaysAvailable = false
79
    ) {
80
        $eventData = [
81
            $resource,
82
            $path,
83
            $languageCode,
84
            $forwarding,
85
            $alwaysAvailable,
86
        ];
87
88
        $beforeEvent = new BeforeCreateGlobalUrlAliasEvent(...$eventData);
0 ignored issues
show
Bug introduced by
The call to BeforeCreateGlobalUrlAliasEvent::__construct() misses some required arguments starting with $path.
Loading history...
89
        if ($this->eventDispatcher->dispatch(URLAliasEvents::BEFORE_CREATE_GLOBAL_URL_ALIAS, $beforeEvent)->isPropagationStopped()) {
90
            return $beforeEvent->getUrlAlias();
91
        }
92
93
        $urlAlias = $beforeEvent->hasUrlAlias()
94
            ? $beforeEvent->getUrlAlias()
95
            : parent::createGlobalUrlAlias($resource, $path, $languageCode, $forwarding, $alwaysAvailable);
96
97
        $this->eventDispatcher->dispatch(
98
            URLAliasEvents::CREATE_GLOBAL_URL_ALIAS,
99
            new CreateGlobalUrlAliasEvent($urlAlias, ...$eventData)
0 ignored issues
show
Bug introduced by
The call to CreateGlobalUrlAliasEvent::__construct() misses some required arguments starting with $path.
Loading history...
100
        );
101
102
        return $urlAlias;
103
    }
104
105
    public function removeAliases(array $aliasList)
106
    {
107
        $eventData = [$aliasList];
108
109
        $beforeEvent = new BeforeRemoveAliasesEvent(...$eventData);
110
        if ($this->eventDispatcher->dispatch(URLAliasEvents::BEFORE_REMOVE_ALIASES, $beforeEvent)->isPropagationStopped()) {
111
            return;
112
        }
113
114
        parent::removeAliases($aliasList);
115
116
        $this->eventDispatcher->dispatch(
117
            URLAliasEvents::REMOVE_ALIASES,
118
            new RemoveAliasesEvent(...$eventData)
119
        );
120
    }
121
122
    public function refreshSystemUrlAliasesForLocation(Location $location): void
123
    {
124
        $eventData = [$location];
125
126
        $beforeEvent = new BeforeRefreshSystemUrlAliasesForLocationEvent(...$eventData);
127
        if ($this->eventDispatcher->dispatch(URLAliasEvents::BEFORE_REFRESH_SYSTEM_URL_ALIASES_FOR_LOCATION, $beforeEvent)->isPropagationStopped()) {
128
            return;
129
        }
130
131
        parent::refreshSystemUrlAliasesForLocation($location);
132
133
        $this->eventDispatcher->dispatch(
134
            URLAliasEvents::REFRESH_SYSTEM_URL_ALIASES_FOR_LOCATION,
135
            new RefreshSystemUrlAliasesForLocationEvent(...$eventData)
136
        );
137
    }
138
}
139