Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow overriding saver id only via id parameter #348

Merged
merged 4 commits into from
Oct 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Xhgui/Controller/RunController.php
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ public function deleteSubmit(Request $request)
{
$id = $request->post('id');
// Don't call profilers->delete() unless $id is set,
// otherwise it will turn the null into a MongoId and return "Sucessful".
// otherwise it will turn the null into a MongoId and return "Successful".
if (!is_string($id) || !strlen($id)) {
// Form checks this already,
// only reachable by handcrafted or malformed requests.
Expand Down
7 changes: 3 additions & 4 deletions src/Xhgui/Saver/MongoSaver.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public function __construct(MongoCollection $collection)
$this->_collection = $collection;
}

public function save(array $data): string
public function save(array $data, string $id = null): string
{
// build 'request_ts' and 'request_date' from 'request_ts_micro'
$ts = $data['meta']['request_ts_micro'];
Expand All @@ -36,15 +36,14 @@ public function save(array $data): string
'request_date' => date('Y-m-d', $sec),
];

$id = $data['_id'] ?? new MongoId();
$a = [
'_id' => $id,
'_id' => new MongoId($id),
'meta' => $meta,
'profile' => $data['profile'],
];

$this->_collection->insert($a, ['w' => 0]);

return (string)$id;
return (string)$a['_id'];
}
}
4 changes: 2 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): string
public function save(array $data, string $id = null): string
{
$main = $data['profile']['main()'];

Expand All @@ -25,7 +25,7 @@ public function save(array $data): string
$sec = $ts['sec'];
$usec = $ts['usec'];

$id = $data['_id'] ?? Util::generateId();
$id = $id ?? Util::generateId();
$this->db->saveProfile([
'id' => $id,
'profile' => json_encode($data['profile']),
Expand Down
2 changes: 1 addition & 1 deletion src/Xhgui/Saver/SaverInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ interface SaverInterface
/**
* Returns id of the saved profile
*/
public function save(array $data): string;
public function save(array $data, string $id = null): string;
}
2 changes: 1 addition & 1 deletion tests/Saver/MongoTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public function testSave()
$saver = new MongoSaver($collection);

foreach ($data as $profile) {
$saver->save($profile);
$saver->save($profile, $profile['_id'] ?? null);
}
}
}
9 changes: 1 addition & 8 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,6 @@ function loadFixture(SaverInterface $saver, $file)
{
$data = json_decode(file_get_contents($file), true);
foreach ($data as $record) {
if (isset($record['meta']['request_time'])) {
$time = strtotime($record['meta']['request_time']);
$record['meta']['request_time'] = new MongoDate($time);
}
if (isset($record['_id'])) {
$record['_id'] = new MongoId($record['_id']);
}
$saver->save($record);
$saver->save($record, $record['_id'] ?? null);
}
}