Completed
Push — ezp-30616-follow-up ( de1597...f1c1e1 )
by
unknown
14:34
created

SectionService::deleteSection()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 1
dl 0
loc 18
rs 9.6666
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\API\Repository\Events;
10
11
use eZ\Publish\API\Repository\Events\Section\AssignSectionEvent as AssignSectionEventInterface;
12
use eZ\Publish\API\Repository\Events\Section\AssignSectionToSubtreeEvent as AssignSectionToSubtreeEventInterface;
13
use eZ\Publish\API\Repository\Events\Section\BeforeAssignSectionEvent as BeforeAssignSectionEventInterface;
14
use eZ\Publish\API\Repository\Events\Section\BeforeAssignSectionToSubtreeEvent as BeforeAssignSectionToSubtreeEventInterface;
15
use eZ\Publish\API\Repository\Events\Section\BeforeCreateSectionEvent as BeforeCreateSectionEventInterface;
16
use eZ\Publish\API\Repository\Events\Section\BeforeDeleteSectionEvent as BeforeDeleteSectionEventInterface;
17
use eZ\Publish\API\Repository\Events\Section\BeforeUpdateSectionEvent as BeforeUpdateSectionEventInterface;
18
use eZ\Publish\API\Repository\Events\Section\CreateSectionEvent as CreateSectionEventInterface;
19
use eZ\Publish\API\Repository\Events\Section\DeleteSectionEvent as DeleteSectionEventInterface;
20
use eZ\Publish\API\Repository\Events\Section\UpdateSectionEvent as UpdateSectionEventInterface;
21
use eZ\Publish\API\Repository\SectionService as SectionServiceInterface;
22
use eZ\Publish\API\Repository\Values\Content\ContentInfo;
23
use eZ\Publish\API\Repository\Values\Content\Location;
24
use eZ\Publish\API\Repository\Values\Content\Section;
25
use eZ\Publish\API\Repository\Values\Content\SectionCreateStruct;
26
use eZ\Publish\API\Repository\Values\Content\SectionUpdateStruct;
27
use eZ\Publish\API\Repository\Events\Section\AssignSectionEvent;
28
use eZ\Publish\API\Repository\Events\Section\AssignSectionToSubtreeEvent;
29
use eZ\Publish\API\Repository\Events\Section\BeforeAssignSectionEvent;
30
use eZ\Publish\API\Repository\Events\Section\BeforeAssignSectionToSubtreeEvent;
31
use eZ\Publish\API\Repository\Events\Section\BeforeCreateSectionEvent;
32
use eZ\Publish\API\Repository\Events\Section\BeforeDeleteSectionEvent;
33
use eZ\Publish\API\Repository\Events\Section\BeforeUpdateSectionEvent;
34
use eZ\Publish\API\Repository\Events\Section\CreateSectionEvent;
35
use eZ\Publish\API\Repository\Events\Section\DeleteSectionEvent;
36
use eZ\Publish\API\Repository\Events\Section\UpdateSectionEvent;
37
use eZ\Publish\SPI\Repository\Decorator\SectionServiceDecorator;
38
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
39
40
class SectionService extends SectionServiceDecorator
41
{
42
    /** @var \Symfony\Contracts\EventDispatcher\EventDispatcherInterface */
43
    protected $eventDispatcher;
44
45
    public function __construct(
46
        SectionServiceInterface $innerService,
47
        EventDispatcherInterface $eventDispatcher
48
    ) {
49
        parent::__construct($innerService);
50
51
        $this->eventDispatcher = $eventDispatcher;
52
    }
53
54
    public function createSection(SectionCreateStruct $sectionCreateStruct)
55
    {
56
        $eventData = [$sectionCreateStruct];
57
58
        $beforeEvent = new BeforeCreateSectionEvent(...$eventData);
59
60
        $this->eventDispatcher->dispatch($beforeEvent, BeforeCreateSectionEventInterface::class);
61
        if ($beforeEvent->isPropagationStopped()) {
62
            return $beforeEvent->getSection();
63
        }
64
65
        $section = $beforeEvent->hasSection()
66
            ? $beforeEvent->getSection()
67
            : $this->innerService->createSection($sectionCreateStruct);
68
69
        $this->eventDispatcher->dispatch(
70
            new CreateSectionEvent($section, ...$eventData),
71
            CreateSectionEventInterface::class
72
        );
73
74
        return $section;
75
    }
76
77
    public function updateSection(
78
        Section $section,
79
        SectionUpdateStruct $sectionUpdateStruct
80
    ) {
81
        $eventData = [
82
            $section,
83
            $sectionUpdateStruct,
84
        ];
85
86
        $beforeEvent = new BeforeUpdateSectionEvent(...$eventData);
0 ignored issues
show
Bug introduced by
The call to BeforeUpdateSectionEvent::__construct() misses a required argument $sectionUpdateStruct.

This check looks for function calls that miss required arguments.

Loading history...
87
88
        $this->eventDispatcher->dispatch($beforeEvent, BeforeUpdateSectionEventInterface::class);
89
        if ($beforeEvent->isPropagationStopped()) {
90
            return $beforeEvent->getUpdatedSection();
91
        }
92
93
        $updatedSection = $beforeEvent->hasUpdatedSection()
94
            ? $beforeEvent->getUpdatedSection()
95
            : $this->innerService->updateSection($section, $sectionUpdateStruct);
96
97
        $this->eventDispatcher->dispatch(
98
            new UpdateSectionEvent($updatedSection, ...$eventData),
0 ignored issues
show
Bug introduced by
The call to UpdateSectionEvent::__construct() misses a required argument $sectionUpdateStruct.

This check looks for function calls that miss required arguments.

Loading history...
99
            UpdateSectionEventInterface::class
100
        );
101
102
        return $updatedSection;
103
    }
104
105 View Code Duplication
    public function assignSection(
106
        ContentInfo $contentInfo,
107
        Section $section
108
    ): void {
109
        $eventData = [
110
            $contentInfo,
111
            $section,
112
        ];
113
114
        $beforeEvent = new BeforeAssignSectionEvent(...$eventData);
0 ignored issues
show
Bug introduced by
The call to BeforeAssignSectionEvent::__construct() misses a required argument $section.

This check looks for function calls that miss required arguments.

Loading history...
115
116
        $this->eventDispatcher->dispatch($beforeEvent, BeforeAssignSectionEventInterface::class);
117
        if ($beforeEvent->isPropagationStopped()) {
118
            return;
119
        }
120
121
        $this->innerService->assignSection($contentInfo, $section);
122
123
        $this->eventDispatcher->dispatch(
124
            new AssignSectionEvent(...$eventData),
0 ignored issues
show
Bug introduced by
The call to AssignSectionEvent::__construct() misses a required argument $section.

This check looks for function calls that miss required arguments.

Loading history...
125
            AssignSectionEventInterface::class
126
        );
127
    }
128
129 View Code Duplication
    public function assignSectionToSubtree(
130
        Location $location,
131
        Section $section
132
    ): void {
133
        $eventData = [
134
            $location,
135
            $section,
136
        ];
137
138
        $beforeEvent = new BeforeAssignSectionToSubtreeEvent(...$eventData);
0 ignored issues
show
Bug introduced by
The call to BeforeAssignSectionToSubtreeEvent::__construct() misses a required argument $section.

This check looks for function calls that miss required arguments.

Loading history...
139
140
        $this->eventDispatcher->dispatch($beforeEvent, BeforeAssignSectionToSubtreeEventInterface::class);
141
        if ($beforeEvent->isPropagationStopped()) {
142
            return;
143
        }
144
145
        $this->innerService->assignSectionToSubtree($location, $section);
146
147
        $this->eventDispatcher->dispatch(
148
            new AssignSectionToSubtreeEvent(...$eventData),
0 ignored issues
show
Bug introduced by
The call to AssignSectionToSubtreeEvent::__construct() misses a required argument $section.

This check looks for function calls that miss required arguments.

Loading history...
149
            AssignSectionToSubtreeEventInterface::class
150
        );
151
    }
152
153
    public function deleteSection(Section $section): void
154
    {
155
        $eventData = [$section];
156
157
        $beforeEvent = new BeforeDeleteSectionEvent(...$eventData);
158
159
        $this->eventDispatcher->dispatch($beforeEvent, BeforeDeleteSectionEventInterface::class);
160
        if ($beforeEvent->isPropagationStopped()) {
161
            return;
162
        }
163
164
        $this->innerService->deleteSection($section);
165
166
        $this->eventDispatcher->dispatch(
167
            new DeleteSectionEvent(...$eventData),
168
            DeleteSectionEventInterface::class
169
        );
170
    }
171
}
172