Completed
Push — ezp-30616-follow-up ( 2e0607...b74676 )
by
unknown
35:08 queued 20:12
created

URLAliasService   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 120
Duplicated Lines 55 %

Coupling/Cohesion

Components 1
Dependencies 11

Importance

Changes 0
Metric Value
dl 66
loc 120
rs 10
c 0
b 0
f 0
wmc 11
lcom 1
cbo 11

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A createUrlAlias() 33 33 3
A createGlobalUrlAlias() 33 33 3
A removeAliases() 0 18 2
A refreshSystemUrlAliasesForLocation() 0 18 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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\API\Repository\Events\URLAlias\BeforeCreateGlobalUrlAliasEvent as BeforeCreateGlobalUrlAliasEventInterface;
12
use eZ\Publish\API\Repository\Events\URLAlias\BeforeCreateUrlAliasEvent as BeforeCreateUrlAliasEventInterface;
13
use eZ\Publish\API\Repository\Events\URLAlias\BeforeRefreshSystemUrlAliasesForLocationEvent as BeforeRefreshSystemUrlAliasesForLocationEventInterface;
14
use eZ\Publish\API\Repository\Events\URLAlias\BeforeRemoveAliasesEvent as BeforeRemoveAliasesEventInterface;
15
use eZ\Publish\API\Repository\Events\URLAlias\CreateGlobalUrlAliasEvent as CreateGlobalUrlAliasEventInterface;
16
use eZ\Publish\API\Repository\Events\URLAlias\CreateUrlAliasEvent as CreateUrlAliasEventInterface;
17
use eZ\Publish\API\Repository\Events\URLAlias\RefreshSystemUrlAliasesForLocationEvent as RefreshSystemUrlAliasesForLocationEventInterface;
18
use eZ\Publish\API\Repository\Events\URLAlias\RemoveAliasesEvent as RemoveAliasesEventInterface;
19
use eZ\Publish\API\Repository\URLAliasService as URLAliasServiceInterface;
20
use eZ\Publish\API\Repository\Values\Content\Location;
21
use eZ\Publish\API\Repository\Events\URLAlias\BeforeCreateGlobalUrlAliasEvent;
22
use eZ\Publish\API\Repository\Events\URLAlias\BeforeCreateUrlAliasEvent;
23
use eZ\Publish\API\Repository\Events\URLAlias\BeforeRefreshSystemUrlAliasesForLocationEvent;
24
use eZ\Publish\API\Repository\Events\URLAlias\BeforeRemoveAliasesEvent;
25
use eZ\Publish\API\Repository\Events\URLAlias\CreateGlobalUrlAliasEvent;
26
use eZ\Publish\API\Repository\Events\URLAlias\CreateUrlAliasEvent;
27
use eZ\Publish\API\Repository\Events\URLAlias\RefreshSystemUrlAliasesForLocationEvent;
28
use eZ\Publish\API\Repository\Events\URLAlias\RemoveAliasesEvent;
29
use eZ\Publish\SPI\Repository\Decorator\URLAliasServiceDecorator;
30
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
31
32
class URLAliasService extends URLAliasServiceDecorator
33
{
34
    /** @var \Symfony\Contracts\EventDispatcher\EventDispatcherInterface */
35
    protected $eventDispatcher;
36
37
    public function __construct(
38
        URLAliasServiceInterface $innerService,
39
        EventDispatcherInterface $eventDispatcher
40
    ) {
41
        parent::__construct($innerService);
42
43
        $this->eventDispatcher = $eventDispatcher;
44
    }
45
46 View Code Duplication
    public function createUrlAlias(
47
        Location $location,
48
        $path,
49
        $languageCode,
50
        $forwarding = false,
51
        $alwaysAvailable = false
52
    ) {
53
        $eventData = [
54
            $location,
55
            $path,
56
            $languageCode,
57
            $forwarding,
58
            $alwaysAvailable,
59
        ];
60
61
        $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...
62
63
        $this->eventDispatcher->dispatch($beforeEvent, BeforeCreateUrlAliasEventInterface::class);
64
        if ($beforeEvent->isPropagationStopped()) {
65
            return $beforeEvent->getUrlAlias();
66
        }
67
68
        $urlAlias = $beforeEvent->hasUrlAlias()
69
            ? $beforeEvent->getUrlAlias()
70
            : $this->innerService->createUrlAlias($location, $path, $languageCode, $forwarding, $alwaysAvailable);
71
72
        $this->eventDispatcher->dispatch(
73
            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...
74
            CreateUrlAliasEventInterface::class
75
        );
76
77
        return $urlAlias;
78
    }
79
80 View Code Duplication
    public function createGlobalUrlAlias(
81
        $resource,
82
        $path,
83
        $languageCode,
84
        $forwarding = false,
85
        $alwaysAvailable = false
86
    ) {
87
        $eventData = [
88
            $resource,
89
            $path,
90
            $languageCode,
91
            $forwarding,
92
            $alwaysAvailable,
93
        ];
94
95
        $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...
96
97
        $this->eventDispatcher->dispatch($beforeEvent, BeforeCreateGlobalUrlAliasEventInterface::class);
98
        if ($beforeEvent->isPropagationStopped()) {
99
            return $beforeEvent->getUrlAlias();
100
        }
101
102
        $urlAlias = $beforeEvent->hasUrlAlias()
103
            ? $beforeEvent->getUrlAlias()
104
            : $this->innerService->createGlobalUrlAlias($resource, $path, $languageCode, $forwarding, $alwaysAvailable);
105
106
        $this->eventDispatcher->dispatch(
107
            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...
108
            CreateGlobalUrlAliasEventInterface::class
109
        );
110
111
        return $urlAlias;
112
    }
113
114
    public function removeAliases(array $aliasList): void
115
    {
116
        $eventData = [$aliasList];
117
118
        $beforeEvent = new BeforeRemoveAliasesEvent(...$eventData);
119
120
        $this->eventDispatcher->dispatch($beforeEvent, BeforeRemoveAliasesEventInterface::class);
121
        if ($beforeEvent->isPropagationStopped()) {
122
            return;
123
        }
124
125
        $this->innerService->removeAliases($aliasList);
126
127
        $this->eventDispatcher->dispatch(
128
            new RemoveAliasesEvent(...$eventData),
129
            RemoveAliasesEventInterface::class
130
        );
131
    }
132
133
    public function refreshSystemUrlAliasesForLocation(Location $location): void
134
    {
135
        $eventData = [$location];
136
137
        $beforeEvent = new BeforeRefreshSystemUrlAliasesForLocationEvent(...$eventData);
138
139
        $this->eventDispatcher->dispatch($beforeEvent, BeforeRefreshSystemUrlAliasesForLocationEventInterface::class);
140
        if ($beforeEvent->isPropagationStopped()) {
141
            return;
142
        }
143
144
        $this->innerService->refreshSystemUrlAliasesForLocation($location);
145
146
        $this->eventDispatcher->dispatch(
147
            new RefreshSystemUrlAliasesForLocationEvent(...$eventData),
148
            RefreshSystemUrlAliasesForLocationEventInterface::class
149
        );
150
    }
151
}
152