ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
CollectionBuilder.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
29use ILIAS\ResourceStorage\Preloader\SecureString;
33
41{
42 use SecureString;
43
44
45 public function __construct(private CollectionRepository $collection_repository, private Subject $events, private CollectionIdentificationGenerator $id_generator = new UniqueIDCollectionIdentificationGenerator(), private ?LockHandler $lock_handler = null)
46 {
47 }
48
49 public function has(ResourceCollectionIdentification $identification): bool
50 {
51 return $this->collection_repository->has($identification);
52 }
53
57 public function getResourceIdStrings(ResourceCollectionIdentification $identification): \Generator
58 {
59 yield from $this->collection_repository->getResourceIdStrings($identification);
60 }
61
65 public function getResourceIds(ResourceCollectionIdentification $identification): \Generator
66 {
67 foreach ($this->getResourceIdStrings($identification) as $string) {
68 yield new ResourceIdentification($string);
69 }
70 }
71
72 private function validate(ResourceCollectionIdentification $identification): void
73 {
74 if (!$this->id_generator->validateScheme($identification->serialize())) {
75 throw new \InvalidArgumentException('Invalid identification scheme');
76 }
77 }
78
79 public function new(?int $owner = null): ResourceCollection
80 {
81 return $this->collection_repository->blank(
82 $this->id_generator->getUniqueResourceCollectionIdentification(),
84 );
85 }
86
87 public function get(ResourceCollectionIdentification $identification, ?int $owner = null): ResourceCollection
88 {
89 $this->validate($identification);
90 $existing = $this->collection_repository->existing($identification);
91 if ($existing->hasSpecificOwner()
92 && $existing->getOwner() !== $owner
93 ) {
94 // The original plan was that collections could be explicitly assigned to a user as an option.
95 // Such collections can then only be read by that user. However, the concept was never described
96 // and the check has therefore now been deactivated.
97 // See, for example, https://mantis.ilias.de/view.php?id=42127#c112463
98 // throw new \InvalidArgumentException('Invalid owner of collection');
99
100 }
101 return $existing;
102 }
103
104 public function store(ResourceCollection $collection): bool
105 {
106 $event_data_container = new DataContainer();
107 if ($this->lock_handler !== null) {
108 $result = $this->lock_handler->lockTables(
109 $this->collection_repository->getNamesForLocking(),
110 function () use ($collection, $event_data_container): void {
111 $this->collection_repository->update($collection, $event_data_container);
112 }
113 );
114 $result->runAndUnlock();
115 } else {
116 $this->collection_repository->update($collection, $event_data_container);
117 }
118
119 // notify about the change. we must do this after the lock is released
120 foreach ($event_data_container->get() as $event_data) {
121 $this->events->notify(Event::COLLECTION_RESOURCE_ADDED, $event_data);
122 }
123
124 return true;
125 }
126
127 public function delete(ResourceCollectionIdentification $identification): bool
128 {
129 $this->collection_repository->delete($identification);
130 return true;
131 }
132
133 public function notififyResourceDeletion(ResourceIdentification $identification): void
134 {
135 $this->collection_repository->removeResourceFromAllCollections($identification);
136 }
137}
notififyResourceDeletion(ResourceIdentification $identification)
getResourceIdStrings(ResourceCollectionIdentification $identification)
__construct(private CollectionRepository $collection_repository, private Subject $events, private CollectionIdentificationGenerator $id_generator=new UniqueIDCollectionIdentificationGenerator(), private ?LockHandler $lock_handler=null)
has(ResourceCollectionIdentification $identification)
getResourceIds(ResourceCollectionIdentification $identification)
validate(ResourceCollectionIdentification $identification)