1<?php declare(strict_types=1); 2 3namespace ILIAS\MainMenu\Storage; 4 5use Generator; 6use ILIAS\FileUpload\DTO\UploadResult; 7use ILIAS\MainMenu\Storage\Consumer\ConsumerFactory; 8use ILIAS\MainMenu\Storage\Consumer\DownloadConsumer; 9use ILIAS\MainMenu\Storage\Consumer\FileStreamConsumer; 10use ILIAS\MainMenu\Storage\Consumer\InlineConsumer; 11use ILIAS\MainMenu\Storage\Identification\ResourceIdentification; 12use ILIAS\MainMenu\Storage\Information\Repository\InformationARRepository; 13use ILIAS\MainMenu\Storage\Resource\Repository\ResourceARRepository; 14use ILIAS\MainMenu\Storage\Resource\ResourceBuilder; 15use ILIAS\MainMenu\Storage\Resource\Stakeholder\ResourceStakeholder; 16use ILIAS\MainMenu\Storage\Revision\Repository\RevisionARRepository; 17use ILIAS\MainMenu\Storage\Revision\Revision; 18use ILIAS\MainMenu\Storage\StorageHandler\FileSystemStorageHandler; 19use ILIAS\MainMenu\Storage\StorageHandler\StorageHandlerFactory; 20use LogicException; 21 22/** 23 * Class Services 24 * 25 * @public 26 * 27 * @author Fabian Schmid <fs@studer-raimann.ch> 28 */ 29class Services 30{ 31 32 /** 33 * @var ConsumerFactory 34 */ 35 private $consumer_factory; 36 /** 37 * @var ResourceBuilder 38 */ 39 private $resource_builder; 40 41 42 /** 43 * Services constructor. 44 */ 45 public function __construct() 46 { 47 $this->resource_builder = new ResourceBuilder( 48 new FileSystemStorageHandler(), 49 new RevisionARRepository(), 50 new ResourceARRepository(), 51 new InformationARRepository() 52 ); 53 $this->consumer_factory = new ConsumerFactory(new StorageHandlerFactory()); 54 } 55 56 57 /** 58 * this is the fast-lane: in most cases you want to store a uploaded file in 59 * the storage and use it's identification. 60 * 61 * @param UploadResult $result 62 * @param ResourceStakeholder $stakeholder 63 * @param string $title 64 * 65 * @return ResourceIdentification 66 */ 67 public function upload(UploadResult $result, ResourceStakeholder $stakeholder, string $title = null) : ResourceIdentification 68 { 69 if ($result->isOK()) { 70 $resource = $this->resource_builder->new($result); 71 72 $this->resource_builder->store($resource); 73 74 return $resource->getIdentification(); 75 } else { 76 throw new LogicException("Can't handle UploadResult: " . $result->getStatus()->getMessage()); 77 } 78 } 79 80 81 public function find(string $identification) : ?ResourceIdentification 82 { 83 $resource_identification = new ResourceIdentification($identification); 84 85 if ($this->resource_builder->has($resource_identification)) { 86 return $resource_identification; 87 } 88 89 return null; 90 } 91 92 93 public function getRevision(ResourceIdentification $identification) : Revision 94 { 95 return $this->resource_builder->get($identification)->getCurrentRevision(); 96 } 97 98 99 public function remove(ResourceIdentification $identification) : void 100 { 101 $this->resource_builder->remove($this->resource_builder->get($identification)); 102 } 103 104 105 /** 106 * @return Generator|ResourceIdentification[] 107 */ 108 public function getAll() : Generator 109 { 110 foreach ($this->resource_builder->getAll() as $item) { 111 /** 112 * @var $item StorableResource 113 */ 114 yield $item->getIdentification(); 115 } 116 } 117 118 119 // 120 // CONSUMERS 121 // 122 123 public function download(ResourceIdentification $identification) : DownloadConsumer 124 { 125 return $this->consumer_factory->download($this->resource_builder->get($identification)); 126 } 127 128 129 public function inline(ResourceIdentification $identification) : InlineConsumer 130 { 131 return $this->consumer_factory->inline($this->resource_builder->get($identification)); 132 } 133 134 135 public function stream(ResourceIdentification $identification) : FileStreamConsumer 136 { 137 return $this->consumer_factory->fileStream($this->resource_builder->get($identification)); 138 } 139} 140