Skip to content

Commit

Permalink
Merge pull request #345 from perftools/saver-return-id
Browse files Browse the repository at this point in the history
  • Loading branch information
glensc authored Oct 9, 2020
2 parents 4fd798d + b63a6c5 commit 9733d3e
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 10 deletions.
8 changes: 4 additions & 4 deletions src/Xhgui/Controller/ImportController.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ public function __construct(App $app, SaverInterface $saver, $token)
public function import(Request $request, Response $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);
Expand All @@ -44,7 +44,7 @@ public function import(Request $request, Response $response)
$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')) {
Expand All @@ -53,6 +53,6 @@ private function runImport(Request $request)
}

$data = json_decode($request->getBody(), true);
$this->saver->save($data);
return $this->saver->save($data);
}
}
9 changes: 6 additions & 3 deletions src/Xhgui/Saver/MongoSaver.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'];
Expand All @@ -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;
}
}
7 changes: 5 additions & 2 deletions src/Xhgui/Saver/PdoSaver.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()'];

Expand All @@ -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']),
Expand All @@ -42,5 +43,7 @@ public function save(array $data)
'main_mu' => $main['mu'],
'main_pmu' => $main['pmu'],
]);

return $id;
}
}
5 changes: 4 additions & 1 deletion src/Xhgui/Saver/SaverInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,8 @@

interface SaverInterface
{
public function save(array $data);
/**
* Returns id of the saved profile
*/
public function save(array $data): string;
}

0 comments on commit 9733d3e

Please sign in to comment.