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

SectionService::updateSection()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 3
nop 2
dl 0
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\SectionServiceDecorator;
12
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
13
use eZ\Publish\API\Repository\SectionService as SectionServiceInterface;
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\Section;
17
use eZ\Publish\API\Repository\Values\Content\SectionCreateStruct;
18
use eZ\Publish\API\Repository\Values\Content\SectionUpdateStruct;
19
use eZ\Publish\Core\Event\Section\AssignSectionEvent;
20
use eZ\Publish\Core\Event\Section\AssignSectionToSubtreeEvent;
21
use eZ\Publish\Core\Event\Section\BeforeAssignSectionEvent;
22
use eZ\Publish\Core\Event\Section\BeforeAssignSectionToSubtreeEvent;
23
use eZ\Publish\Core\Event\Section\BeforeCreateSectionEvent;
24
use eZ\Publish\Core\Event\Section\BeforeDeleteSectionEvent;
25
use eZ\Publish\Core\Event\Section\BeforeUpdateSectionEvent;
26
use eZ\Publish\Core\Event\Section\CreateSectionEvent;
27
use eZ\Publish\Core\Event\Section\DeleteSectionEvent;
28
use eZ\Publish\Core\Event\Section\SectionEvents;
29
use eZ\Publish\Core\Event\Section\UpdateSectionEvent;
30
31
class SectionService extends SectionServiceDecorator implements SectionServiceInterface
32
{
33
    /**
34
     * @var \Symfony\Component\EventDispatcher\EventDispatcherInterface
35
     */
36
    protected $eventDispatcher;
37
38
    public function __construct(
39
        SectionServiceInterface $innerService,
40
        EventDispatcherInterface $eventDispatcher
41
    ) {
42
        parent::__construct($innerService);
43
44
        $this->eventDispatcher = $eventDispatcher;
45
    }
46
47
    public function createSection(SectionCreateStruct $sectionCreateStruct)
48
    {
49
        $eventData = [$sectionCreateStruct];
50
51
        $beforeEvent = new BeforeCreateSectionEvent(...$eventData);
52
        if ($this->eventDispatcher->dispatch(SectionEvents::BEFORE_CREATE_SECTION, $beforeEvent)->isPropagationStopped()) {
53
            return $beforeEvent->getSection();
54
        }
55
56
        $section = $beforeEvent->hasSection()
57
            ? $beforeEvent->getSection()
58
            : parent::createSection($sectionCreateStruct);
59
60
        $this->eventDispatcher->dispatch(
61
            SectionEvents::CREATE_SECTION,
62
            new CreateSectionEvent($section, ...$eventData)
63
        );
64
65
        return $section;
66
    }
67
68
    public function updateSection(
69
        Section $section,
70
        SectionUpdateStruct $sectionUpdateStruct
71
    ) {
72
        $eventData = [
73
            $section,
74
            $sectionUpdateStruct,
75
        ];
76
77
        $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...
78
        if ($this->eventDispatcher->dispatch(SectionEvents::BEFORE_UPDATE_SECTION, $beforeEvent)->isPropagationStopped()) {
79
            return $beforeEvent->getUpdatedSection();
80
        }
81
82
        $updatedSection = $beforeEvent->hasUpdatedSection()
83
            ? $beforeEvent->getUpdatedSection()
84
            : parent::updateSection($section, $sectionUpdateStruct);
85
86
        $this->eventDispatcher->dispatch(
87
            SectionEvents::UPDATE_SECTION,
88
            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...
89
        );
90
91
        return $updatedSection;
92
    }
93
94
    public function assignSection(
95
        ContentInfo $contentInfo,
96
        Section $section
97
    ) {
98
        $eventData = [
99
            $contentInfo,
100
            $section,
101
        ];
102
103
        $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...
104
        if ($this->eventDispatcher->dispatch(SectionEvents::BEFORE_ASSIGN_SECTION, $beforeEvent)->isPropagationStopped()) {
105
            return;
106
        }
107
108
        parent::assignSection($contentInfo, $section);
109
110
        $this->eventDispatcher->dispatch(
111
            SectionEvents::ASSIGN_SECTION,
112
            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...
113
        );
114
    }
115
116 View Code Duplication
    public function assignSectionToSubtree(
117
        Location $location,
118
        Section $section
119
    ): void {
120
        $eventData = [
121
            $location,
122
            $section,
123
        ];
124
125
        $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...
126
        if ($this->eventDispatcher->dispatch(SectionEvents::BEFORE_ASSIGN_SECTION_TO_SUBTREE, $beforeEvent)->isPropagationStopped()) {
127
            return;
128
        }
129
130
        parent::assignSectionToSubtree($location, $section);
131
132
        $this->eventDispatcher->dispatch(
133
            SectionEvents::ASSIGN_SECTION_TO_SUBTREE,
134
            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...
135
        );
136
    }
137
138
    public function deleteSection(Section $section)
139
    {
140
        $eventData = [$section];
141
142
        $beforeEvent = new BeforeDeleteSectionEvent(...$eventData);
143
        if ($this->eventDispatcher->dispatch(SectionEvents::BEFORE_DELETE_SECTION, $beforeEvent)->isPropagationStopped()) {
144
            return;
145
        }
146
147
        parent::deleteSection($section);
148
149
        $this->eventDispatcher->dispatch(
150
            SectionEvents::DELETE_SECTION,
151
            new DeleteSectionEvent(...$eventData)
152
        );
153
    }
154
}
155