ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables Modules Pages
CollectionBuilder.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 
28 
36 {
37  use SecureString;
38 
42 
43 
44  public function __construct(
45  Repository\CollectionRepository $collection_repository,
46  ?CollectionIdentificationGenerator $id_generator = null,
47  ?\ILIAS\ResourceStorage\Lock\LockHandler $lock_handler = null
48  ) {
49  $this->collection_repository = $collection_repository;
50  $this->lock_handler = $lock_handler;
51  $this->id_generator = $id_generator ?? new UniqueIDCollectionIdentificationGenerator();
52  }
53 
54  public function has(ResourceCollectionIdentification $identification): bool
55  {
56  return $this->collection_repository->has($identification);
57  }
58 
63  {
64  yield from $this->collection_repository->getResourceIdStrings($identification);
65  }
66 
70  public function getResourceIds(ResourceCollectionIdentification $identification): \Generator
71  {
72  foreach ($this->getResourceIdStrings($identification) as $string) {
73  yield new ResourceIdentification($string);
74  }
75  }
76 
77  private function validate(ResourceCollectionIdentification $identification): void
78  {
79  if (!$this->id_generator->validateScheme($identification->serialize())) {
80  throw new \InvalidArgumentException('Invalid identification scheme');
81  }
82  }
83 
84  public function new(?int $owner = null): ResourceCollection
85  {
86  return $this->collection_repository->blank(
87  $this->id_generator->getUniqueResourceCollectionIdentification(),
89  );
90  }
91 
92  public function get(ResourceCollectionIdentification $identification, ?int $owner = null): ResourceCollection
93  {
94  $this->validate($identification);
95  $existing = $this->collection_repository->existing($identification);
96  if ($existing->hasSpecificOwner()
97  && $existing->getOwner() !== $owner
98  ) {
99  throw new \InvalidArgumentException('Invalid owner of collection');
100  }
101  return $existing;
102  }
103 
104  public function store(ResourceCollection $collection): bool
105  {
106  if ($this->lock_handler !== null) {
107  $result = $this->lock_handler->lockTables(
108  $this->collection_repository->getNamesForLocking(),
109  function () use ($collection): void {
110  $this->collection_repository->update($collection);
111  }
112  );
113  $result->runAndUnlock();
114  } else {
115  $this->collection_repository->update($collection);
116  }
117 
118  return true;
119  }
120 
121  public function delete(ResourceCollectionIdentification $identification): bool
122  {
123  $this->collection_repository->delete($identification);
124  return true;
125  }
126 
127  public function notififyResourceDeletion(ResourceIdentification $identification): void
128  {
129  $this->collection_repository->removeResourceFromAllCollections($identification);
130  }
131 }
getResourceIdStrings(ResourceCollectionIdentification $identification)
notififyResourceDeletion(ResourceIdentification $identification)
Class ChatMainBarProvider .
Repository CollectionRepository $collection_repository
getResourceIds(ResourceCollectionIdentification $identification)
CollectionIdentificationGenerator $id_generator
__construct(Repository\CollectionRepository $collection_repository, ?CollectionIdentificationGenerator $id_generator=null, ?\ILIAS\ResourceStorage\Lock\LockHandler $lock_handler=null)
validate(ResourceCollectionIdentification $identification)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
has(ResourceCollectionIdentification $identification)
ILIAS ResourceStorage Lock LockHandler $lock_handler
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...