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