diff --git a/src/Xhgui/Controller/ImportController.php b/src/Xhgui/Controller/ImportController.php index 67eef4665..2615b31a4 100644 --- a/src/Xhgui/Controller/ImportController.php +++ b/src/Xhgui/Controller/ImportController.php @@ -32,8 +32,8 @@ public function import() $response = $this->app->response(); try { - $this->runImport($request); - $result = ['ok' => true, 'size' => $request->getContentLength()]; + $id = $this->runImport($request); + $result = ['ok' => true, 'id' => $id, 'size' => $request->getContentLength()]; } catch (InvalidArgumentException $e) { $result = ['error' => true, 'message' => $e->getMessage()]; $response->setStatus(401); @@ -46,7 +46,7 @@ public function import() $response->body(json_encode($result)); } - private function runImport(Request $request) + private function runImport(Request $request): string { if ($this->token) { if ($this->token !== $request->get('token')) { @@ -55,6 +55,6 @@ private function runImport(Request $request) } $data = json_decode($request->getBody(), true); - $this->saver->save($data); + return $this->saver->save($data); } } diff --git a/src/Xhgui/Saver/MongoSaver.php b/src/Xhgui/Saver/MongoSaver.php index a1f9e62dd..ad1ad0bb5 100644 --- a/src/Xhgui/Saver/MongoSaver.php +++ b/src/Xhgui/Saver/MongoSaver.php @@ -23,7 +23,7 @@ public function __construct(MongoCollection $collection) $this->_collection = $collection; } - public function save(array $data) + public function save(array $data): string { // build 'request_ts' and 'request_date' from 'request_ts_micro' $ts = $data['meta']['request_ts_micro']; @@ -41,12 +41,15 @@ public function save(array $data) 'request_date' => date('Y-m-d', $sec), ]; + $id = $data['_id'] ?? new MongoId(); $a = [ - '_id' => $data['_id'] ?? new MongoId(), + '_id' => $id, 'meta' => $meta, 'profile' => $data['profile'], ]; - return $this->_collection->insert($a, ['w' => 0]); + $this->_collection->insert($a, ['w' => 0]); + + return (string)$id; } } diff --git a/src/Xhgui/Saver/PdoSaver.php b/src/Xhgui/Saver/PdoSaver.php index d9517eaf2..594322924 100644 --- a/src/Xhgui/Saver/PdoSaver.php +++ b/src/Xhgui/Saver/PdoSaver.php @@ -16,7 +16,7 @@ public function __construct(PdoRepository $db) $this->db = $db; } - public function save(array $data) + public function save(array $data): string { $main = $data['profile']['main()']; @@ -25,8 +25,9 @@ public function save(array $data) $sec = $ts['sec']; $usec = $ts['usec']; + $id = $data['_id'] ?? Util::generateId(); $this->db->saveProfile([ - 'id' => $data['_id'] ?? Util::generateId(), + 'id' => $id, 'profile' => json_encode($data['profile']), 'url' => $data['meta']['url'], 'SERVER' => json_encode($data['meta']['SERVER']), @@ -42,5 +43,7 @@ public function save(array $data) 'main_mu' => $main['mu'], 'main_pmu' => $main['pmu'], ]); + + return $id; } } diff --git a/src/Xhgui/Saver/SaverInterface.php b/src/Xhgui/Saver/SaverInterface.php index c9a9de4ae..77d52f513 100644 --- a/src/Xhgui/Saver/SaverInterface.php +++ b/src/Xhgui/Saver/SaverInterface.php @@ -4,5 +4,8 @@ interface SaverInterface { - public function save(array $data); + /** + * Returns id of the saved profile + */ + public function save(array $data): string; }