Skip to content

Commit

Permalink
Refactor code to make use of new handler parameters unmarshalling
Browse files Browse the repository at this point in the history
  • Loading branch information
thekid committed Dec 23, 2023
1 parent f2072f3 commit 2147069
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 25 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"require": {
"xp-forge/handlebars-templates": "^3.1",
"xp-forge/frontend": "^5.6",
"xp-forge/frontend": "dev-feature/unmarshal as 5.7.0",
"xp-forge/websockets": "^3.0",
"xp-forge/mongodb": "^2.1",
"xp-forge/redis": "^1.0",
Expand Down
13 changes: 7 additions & 6 deletions src/main/php/de/thekid/crews/Events.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php namespace de\thekid\crews;

use com\mongodb\ObjectId;
use io\redis\RedisProtocol;

class Events {
Expand All @@ -10,17 +11,17 @@ public function __construct(private RedisProtocol $redis) { }
public function socket() { return $this->redis->socket(); }

/** Publish an event in a given group */
public function publish(string $group, array<string, mixed> $event): void {
public function publish(string|ObjectId $group, array<string, mixed> $event): void {
$pass= '';
foreach ($event as $key => $value) {
$pass.= '&'.urlencode($key).'='.urlencode($value);
}
$this->redis->command('PUBLISH', $group, substr($pass, 1));
$this->redis->command('PUBLISH', (string)$group, substr($pass, 1));
}

/** Subscribe to updates from a given group */
public function subscribe(string $group): void {
$this->redis->command('SUBSCRIBE', $group);
public function subscribe(string|ObjectId $group): void {
$this->redis->command('SUBSCRIBE', (string)$group);
}

/** Receive updates, yielding group and event */
Expand All @@ -31,7 +32,7 @@ public function receive(): iterable {
}

/** Unsubscribe from updates of a given group */
public function unsubscribe(string $group): void {
$this->redis->command('UNSUBSCRIBE', $group);
public function unsubscribe(string|ObjectId $group): void {
$this->redis->command('UNSUBSCRIBE', (string)$group);
}
}
34 changes: 16 additions & 18 deletions src/main/php/de/thekid/crews/web/Group.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public function __construct(Database $db, private Events $events) {
$this->posts= $db->collection('posts');
}

private function post(ObjectId $id, string $view= 'post') {
private function post(ObjectId $id, string $view= 'post'): View {
return View::named('group')->fragment($view)->with($this->posts
->find($id)
->first()
Expand All @@ -23,30 +23,29 @@ private function post(ObjectId $id, string $view= 'post') {
}

#[Get]
public function index(string $group) {
$id= new ObjectId($group);
$groups= $this->groups->find($id);
public function index(ObjectId $group) {
$groups= $this->groups->find($group);
$posts= $this->posts->aggregate([
['$match' => ['group' => $id]],
['$match' => ['group' => $group]],
['$sort' => ['created' => -1]],
['$limit' => 20],
]);
return View::named('group')->with(['group' => $groups->first(), 'posts' => $posts->all()]);
}

#[Get('/{view}')]
public function group(string $group, string $view) {
public function group(ObjectId $group, string $view) {
return View::named('group')->fragment($view)->with($this->groups
->find(new ObjectId($group))
->find($group)
->first()
->properties()
);
}

#[Put]
public function describe(string $group, #[Param] string $description) {
public function describe(ObjectId $group, #[Param] string $description) {
$result= $this->groups->run('findAndModify', [
'query' => ['_id' => new ObjectId($group)],
'query' => ['_id' => $group],
'update' => ['$set' => ['description' => $description]],
'new' => true, // Return modified document
'upsert' => false,
Expand All @@ -55,9 +54,9 @@ public function describe(string $group, #[Param] string $description) {
}

#[Post('/posts')]
public function create(string $group, #[Param] string $body) {
public function create(ObjectId $group, #[Param] string $body) {
$insert= $this->posts->insert(new Document([
'group' => new ObjectId($group),
'group' => $group,
'body' => $body,
'created' => Date::now(),
]));
Expand All @@ -66,21 +65,20 @@ public function create(string $group, #[Param] string $body) {
}

#[Get('/posts/{id}/{view}')]
public function view(string $group, string $id, string $view) {
return $this->post(new ObjectId($id), $view);
public function view(ObjectId $group, ObjectId $id, string $view) {
return $this->post($id, $view);
}

#[Delete('/posts/{id}')]
public function delete(string $group, string $id) {
$this->posts->delete(new ObjectId($id));
public function delete(ObjectId $group, ObjectId $id) {
$this->posts->delete($id);
$this->events->publish($group, ['delete' => $id]);
return View::empty()->status(204);
}

#[Put('/posts/{id}')]
public function update(string $group, string $id, #[Param] string $body) {
$post= new ObjectId($id);
$this->posts->update($post, ['$set' => [
public function update(ObjectId $group, ObjectId $id, #[Param] string $body) {
$this->posts->update($id, ['$set' => [
'body' => $body,
'updated' => Date::now(),
]]);
Expand Down

0 comments on commit 2147069

Please sign in to comment.