-
Notifications
You must be signed in to change notification settings - Fork 81
Expand file tree
/
Copy pathObjectStateCommand.php
More file actions
93 lines (74 loc) · 4.27 KB
/
Copy pathObjectStateCommand.php
File metadata and controls
93 lines (74 loc) · 4.27 KB
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
<?php declare(strict_types=1);
namespace App\Command;
use Ibexa\Contracts\Core\Repository\ContentService;
use Ibexa\Contracts\Core\Repository\ObjectStateService;
use Ibexa\Contracts\Core\Repository\PermissionResolver;
use Ibexa\Contracts\Core\Repository\UserService;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
#[AsCommand(
name: 'doc:object_state',
description: 'Creates OS group with provided States and assigned the Lock OS to provided content item'
)]
class ObjectStateCommand extends Command
{
private ContentService $contentService;
private UserService $userService;
private ObjectStateService $objectStateService;
private PermissionResolver $permissionResolver;
public function __construct(ContentService $contentService, UserService $userService, ObjectStateService $objectStateService, PermissionResolver $permissionResolver)
{
$this->contentService = $contentService;
$this->userService = $userService;
$this->objectStateService = $objectStateService;
$this->permissionResolver = $permissionResolver;
parent::__construct();
}
protected function configure(): void
{
$this
->setDefinition([
new InputArgument('objectStateGroupIdentifier', InputArgument::REQUIRED, 'Identifier of new OG group to create'),
new InputArgument('objectStateIdentifier', InputArgument::REQUIRED, 'Identifier(s) of a new Object State'),
new InputArgument('contentID', InputArgument::OPTIONAL, 'Content ID'),
]);
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
$user = $this->userService->loadUserByLogin('admin');
$this->permissionResolver->setCurrentUserReference($user);
$objectStateGroup = $this->objectStateService->loadObjectStateGroupByIdentifier('ez_lock');
$objectState = $this->objectStateService->loadObjectStateByIdentifier($objectStateGroup, 'locked');
$output->writeln($objectStateGroup->getName());
$output->writeln($objectState->getName());
$objectStateGroupIdentifier = $input->getArgument('objectStateGroupIdentifier');
$objectStateIdentifierList = explode(',', $input->getArgument('objectStateIdentifier'));
$objectStateGroupStruct = $this->objectStateService->newObjectStateGroupCreateStruct($objectStateGroupIdentifier);
$objectStateGroupStruct->defaultLanguageCode = 'eng-GB';
$objectStateGroupStruct->names = ['eng-GB' => $objectStateGroupIdentifier];
$newObjectStateGroup = $this->objectStateService->createObjectStateGroup($objectStateGroupStruct);
foreach ($objectStateIdentifierList as $objectStateIdentifier) {
$stateStruct = $this->objectStateService->newObjectStateCreateStruct($objectStateIdentifier);
$stateStruct->defaultLanguageCode = 'eng-GB';
$stateStruct->names = ['eng-GB' => $objectStateIdentifier];
$this->objectStateService->createObjectState($newObjectStateGroup, $stateStruct);
}
$output->writeln('Created new Object state group ' . $newObjectStateGroup->identifier . ' with Object states:');
foreach ($this->objectStateService->loadObjectStates($newObjectStateGroup) as $objectState) {
$output->writeln('* ' . $objectState->getName());
}
if ($input->getArgument('contentID')) {
$contentId = (int) $input->getArgument('contentID');
$objectStateToAssign = $objectStateIdentifierList[0];
$contentInfo = $this->contentService->loadContentInfo($contentId);
$objectStateGroup = $this->objectStateService->loadObjectStateGroupByIdentifier($objectStateGroupIdentifier);
$objectState = $this->objectStateService->loadObjectStateByIdentifier($objectStateGroup, $objectStateToAssign);
$this->objectStateService->setContentState($contentInfo, $objectStateGroup, $objectState);
$output->writeln('Content ' . $contentInfo->name . ' assigned state ' . $objectState->getName());
}
return self::SUCCESS;
}
}