Dispatches old report files and portable bundles through the normal import endpoint. */
| 13 | |
| 14 | /** Dispatches old report files and portable bundles through the normal import endpoint. */ |
| 15 | class BundleImportService { |
| 16 | public function __construct( |
| 17 | private $userId, |
| 18 | private ReportService $reports, |
| 19 | private DatasetService $datasets, |
| 20 | private PanoramaService $panoramas, |
| 21 | private ThresholdService $thresholds, |
| 22 | private StorageMapper $storage, |
| 23 | private IRootFolder $rootFolder, |
| 24 | private IDBConnection $db, |
| 25 | private IConfig $config, |
| 26 | private ILockingProvider $locking, |
| 27 | ) {} |
| 28 | |
| 29 | public function import(?string $path = null, ?string $raw = null): int|array { |
| 30 | if ($path !== null && $path !== '') $raw = $this->rootFolder->getUserFolder($this->userId)->get($path)->getContent(); |
| 31 | if ($raw === null) return 0; |
| 32 | $data = json_decode($raw, true); |
| 33 | if (is_array($data) && array_key_exists('format', $data)) return $this->importBundle($data); |
| 34 | return $this->reports->import(null, $raw); |
| 35 | } |
| 36 | |
| 37 | public function installDemo(): array { |
| 38 | $key = 'analytics-demo-' . $this->userId; |
| 39 | $this->locking->acquireLock($key, ILockingProvider::LOCK_EXCLUSIVE); |
| 40 | try { |
| 41 | $id = (int)$this->config->getUserValue($this->userId, 'analytics', 'business-demo-panorama', '0'); |
| 42 | if ($id > 0 && $this->panoramas->isOwn($id)) return ['kind' => 'bundle', 'panoramaId' => $id, 'reused' => true]; |
| 43 | $raw = file_get_contents(__DIR__ . '/../../resources/demo.json'); |
| 44 | if ($raw === false) throw new \RuntimeException('Demo bundle unavailable'); |
| 45 | return $this->importBundle(json_decode($raw, true, 512, JSON_THROW_ON_ERROR), true); |
| 46 | } finally { |
| 47 | $this->locking->releaseLock($key, ILockingProvider::LOCK_EXCLUSIVE); |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | public function importBundle(array $bundle, bool $demo = false): array { |
| 52 | BundleDefinition::validate($bundle); |
| 53 | $folders = $datasets = $reports = $panoramas = []; |
| 54 | $this->db->beginTransaction(); |
| 55 | try { |
| 56 | foreach ($bundle['folders'] ?? [] as $folder) { |
| 57 | $id = match ($folder['scope']) { |
| 58 | 'datasets' => $this->datasets->createGroup(), |
| 59 | 'panoramas' => $this->panoramas->createGroup(), |
| 60 | 'reports' => $this->reports->create($folder['name'], '', 0, 0, 0, '{}', 'table', '', '', '', ''), |
| 61 | }; |
| 62 | $this->requireId($id); |
| 63 | if ($folder['scope'] === 'datasets') $this->requireSuccess($this->datasets->rename($id, $folder['name'])); |
| 64 | if ($folder['scope'] === 'panoramas') $this->requireSuccess($this->panoramas->rename($id, $folder['name'])); |
| 65 | $folders[$folder['key']] = $id; |
| 66 | } |
| 67 | $definitions = []; |
| 68 | foreach ($bundle['datasets'] ?? [] as $dataset) { |
| 69 | $id = $this->datasets->create($dataset['name'], $dataset['dimension1'], $dataset['dimension2'], $dataset['value']); |
| 70 | $this->requireId($id); |
| 71 | if (isset($dataset['folder'])) $this->requireSuccess($this->datasets->updateGroup($id, $folders[$dataset['folder']])); |
| 72 | foreach ($dataset['data'] ?? [] as $row) $this->storage->create($id, $row[0], $row[1], $row[2]); |
nothing calls this directly
no outgoing calls
no test coverage detected