forked from ezsystems/ezpublish-kernel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SectionService.php
361 lines (322 loc) · 13 KB
/
SectionService.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
<?php
/**
* File containing the eZ\Publish\Core\Repository\SectionService class.
*
* @copyright Copyright (C) 1999-2014 eZ Systems AS. All rights reserved.
* @license http://www.gnu.org/licenses/gpl-2.0.txt GNU General Public License v2
* @version //autogentag//
* @package eZ\Publish\Core\Repository
*/
namespace eZ\Publish\Core\Repository;
use eZ\Publish\API\Repository\Values\Content\SectionCreateStruct;
use eZ\Publish\API\Repository\Values\Content\ContentInfo;
use eZ\Publish\API\Repository\Values\Content\Section;
use eZ\Publish\API\Repository\Values\Content\SectionUpdateStruct;
use eZ\Publish\API\Repository\SectionService as SectionServiceInterface;
use eZ\Publish\API\Repository\Repository as RepositoryInterface;
use eZ\Publish\SPI\Persistence\Content\Section\Handler;
use eZ\Publish\SPI\Persistence\Content\Section as SPISection;
use eZ\Publish\Core\Base\Exceptions\InvalidArgumentValue;
use eZ\Publish\Core\Base\Exceptions\InvalidArgumentException;
use eZ\Publish\Core\Base\Exceptions\BadStateException;
use eZ\Publish\Core\Base\Exceptions\UnauthorizedException;
use eZ\Publish\API\Repository\Exceptions\NotFoundException as APINotFoundException;
use Exception;
/**
* Section service, used for section operations
*
* @package eZ\Publish\Core\Repository
*/
class SectionService implements SectionServiceInterface
{
/**
* @var \eZ\Publish\API\Repository\Repository
*/
protected $repository;
/**
* @var \eZ\Publish\SPI\Persistence\Content\Section\Handler
*/
protected $sectionHandler;
/**
* @var array
*/
protected $settings;
/**
* Setups service with reference to repository object that created it & corresponding handler
*
* @param \eZ\Publish\API\Repository\Repository $repository
* @param \eZ\Publish\SPI\Persistence\Content\Section\Handler $sectionHandler
* @param array $settings
*/
public function __construct( RepositoryInterface $repository, Handler $sectionHandler, array $settings = array() )
{
$this->repository = $repository;
$this->sectionHandler = $sectionHandler;
// Union makes sure default settings are ignored if provided in argument
$this->settings = $settings + array(
//'defaultSetting' => array(),
);
}
/**
* Creates a new Section in the content repository
*
* @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException If the current user user is not allowed to create a section
* @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException If the new identifier in $sectionCreateStruct already exists
*
* @param \eZ\Publish\API\Repository\Values\Content\SectionCreateStruct $sectionCreateStruct
*
* @return \eZ\Publish\API\Repository\Values\Content\Section The newly created section
*/
public function createSection( SectionCreateStruct $sectionCreateStruct )
{
if ( !is_string( $sectionCreateStruct->name ) || empty( $sectionCreateStruct->name ) )
throw new InvalidArgumentValue( "name", $sectionCreateStruct->name, "SectionCreateStruct" );
if ( !is_string( $sectionCreateStruct->identifier ) || empty( $sectionCreateStruct->identifier ) )
throw new InvalidArgumentValue( "identifier", $sectionCreateStruct->identifier, "SectionCreateStruct" );
if ( $this->repository->hasAccess( 'section', 'edit' ) !== true )
throw new UnauthorizedException( 'section', 'edit' );
try
{
$existingSection = $this->loadSectionByIdentifier( $sectionCreateStruct->identifier );
if ( $existingSection !== null )
throw new InvalidArgumentException( "sectionCreateStruct", "section with specified identifier already exists" );
}
catch ( APINotFoundException $e )
{
// Do nothing
}
$this->repository->beginTransaction();
try
{
$spiSection = $this->sectionHandler->create(
$sectionCreateStruct->name,
$sectionCreateStruct->identifier
);
$this->repository->commit();
}
catch ( Exception $e )
{
$this->repository->rollback();
throw $e;
}
return $this->buildDomainSectionObject( $spiSection );
}
/**
* Updates the given section in the content repository
*
* @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException If the current user user is not allowed to create a section
* @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException If the new identifier already exists (if set in the update struct)
*
* @param \eZ\Publish\API\Repository\Values\Content\Section $section
* @param \eZ\Publish\API\Repository\Values\Content\SectionUpdateStruct $sectionUpdateStruct
*
* @return \eZ\Publish\API\Repository\Values\Content\Section
*/
public function updateSection( Section $section, SectionUpdateStruct $sectionUpdateStruct )
{
if ( $sectionUpdateStruct->name !== null && !is_string( $sectionUpdateStruct->name ) )
throw new InvalidArgumentValue( "name", $section->name, "Section" );
if ( $sectionUpdateStruct->identifier !== null && !is_string( $sectionUpdateStruct->identifier ) )
throw new InvalidArgumentValue( "identifier", $section->identifier, "Section" );
if ( $this->repository->canUser( 'section', 'edit', $section ) !== true )
throw new UnauthorizedException( 'section', 'edit' );
if ( $sectionUpdateStruct->identifier !== null )
{
try
{
$existingSection = $this->loadSectionByIdentifier( $sectionUpdateStruct->identifier );
if ( $existingSection !== null )
throw new InvalidArgumentException( "sectionUpdateStruct", "section with specified identifier already exists" );
}
catch ( APINotFoundException $e )
{
// Do nothing
}
}
$loadedSection = $this->loadSection( $section->id );
$this->repository->beginTransaction();
try
{
$spiSection = $this->sectionHandler->update(
$loadedSection->id,
$sectionUpdateStruct->name ?: $loadedSection->name,
$sectionUpdateStruct->identifier ?: $loadedSection->identifier
);
$this->repository->commit();
}
catch ( Exception $e )
{
$this->repository->rollback();
throw $e;
}
return $this->buildDomainSectionObject( $spiSection );
}
/**
* Loads a Section from its id ($sectionId)
*
* @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException if section could not be found
* @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException If the current user user is not allowed to read a section
*
* @param mixed $sectionId
*
* @return \eZ\Publish\API\Repository\Values\Content\Section
*/
public function loadSection( $sectionId )
{
if ( $this->repository->hasAccess( 'section', 'view' ) !== true )
throw new UnauthorizedException( 'section', 'view' );
$spiSection = $this->sectionHandler->load( $sectionId );
return $this->buildDomainSectionObject( $spiSection );
}
/**
* Loads all sections
*
* @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException If the current user user is not allowed to read a section
*
* @return \eZ\Publish\API\Repository\Values\Content\Section[]
*/
public function loadSections()
{
if ( $this->repository->hasAccess( 'section', 'view' ) !== true )
throw new UnauthorizedException( 'section', 'view' );
$spiSections = $this->sectionHandler->loadAll();
$sections = array();
foreach ( $spiSections as $spiSection )
{
$sections[] = $this->buildDomainSectionObject( $spiSection );
}
return $sections;
}
/**
* Loads a Section from its identifier ($sectionIdentifier)
*
* @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException if section could not be found
* @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException If the current user user is not allowed to read a section
*
* @param string $sectionIdentifier
*
* @return \eZ\Publish\API\Repository\Values\Content\Section
*/
public function loadSectionByIdentifier( $sectionIdentifier )
{
if ( !is_string( $sectionIdentifier ) || empty( $sectionIdentifier ) )
throw new InvalidArgumentValue( "sectionIdentifier", $sectionIdentifier );
if ( $this->repository->hasAccess( 'section', 'view' ) !== true )
throw new UnauthorizedException( 'section', 'view' );
$spiSection = $this->sectionHandler->loadByIdentifier( $sectionIdentifier );
return $this->buildDomainSectionObject( $spiSection );
}
/**
* Counts the contents which $section is assigned to
*
* @param \eZ\Publish\API\Repository\Values\Content\Section $section
*
* @return int
*/
public function countAssignedContents( Section $section )
{
return $this->sectionHandler->assignmentsCount( $section->id );
}
/**
* Assigns the content to the given section
* this method overrides the current assigned section
*
* @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException If user does not have access to view provided object
*
* @param \eZ\Publish\API\Repository\Values\Content\ContentInfo $contentInfo
* @param \eZ\Publish\API\Repository\Values\Content\Section $section
*/
public function assignSection( ContentInfo $contentInfo, Section $section )
{
$loadedContentInfo = $this->repository->getContentService()->loadContentInfo( $contentInfo->id );
$loadedSection = $this->loadSection( $section->id );
if ( $this->repository->canUser( 'section', 'assign', $loadedContentInfo, $loadedSection ) !== true )
{
throw new UnauthorizedException(
'section', 'assign',
array(
'name' => $loadedSection->name,
'content-name' => $loadedContentInfo->name
)
);
}
$this->repository->beginTransaction();
try
{
$this->sectionHandler->assign(
$loadedSection->id,
$loadedContentInfo->id
);
$this->repository->commit();
}
catch ( Exception $e )
{
$this->repository->rollback();
throw $e;
}
}
/**
* Deletes $section from content repository
*
* @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException If the specified section is not found
* @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException If the current user is not allowed to delete a section
* @throws \eZ\Publish\API\Repository\Exceptions\BadStateException If section can not be deleted
* because it is still assigned to some contents.
*
* @param \eZ\Publish\API\Repository\Values\Content\Section $section
*/
public function deleteSection( Section $section )
{
$loadedSection = $this->loadSection( $section->id );
if ( $this->repository->canUser( 'section', 'edit', $loadedSection ) !== true )
throw new UnauthorizedException( 'section', 'edit', array( 'name' => $loadedSection->name ) );
if ( $this->countAssignedContents( $loadedSection ) > 0 )
throw new BadStateException( "section", 'section is still assigned to content' );
$this->repository->beginTransaction();
try
{
$this->sectionHandler->delete( $loadedSection->id );
$this->repository->commit();
}
catch ( Exception $e )
{
$this->repository->rollback();
throw $e;
}
}
/**
* Instantiates a new SectionCreateStruct
*
* @return \eZ\Publish\API\Repository\Values\Content\SectionCreateStruct
*/
public function newSectionCreateStruct()
{
return new SectionCreateStruct();
}
/**
* Instantiates a new SectionUpdateStruct
*
* @return \eZ\Publish\API\Repository\Values\Content\SectionUpdateStruct
*/
public function newSectionUpdateStruct()
{
return new SectionUpdateStruct();
}
/**
* Builds API Section object from provided SPI Section object
*
* @param \eZ\Publish\SPI\Persistence\Content\Section $spiSection
*
* @return \eZ\Publish\API\Repository\Values\Content\Section
*/
protected function buildDomainSectionObject( SPISection $spiSection )
{
return new Section(
array(
'id' => $spiSection->id,
'identifier' => $spiSection->identifier,
'name' => $spiSection->name
)
);
}
}