19declare(strict_types=1);
53 private readonly
Factory $flavour_machine_factory,
57 private readonly
Subject $events
66 return $this->flavour_resource_repository->has(
79 bool $force_building = false
81 $this->checkDefinition($definition);
82 if (!$this->resource_builder->has($rid)) {
85 if ($this->
has($rid, $definition)) {
86 return $this->read($rid, $definition, $force_building);
89 return $this->build($rid, $definition);
96 $flavour = $this->new($definition, $rid);
97 $flavour = $this->runMachine($rid, $definition, $flavour);
100 $this->flavour_resource_repository->store($flavour);
101 $flavour = $this->populateFlavourWithExistingStreams($flavour);
110 bool $force_building =
false
112 $current_revision = $this->getResource($rid)->getCurrentRevision();
113 $flavour = $this->flavour_resource_repository->get(
115 $current_revision->getVersionNumber(),
119 if ($force_building || !$this->hasFlavourStreams($flavour)) {
121 $storage = $this->getStorageHandler($flavour);
122 $storage->deleteFlavour($current_revision, $flavour);
124 return $this->runMachine($rid, $definition, $flavour);
127 return $this->populateFlavourWithExistingStreams($flavour);
135 $this->getResource($rid)->getCurrentRevision()->getVersionNumber()
139 public function delete(
143 $current_revision = $this->getResource($rid)->getCurrentRevision();
144 $revision_number = $current_revision->getVersionNumber();
146 if ($this->flavour_resource_repository->has($rid, $revision_number, $definition)) {
147 $flavour = $this->flavour_resource_repository->get($rid, $revision_number, $definition);
148 $this->flavour_resource_repository->delete($flavour);
149 $storage = $this->getStorageHandler($flavour);
150 $storage->deleteFlavour($current_revision, $flavour);
162 return $this->getStorageHandler($flavour)->hasFlavour(
163 $this->getCurrentRevision($flavour),
171 $storable->setStreams($streams);
173 $this->getStorageHandler($flavour)->storeFlavour(
174 $this->getCurrentRevision($flavour),
181 $handler = $this->getStorageHandler($flavour);
183 $revision = $this->getCurrentRevision($flavour);
188 ) as $index => $file_stream
190 $flavour = $this->stream_access->populateFlavour($flavour, $file_stream, $index);
199 throw new \InvalidArgumentException(
"FlavourDefinition not supported by machine");
208 if (strlen($definition->
getVariantName()) > self::VARIANT_NAME_MAX_LENGTH) {
209 throw new \InvalidArgumentException(
"FlavourDefinition variant name too long");
218 $this->checkDefinition($definition);
219 $machine = $this->flavour_machine_factory->get($definition);
220 $this->checkDefinitionForMachine($definition, $machine);
221 }
catch (\Throwable) {
224 if ($machine instanceof NullMachine) {
227 $engine = $machine->getEngine();
228 if (!$engine->isRunning()) {
231 $current_revision = $this->getResource($rid)->getCurrentRevision();
232 $suffix = $current_revision->getInformation()->getSuffix();
233 $size = $current_revision->getInformation()->getSize();
234 if ($size > $engine->getSizeLimitInBytes()) {
238 return $engine->supports($suffix);
247 $revision = $this->getCurrentRevision($flavour);
250 $handler = $this->getStorageHandler($flavour);
252 $stream = $this->resource_builder->extractStream($revision);
254 }
catch (\Throwable) {
261 $machine = $this->flavour_machine_factory->get($definition);
262 $this->checkDefinitionForMachine($definition, $machine);
265 $storable_streams = [];
268 $machine->processStream(
269 $revision->getInformation(),
274 $generated_stream = $result->getStream();
275 if ($result->isStoreable()) {
277 $storable_streams[$result->getIndex()] = $generated_stream;
280 $cloned_stream = Streams::ofString((
string) $generated_stream);
282 $flavour = $this->stream_access->populateFlavour(
288 }
catch (\Throwable $t) {
296 $this->storeFlavourStreams($flavour, $storable_streams);
299 $this->events->notify(Event::FLAVOUR_BUILD_SUCCESS,
new FlavourData($rid, $definition, $flavour));
309 if (isset($this->current_revision_cache[$rid])) {
313 return $this->current_revision_cache[$rid] = $this->getResourceOfFlavour($flavour)->getCurrentRevision();
319 return $this->resources_cache[$rid_string] ?? ($this->resources_cache[$rid_string] = $this->resource_builder->get(
326 return $this->getResource($flavour->getResourceID());
331 return $this->storage_handler_factory->getHandlerForResource($this->getResourceOfFlavour($flavour));
Stream factory which enables the user to create streams without the knowledge of the concrete class.
build(ResourceIdentification $rid, FlavourDefinition $definition)
checkDefinitionForMachine(FlavourDefinition $definition, FlavourMachine $machine)
array $current_revision_cache
has(ResourceIdentification $identification, FlavourDefinition $definition)
testDefinition(ResourceIdentification $rid, FlavourDefinition $definition)
read(ResourceIdentification $rid, FlavourDefinition $definition, bool $force_building=false)
__construct(private readonly FlavourRepository $flavour_resource_repository, private readonly Factory $flavour_machine_factory, private readonly ResourceBuilder $resource_builder, private readonly StorageHandlerFactory $storage_handler_factory, private readonly StreamAccess $stream_access, private readonly Subject $events)
getCurrentRevision(Flavour $flavour)
hasFlavourStreams(Flavour $flavour)
runMachine(ResourceIdentification $rid, FlavourDefinition $definition, Flavour $flavour)
getResource(ResourceIdentification $rid)
checkDefinition(FlavourDefinition $definition)
storeFlavourStreams(Flavour $flavour, array $streams)
getResourceOfFlavour(Flavour $flavour)
const VARIANT_NAME_MAX_LENGTH
getStorageHandler(Flavour $flavour)
populateFlavourWithExistingStreams(Flavour $flavour)
Class ResourceIdentification.
Class ResourceNotFoundException.
Class StorageHandlerFactory.
persist()
Define whether the generated flavor and the respective streams should be persisted,...
getVariantName()
If a definition can be used in several variants (e.g.
canHandleDefinition(FlavourDefinition $definition)
Check if a corresponding configuration can be processed by this Machine.
Interface StorageResource.
Class FileResourceHandler.
@ FLAVOUR_BUILD_FAILED
event string being used if a new Resource has been stored to the IRSS.