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

URLWildcardService::create()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 25

Duplication

Lines 25
Ratio 100 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 3
dl 25
loc 25
rs 9.52
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\URLWildcardServiceDecorator;
12
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
13
use eZ\Publish\API\Repository\URLWildcardService as URLWildcardServiceInterface;
14
use eZ\Publish\API\Repository\Values\Content\URLWildcard;
15
use eZ\Publish\Core\Event\URLWildcard\BeforeCreateEvent;
16
use eZ\Publish\Core\Event\URLWildcard\BeforeRemoveEvent;
17
use eZ\Publish\Core\Event\URLWildcard\BeforeTranslateEvent;
18
use eZ\Publish\Core\Event\URLWildcard\CreateEvent;
19
use eZ\Publish\Core\Event\URLWildcard\RemoveEvent;
20
use eZ\Publish\Core\Event\URLWildcard\TranslateEvent;
21
use eZ\Publish\Core\Event\URLWildcard\URLWildcardEvents;
22
23
class URLWildcardService extends URLWildcardServiceDecorator implements URLWildcardServiceInterface
24
{
25
    /**
26
     * @var \Symfony\Component\EventDispatcher\EventDispatcherInterface
27
     */
28
    protected $eventDispatcher;
29
30
    public function __construct(
31
        URLWildcardServiceInterface $innerService,
32
        EventDispatcherInterface $eventDispatcher
33
    ) {
34
        parent::__construct($innerService);
35
36
        $this->eventDispatcher = $eventDispatcher;
37
    }
38
39 View Code Duplication
    public function create(
40
        $sourceUrl,
41
        $destinationUrl,
42
        $forward = false
43
    ) {
44
        $eventData = [
45
            $sourceUrl,
46
            $destinationUrl,
47
            $forward,
48
        ];
49
50
        $beforeEvent = new BeforeCreateEvent(...$eventData);
0 ignored issues
show
Bug introduced by
The call to BeforeCreateEvent::__construct() misses some required arguments starting with $destinationUrl.
Loading history...
51
        if ($this->eventDispatcher->dispatch(URLWildcardEvents::BEFORE_CREATE, $beforeEvent)->isPropagationStopped()) {
52
            return $beforeEvent->getReturnValue();
53
        } else {
54
            $urlWildcard = parent::create($sourceUrl, $destinationUrl, $forward);
55
        }
56
57
        $this->eventDispatcher->dispatch(
58
            URLWildcardEvents::CREATE,
59
            new CreateEvent($urlWildcard, ...$eventData)
0 ignored issues
show
Bug introduced by
The call to CreateEvent::__construct() misses some required arguments starting with $destinationUrl.
Loading history...
Bug introduced by
It seems like $urlWildcard defined by parent::create($sourceUr...stinationUrl, $forward) on line 54 can be null; however, eZ\Publish\Core\Event\UR...ateEvent::__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...
60
        );
61
62
        return $urlWildcard;
63
    }
64
65
    public function remove(URLWildcard $urlWildcard)
66
    {
67
        $eventData = [$urlWildcard];
68
69
        $beforeEvent = new BeforeRemoveEvent(...$eventData);
70
        if ($this->eventDispatcher->dispatch(URLWildcardEvents::BEFORE_REMOVE, $beforeEvent)->isPropagationStopped()) {
71
            return;
72
        } else {
73
            parent::remove($urlWildcard);
74
        }
75
76
        $this->eventDispatcher->dispatch(
77
            URLWildcardEvents::REMOVE,
78
            new RemoveEvent(...$eventData)
79
        );
80
    }
81
82 View Code Duplication
    public function translate($url)
83
    {
84
        $eventData = [$url];
85
86
        $beforeEvent = new BeforeTranslateEvent(...$eventData);
87
        if ($this->eventDispatcher->dispatch(URLWildcardEvents::BEFORE_TRANSLATE, $beforeEvent)->isPropagationStopped()) {
88
            return $beforeEvent->getReturnValue();
89
        } else {
90
            $result = parent::translate($url);
91
        }
92
93
        $this->eventDispatcher->dispatch(
94
            URLWildcardEvents::TRANSLATE,
95
            new TranslateEvent($result, ...$eventData)
0 ignored issues
show
Bug introduced by
It seems like $result defined by parent::translate($url) on line 90 can be null; however, eZ\Publish\Core\Event\UR...ateEvent::__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 $result;
99
    }
100
}
101