Completed
Push — ezp-30616 ( 0a36b6 )
by
unknown
21:15 queued 06:15
created

LocationService::moveSubtree()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 21

Duplication

Lines 21
Ratio 100 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 2
dl 21
loc 21
rs 9.584
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\LocationServiceDecorator;
12
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
13
use eZ\Publish\API\Repository\LocationService as LocationServiceInterface;
14
use eZ\Publish\API\Repository\Values\Content\ContentInfo;
15
use eZ\Publish\API\Repository\Values\Content\Location;
16
use eZ\Publish\API\Repository\Values\Content\LocationCreateStruct;
17
use eZ\Publish\API\Repository\Values\Content\LocationUpdateStruct;
18
use eZ\Publish\Core\Event\Location\BeforeCopySubtreeEvent;
19
use eZ\Publish\Core\Event\Location\BeforeCreateLocationEvent;
20
use eZ\Publish\Core\Event\Location\BeforeDeleteLocationEvent;
21
use eZ\Publish\Core\Event\Location\BeforeHideLocationEvent;
22
use eZ\Publish\Core\Event\Location\BeforeMoveSubtreeEvent;
23
use eZ\Publish\Core\Event\Location\BeforeSwapLocationEvent;
24
use eZ\Publish\Core\Event\Location\BeforeUnhideLocationEvent;
25
use eZ\Publish\Core\Event\Location\BeforeUpdateLocationEvent;
26
use eZ\Publish\Core\Event\Location\CopySubtreeEvent;
27
use eZ\Publish\Core\Event\Location\CreateLocationEvent;
28
use eZ\Publish\Core\Event\Location\DeleteLocationEvent;
29
use eZ\Publish\Core\Event\Location\HideLocationEvent;
30
use eZ\Publish\Core\Event\Location\LocationEvents;
31
use eZ\Publish\Core\Event\Location\MoveSubtreeEvent;
32
use eZ\Publish\Core\Event\Location\SwapLocationEvent;
33
use eZ\Publish\Core\Event\Location\UnhideLocationEvent;
34
use eZ\Publish\Core\Event\Location\UpdateLocationEvent;
35
36
class LocationService extends LocationServiceDecorator implements LocationServiceInterface
37
{
38
    /**
39
     * @var \Symfony\Component\EventDispatcher\EventDispatcherInterface
40
     */
41
    protected $eventDispatcher;
42
43
    public function __construct(
44
        LocationServiceInterface $innerService,
45
        EventDispatcherInterface $eventDispatcher
46
    ) {
47
        parent::__construct($innerService);
48
49
        $this->eventDispatcher = $eventDispatcher;
50
    }
51
52
    public function copySubtree(
53
        Location $subtree,
54
        Location $targetParentLocation
55
    ): Location {
56
        $eventData = [
57
            $subtree,
58
            $targetParentLocation,
59
        ];
60
61
        $beforeEvent = new BeforeCopySubtreeEvent(...$eventData);
0 ignored issues
show
Bug introduced by
The call to BeforeCopySubtreeEvent::__construct() misses a required argument $targetParentLocation.

This check looks for function calls that miss required arguments.

Loading history...
62
        if ($this->eventDispatcher->dispatch(LocationEvents::BEFORE_COPY_SUBTREE, $beforeEvent)->isPropagationStopped()) {
63
            return $beforeEvent->getReturnValue();
64
        } else {
65
            $location = parent::copySubtree($subtree, $targetParentLocation);
66
        }
67
68
        $this->eventDispatcher->dispatch(
69
            LocationEvents::COPY_SUBTREE,
70
            new CopySubtreeEvent($location, ...$eventData)
0 ignored issues
show
Bug introduced by
The call to CopySubtreeEvent::__construct() misses a required argument $targetParentLocation.

This check looks for function calls that miss required arguments.

Loading history...
Bug introduced by
It seems like $location defined by parent::copySubtree($sub... $targetParentLocation) on line 65 can be null; however, eZ\Publish\Core\Event\Lo...reeEvent::__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...
71
        );
72
73
        return $location;
74
    }
75
76
    public function createLocation(
77
        ContentInfo $contentInfo,
78
        LocationCreateStruct $locationCreateStruct
79
    ): Location {
80
        $eventData = [
81
            $contentInfo,
82
            $locationCreateStruct,
83
        ];
84
85
        $beforeEvent = new BeforeCreateLocationEvent(...$eventData);
0 ignored issues
show
Bug introduced by
The call to BeforeCreateLocationEvent::__construct() misses a required argument $locationCreateStruct.

This check looks for function calls that miss required arguments.

Loading history...
86
        if ($this->eventDispatcher->dispatch(LocationEvents::BEFORE_CREATE_LOCATION, $beforeEvent)->isPropagationStopped()) {
87
            return $beforeEvent->getReturnValue();
88
        } else {
89
            $location = parent::createLocation($contentInfo, $locationCreateStruct);
90
        }
91
92
        $this->eventDispatcher->dispatch(
93
            LocationEvents::CREATE_LOCATION,
94
            new CreateLocationEvent($location, ...$eventData)
0 ignored issues
show
Bug introduced by
The call to CreateLocationEvent::__construct() misses a required argument $locationCreateStruct.

This check looks for function calls that miss required arguments.

Loading history...
Bug introduced by
It seems like $location defined by parent::createLocation($... $locationCreateStruct) on line 89 can be null; however, eZ\Publish\Core\Event\Lo...ionEvent::__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...
95
        );
96
97
        return $location;
98
    }
99
100
    public function updateLocation(
101
        Location $location,
102
        LocationUpdateStruct $locationUpdateStruct
103
    ): Location {
104
        $eventData = [
105
            $location,
106
            $locationUpdateStruct,
107
        ];
108
109
        $beforeEvent = new BeforeUpdateLocationEvent(...$eventData);
0 ignored issues
show
Bug introduced by
The call to BeforeUpdateLocationEvent::__construct() misses a required argument $locationUpdateStruct.

This check looks for function calls that miss required arguments.

Loading history...
110
        if ($this->eventDispatcher->dispatch(LocationEvents::BEFORE_UPDATE_LOCATION, $beforeEvent)->isPropagationStopped()) {
111
            return $beforeEvent->getReturnValue();
112
        } else {
113
            $updatedLocation = parent::updateLocation($location, $locationUpdateStruct);
114
        }
115
116
        $this->eventDispatcher->dispatch(
117
            LocationEvents::UPDATE_LOCATION,
118
            new UpdateLocationEvent($updatedLocation, ...$eventData)
0 ignored issues
show
Bug introduced by
The call to UpdateLocationEvent::__construct() misses a required argument $locationUpdateStruct.

This check looks for function calls that miss required arguments.

Loading history...
Bug introduced by
It seems like $updatedLocation defined by parent::updateLocation($... $locationUpdateStruct) on line 113 can be null; however, eZ\Publish\Core\Event\Lo...ionEvent::__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...
119
        );
120
121
        return $updatedLocation;
122
    }
123
124 View Code Duplication
    public function swapLocation(
125
        Location $location1,
126
        Location $location2
127
    ): void {
128
        $eventData = [
129
            $location1,
130
            $location2,
131
        ];
132
133
        $beforeEvent = new BeforeSwapLocationEvent(...$eventData);
0 ignored issues
show
Bug introduced by
The call to BeforeSwapLocationEvent::__construct() misses a required argument $location2.

This check looks for function calls that miss required arguments.

Loading history...
134
        if ($this->eventDispatcher->dispatch(LocationEvents::BEFORE_SWAP_LOCATION, $beforeEvent)->isPropagationStopped()) {
135
            return $beforeEvent->getReturnValue();
136
        } else {
137
            parent::swapLocation($location1, $location2);
138
        }
139
140
        $this->eventDispatcher->dispatch(
141
            LocationEvents::SWAP_LOCATION,
142
            new SwapLocationEvent(...$eventData)
0 ignored issues
show
Bug introduced by
The call to SwapLocationEvent::__construct() misses a required argument $location2.

This check looks for function calls that miss required arguments.

Loading history...
143
        );
144
    }
145
146 View Code Duplication
    public function hideLocation(Location $location): Location
147
    {
148
        $eventData = [$location];
149
150
        $beforeEvent = new BeforeHideLocationEvent(...$eventData);
151
        if ($this->eventDispatcher->dispatch(LocationEvents::BEFORE_HIDE_LOCATION, $beforeEvent)->isPropagationStopped()) {
152
            return $beforeEvent->getReturnValue();
153
        } else {
154
            $hiddenLocation = parent::hideLocation($location);
155
        }
156
157
        $this->eventDispatcher->dispatch(
158
            LocationEvents::HIDE_LOCATION,
159
            new HideLocationEvent($hiddenLocation, ...$eventData)
0 ignored issues
show
Bug introduced by
It seems like $hiddenLocation defined by parent::hideLocation($location) on line 154 can be null; however, eZ\Publish\Core\Event\Lo...ionEvent::__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...
160
        );
161
162
        return $hiddenLocation;
163
    }
164
165 View Code Duplication
    public function unhideLocation(Location $location): Location
166
    {
167
        $eventData = [$location];
168
169
        $beforeEvent = new BeforeUnhideLocationEvent(...$eventData);
170
        if ($this->eventDispatcher->dispatch(LocationEvents::BEFORE_UNHIDE_LOCATION, $beforeEvent)->isPropagationStopped()) {
171
            return $beforeEvent->getReturnValue();
172
        } else {
173
            $revealedLocation = parent::unhideLocation($location);
174
        }
175
176
        $this->eventDispatcher->dispatch(
177
            LocationEvents::UNHIDE_LOCATION,
178
            new UnhideLocationEvent($revealedLocation, ...$eventData)
0 ignored issues
show
Bug introduced by
It seems like $revealedLocation defined by parent::unhideLocation($location) on line 173 can be null; however, eZ\Publish\Core\Event\Lo...ionEvent::__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...
179
        );
180
181
        return $revealedLocation;
182
    }
183
184 View Code Duplication
    public function moveSubtree(
185
        Location $location,
186
        Location $newParentLocation
187
    ): void {
188
        $eventData = [
189
            $location,
190
            $newParentLocation,
191
        ];
192
193
        $beforeEvent = new BeforeMoveSubtreeEvent(...$eventData);
0 ignored issues
show
Bug introduced by
The call to BeforeMoveSubtreeEvent::__construct() misses a required argument $newParentLocation.

This check looks for function calls that miss required arguments.

Loading history...
194
        if ($this->eventDispatcher->dispatch(LocationEvents::BEFORE_MOVE_SUBTREE, $beforeEvent)->isPropagationStopped()) {
195
            return;
196
        } else {
197
            parent::moveSubtree($location, $newParentLocation);
198
        }
199
200
        $this->eventDispatcher->dispatch(
201
            LocationEvents::MOVE_SUBTREE,
202
            new MoveSubtreeEvent(...$eventData)
0 ignored issues
show
Bug introduced by
The call to MoveSubtreeEvent::__construct() misses a required argument $newParentLocation.

This check looks for function calls that miss required arguments.

Loading history...
203
        );
204
    }
205
206
    public function deleteLocation(Location $location): void
207
    {
208
        $eventData = [$location];
209
210
        $beforeEvent = new BeforeDeleteLocationEvent(...$eventData);
211
        if ($this->eventDispatcher->dispatch(LocationEvents::BEFORE_DELETE_LOCATION, $beforeEvent)->isPropagationStopped()) {
212
            return $beforeEvent->getReturnValue();
213
        } else {
214
            parent::deleteLocation($location);
215
        }
216
217
        $this->eventDispatcher->dispatch(
218
            LocationEvents::DELETE_LOCATION,
219
            new DeleteLocationEvent(...$eventData)
220
        );
221
    }
222
}
223