Skip to content

Commit 8c5ec43

Browse files
committed
add built-in jobs for responding to HTTP requests
1 parent 1061314 commit 8c5ec43

File tree

3 files changed

+88
-0
lines changed

3 files changed

+88
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace Lucid\Domains\Http\Jobs;
4+
5+
use Lucid\Units\Job;
6+
use Illuminate\Routing\ResponseFactory;
7+
8+
class RespondWithJsonErrorJob extends Job
9+
{
10+
public function __construct($message = 'An error occurred', $code = 400, $status = 400, $headers = [], $options = 0)
11+
{
12+
$this->content = [
13+
'status' => $status,
14+
'error' => [
15+
'code' => $code,
16+
'message' => $message,
17+
],
18+
];
19+
20+
$this->status = $status;
21+
$this->headers = $headers;
22+
$this->options = $options;
23+
}
24+
25+
public function handle(ResponseFactory $response)
26+
{
27+
return $response->json($this->content, $this->status, $this->headers, $this->options);
28+
}
29+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace Lucid\Domains\Http\Jobs;
4+
5+
use Lucid\Units\Job;
6+
use Illuminate\Routing\ResponseFactory;
7+
8+
class RespondWithJsonJob extends Job
9+
{
10+
protected $status;
11+
protected $content;
12+
protected $headers;
13+
protected $options;
14+
15+
public function __construct($content, $status = 200, array $headers = [], $options = 0)
16+
{
17+
$this->content = $content;
18+
$this->status = $status;
19+
$this->headers = $headers;
20+
$this->options = $options;
21+
}
22+
23+
public function handle(ResponseFactory $factory)
24+
{
25+
$response = [
26+
'data' => $this->content,
27+
'status' => $this->status,
28+
];
29+
30+
return $factory->json($response, $this->status, $this->headers, $this->options);
31+
}
32+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace Lucid\Domains\Http\Jobs;
4+
5+
use Lucid\Units\Job;
6+
use Illuminate\Routing\ResponseFactory;
7+
8+
class RespondWithViewJob extends Job
9+
{
10+
protected $status;
11+
protected $data;
12+
protected $headers;
13+
protected $template;
14+
15+
public function __construct($template, $data = [], $status = 200, array $headers = [])
16+
{
17+
$this->template = $template;
18+
$this->data = $data;
19+
$this->status = $status;
20+
$this->headers = $headers;
21+
}
22+
23+
public function handle(ResponseFactory $factory)
24+
{
25+
return $factory->view($this->template, $this->data, $this->status, $this->headers);
26+
}
27+
}

0 commit comments

Comments
 (0)