Completed
Push — signal-slots ( f9cce0...3c05c8 )
by
unknown
14:14
created

URLAliasService::removeAliases()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 1
dl 0
loc 16
rs 9.7333
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
    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 View Code Duplication
        if ($this->eventDispatcher->dispatch(URLAliasEvents::BEFORE_CREATE_URL_ALIAS, $beforeEvent)->isPropagationStopped()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
58
            return $beforeEvent->getReturnValue();
59
        } else {
60
            $urlAlias = parent::createUrlAlias($location, $path, $languageCode, $forwarding, $alwaysAvailable);
61
        }
62
63
        $this->eventDispatcher->dispatch(
64
            URLAliasEvents::CREATE_URL_ALIAS,
65
            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...
Bug introduced by
It seems like $urlAlias defined by parent::createUrlAlias($...ding, $alwaysAvailable) on line 60 can be null; however, eZ\Publish\Core\Event\UR...iasEvent::__construct() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
66
        );
67
68
        return $urlAlias;
69
    }
70
71
    public function createGlobalUrlAlias(
72
        $resource,
73
        $path,
74
        $languageCode,
75
        $forwarding = false,
76
        $alwaysAvailable = false
77
    ) {
78
        $eventData = [
79
            $resource,
80
            $path,
81
            $languageCode,
82
            $forwarding,
83
            $alwaysAvailable,
84
        ];
85
86
        $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...
87 View Code Duplication
        if ($this->eventDispatcher->dispatch(URLAliasEvents::BEFORE_CREATE_GLOBAL_URL_ALIAS, $beforeEvent)->isPropagationStopped()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
88
            return $beforeEvent->getReturnValue();
89
        } else {
90
            $urlAlias = parent::createGlobalUrlAlias($resource, $path, $languageCode, $forwarding, $alwaysAvailable);
91
        }
92
93
        $this->eventDispatcher->dispatch(
94
            URLAliasEvents::CREATE_GLOBAL_URL_ALIAS,
95
            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...
Bug introduced by
It seems like $urlAlias defined by parent::createGlobalUrlA...ding, $alwaysAvailable) on line 90 can be null; however, eZ\Publish\Core\Event\UR...iasEvent::__construct() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
96
        );
97
98
        return $urlAlias;
99
    }
100
101
    public function removeAliases(array $aliasList)
102
    {
103
        $eventData = [$aliasList];
104
105
        $beforeEvent = new BeforeRemoveAliasesEvent(...$eventData);
106
        if ($this->eventDispatcher->dispatch(URLAliasEvents::BEFORE_REMOVE_ALIASES, $beforeEvent)->isPropagationStopped()) {
107
            return;
108
        } else {
109
            parent::removeAliases($aliasList);
110
        }
111
112
        $this->eventDispatcher->dispatch(
113
            URLAliasEvents::REMOVE_ALIASES,
114
            new RemoveAliasesEvent(...$eventData)
115
        );
116
    }
117
118
    public function refreshSystemUrlAliasesForLocation(Location $location): void
119
    {
120
        $eventData = [$location];
121
122
        $beforeEvent = new BeforeRefreshSystemUrlAliasesForLocationEvent(...$eventData);
123
        if ($this->eventDispatcher->dispatch(URLAliasEvents::BEFORE_REFRESH_SYSTEM_URL_ALIASES_FOR_LOCATION, $beforeEvent)->isPropagationStopped()) {
124
            return;
125
        } else {
126
            parent::refreshSystemUrlAliasesForLocation($location);
127
        }
128
129
        $this->eventDispatcher->dispatch(
130
            URLAliasEvents::REFRESH_SYSTEM_URL_ALIASES_FOR_LOCATION,
131
            new RefreshSystemUrlAliasesForLocationEvent(...$eventData)
132
        );
133
    }
134
}
135