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

URLWildcardService::create()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 3
nop 3
dl 0
loc 27
rs 9.488
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
    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->getUrlWildcard();
53
        }
54
55
        $urlWildcard = $beforeEvent->hasUrlWildcard()
56
            ? $beforeEvent->getUrlWildcard()
57
            : parent::create($sourceUrl, $destinationUrl, $forward);
58
59
        $this->eventDispatcher->dispatch(
60
            URLWildcardEvents::CREATE,
61
            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...
62
        );
63
64
        return $urlWildcard;
65
    }
66
67
    public function remove(URLWildcard $urlWildcard)
68
    {
69
        $eventData = [$urlWildcard];
70
71
        $beforeEvent = new BeforeRemoveEvent(...$eventData);
72
        if ($this->eventDispatcher->dispatch(URLWildcardEvents::BEFORE_REMOVE, $beforeEvent)->isPropagationStopped()) {
73
            return;
74
        }
75
76
        parent::remove($urlWildcard);
77
78
        $this->eventDispatcher->dispatch(
79
            URLWildcardEvents::REMOVE,
80
            new RemoveEvent(...$eventData)
81
        );
82
    }
83
84 View Code Duplication
    public function translate($url)
85
    {
86
        $eventData = [$url];
87
88
        $beforeEvent = new BeforeTranslateEvent(...$eventData);
89
        if ($this->eventDispatcher->dispatch(URLWildcardEvents::BEFORE_TRANSLATE, $beforeEvent)->isPropagationStopped()) {
90
            return $beforeEvent->getResult();
91
        }
92
93
        $result = $beforeEvent->hasResult()
94
            ? $beforeEvent->getResult()
95
            : parent::translate($url);
96
97
        $this->eventDispatcher->dispatch(
98
            URLWildcardEvents::TRANSLATE,
99
            new TranslateEvent($result, ...$eventData)
100
        );
101
102
        return $result;
103
    }
104
}
105