*/ class StorageLocationsController extends AbstractAdminApiCrudController { protected string $entityClass = Entity\StorageLocation::class; protected string $resourceRouteName = 'api:admin:storage_location'; public function __construct( protected Entity\Repository\StorageLocationRepository $storageLocationRepo, ReloadableEntityManagerInterface $em, Serializer $serializer, ValidatorInterface $validator ) { parent::__construct($em, $serializer, $validator); } public function listAction(ServerRequest $request, Response $response): ResponseInterface { $qb = $this->em->createQueryBuilder(); $qb->select('sl') ->from(Entity\StorageLocation::class, 'sl'); $type = $request->getQueryParam('type'); if (!empty($type)) { $qb->andWhere('sl.type = :type') ->setParameter('type', $type); } $query = $qb->getQuery(); return $this->listPaginatedFromQuery($request, $response, $query); } /** @inheritDoc */ protected function viewRecord(object $record, ServerRequest $request): object { /** @var Entity\StorageLocation $record */ $original = parent::viewRecord($record, $request); $return = new Entity\Api\Admin\StorageLocation(); $return->fromParentObject($original); $return->storageQuotaBytes = (string)($record->getStorageQuotaBytes() ?? ''); $return->storageUsedBytes = (string)$record->getStorageUsedBytes(); $return->storageUsedPercent = $record->getStorageUsePercentage(); $return->storageAvailable = $record->getStorageAvailable(); $return->storageAvailableBytes = (string)($record->getStorageAvailableBytes() ?? ''); $return->isFull = $record->isStorageFull(); $return->uri = $record->getUri(); $stationsRaw = $this->storageLocationRepo->getStationsUsingLocation($record); $stations = []; foreach ($stationsRaw as $station) { $stations[] = $station->getName(); } $return->stations = $stations; return $return; } protected function deleteRecord(object $record): void { if (!($record instanceof Entity\StorageLocation)) { throw new InvalidArgumentException(sprintf('Record must be an instance of %s.', $this->entityClass)); } $stations = $this->storageLocationRepo->getStationsUsingLocation($record); if (0 !== count($stations)) { $stationNames = []; foreach ($stations as $station) { $stationNames[] = $station->getName(); } throw new RuntimeException('This storage location has stations associated with it, and cannot be ' . ' deleted until these stations are updated: ' . implode(', ', $stationNames)); } parent::deleteRecord($record); } }