Skip to content

Commit 1e07a11

Browse files
committed
✨ actions
1 parent 5aee8b9 commit 1e07a11

26 files changed

+1127
-17
lines changed

README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,12 +84,14 @@ TODO
8484

8585
## Contributing
8686

87-
8887
TODO
8988

9089
## Roadmap for the end of bêta (Estimated delivery October 2023)
9190

9291
- Custom directives (Filters / sorting)
93-
- Actions / Metrics
9492
- Automatic documentation with extension possible
95-
- Refactor the response class
93+
94+
## Roadmap
95+
96+
- Metrics
97+
- Refactor the response class

src/Actions/Action.php

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
<?php
2+
3+
namespace Lomkit\Rest\Actions;
4+
5+
use Illuminate\Bus\Queueable;
6+
use Illuminate\Support\Collection;
7+
use Illuminate\Support\Str;
8+
use Lomkit\Rest\Concerns\Makeable;
9+
use Lomkit\Rest\Http\Requests\OperateRequest;
10+
use Lomkit\Rest\Http\Requests\RestRequest;
11+
12+
class Action implements \JsonSerializable
13+
{
14+
use Makeable;
15+
16+
/**
17+
* The name of the connection the job should be sent to.
18+
*
19+
* @var string|null
20+
*/
21+
public $connection;
22+
23+
/**
24+
* The name of the queue the job should be sent to.
25+
*
26+
* @var string|null
27+
*/
28+
public $queue;
29+
30+
/**
31+
* The displayable name of the action.
32+
*
33+
* @var string
34+
*/
35+
public $name;
36+
37+
/**
38+
* The number of models that should be included in each chunk.
39+
*
40+
* @var int
41+
*/
42+
public $chunkCount = 100;
43+
44+
/**
45+
* Get the name of the action.
46+
*
47+
* @return string
48+
*/
49+
public function name()
50+
{
51+
return $this->name ?: Str::of(class_basename(get_class($this)))->beforeLast('Action')->snake(' ')->title();
52+
}
53+
54+
/**
55+
* Get the URI key for the action.
56+
*
57+
* @return string
58+
*/
59+
public function uriKey()
60+
{
61+
return Str::slug($this->name(), '-', null);
62+
}
63+
64+
/**
65+
* Prepare the action for JSON serialization.
66+
*
67+
* @return array<string, mixed>
68+
*/
69+
public function jsonSerialize(): array
70+
{
71+
$request = app()->make(RestRequest::class);
72+
73+
return [
74+
'name' => $this->name(),
75+
'uriKey' => $this->uriKey(),
76+
'fields' => $this->fields($request)
77+
];
78+
}
79+
80+
/**
81+
* Perform the action on the given models.
82+
*
83+
* @param array $fields
84+
* @param \Illuminate\Support\Collection $models
85+
* @return mixed
86+
*/
87+
public function handle(array $fields, \Illuminate\Support\Collection $models)
88+
{
89+
//
90+
}
91+
92+
/**
93+
* Called in an action failed.
94+
*
95+
* @param \Exception $exception
96+
* @param array $fields
97+
* @param \Illuminate\Support\Collection $models
98+
* @return mixed
99+
*/
100+
public function failed(array $fields, Collection $models, $exception)
101+
{
102+
//
103+
}
104+
105+
/**
106+
* Called in an action failed.
107+
*
108+
* @param \Lomkit\Rest\Http\Requests\RestRequest $request
109+
* @return array
110+
*/
111+
public function fields(\Lomkit\Rest\Http\Requests\RestRequest $request)
112+
{
113+
return [];
114+
}
115+
116+
/**
117+
* Execute the action for the given request.
118+
*
119+
* @param OperateRequest $request
120+
* @return int
121+
*
122+
* @throws \Throwable
123+
*/
124+
public function handleRequest(OperateRequest $request)
125+
{
126+
$fields = $request->resolveFields($this);
127+
128+
$dispatcher = new DispatchAction($request, $this, $fields);
129+
130+
$count = $dispatcher->dispatch($this->chunkCount);
131+
132+
return $count;
133+
}
134+
}

src/Actions/Actionable.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace Lomkit\Rest\Actions;
4+
5+
use Lomkit\Rest\Http\Requests\RestRequest;
6+
7+
trait Actionable
8+
{
9+
/**
10+
* The actions that should be linked
11+
* @param RestRequest $request
12+
* @return array
13+
*/
14+
public function actions(RestRequest $request): array {
15+
return [];
16+
}
17+
18+
public function actionExists(RestRequest $request, string $actionKey): bool {
19+
return collect($this->actions($request))
20+
->contains(function (Action $action) use ($actionKey) {
21+
return $action->uriKey() === $actionKey;
22+
});
23+
}
24+
25+
public function action(RestRequest $request, string $actionKey): Action {
26+
return collect($this->actions($request))
27+
->sole(function (Action $action) use ($actionKey) {
28+
return $action->uriKey() === $actionKey;
29+
});
30+
}
31+
}

src/Actions/CallRestApiAction.php

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
<?php
2+
3+
namespace Lomkit\Rest\Actions;
4+
5+
use Illuminate\Bus\Queueable;
6+
use Illuminate\Foundation\Bus\Dispatchable;
7+
use Illuminate\Queue\InteractsWithQueue;
8+
use Illuminate\Queue\SerializesModels;
9+
use Illuminate\Support\Collection;
10+
11+
class CallRestApiAction
12+
{
13+
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
14+
15+
/**
16+
* Models collection.
17+
*
18+
* @var \Illuminate\Database\Eloquent\Collection|\Illuminate\Support\Collection
19+
*/
20+
protected $models;
21+
22+
/**
23+
* The action instance.
24+
*
25+
* @var \Lomkit\Rest\Actions\Action
26+
*/
27+
protected \Lomkit\Rest\Actions\Action $action;
28+
29+
/**
30+
* The fields for the action instance.
31+
*
32+
* @var array
33+
*/
34+
protected array $fields;
35+
36+
/**
37+
* Create a new job instance.
38+
*
39+
* @param \Lomkit\Rest\Actions\Action $action
40+
* @param array $fields
41+
* @param \Illuminate\Database\Eloquent\Collection|\Illuminate\Support\Collection $models
42+
* @return void
43+
*/
44+
public function __construct(Action $action, array $fields, Collection $models)
45+
{
46+
$this->action = $action;
47+
$this->fields = $fields;
48+
$this->models = $models;
49+
}
50+
51+
/**
52+
* Execute the job.
53+
*
54+
* @return void
55+
*/
56+
public function handle()
57+
{
58+
$this->action->handle($this->fields, $this->models);
59+
}
60+
61+
/**
62+
* Call the failed method on the job instance.
63+
*
64+
* @param \Exception $e
65+
* @return void
66+
*/
67+
public function failed($e)
68+
{
69+
$this->action->failed($this->fields, $this->models, $e);
70+
}
71+
72+
/**
73+
* Get the display name for the queued job.
74+
*
75+
* @return string
76+
*/
77+
public function displayName()
78+
{
79+
return get_class($this->action);
80+
}
81+
}

0 commit comments

Comments
 (0)