Passed
Push — master ( 5ad05b...a04153 )
by Koen
04:32
created

ResponseFactory::redirectTo()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 3
dl 0
loc 3
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\Http\Responses;
6
7
use App\Contracts\Http\Responses\ResponseFactory as ResponseFactoryContract;
8
use Illuminate\Http\JsonResponse;
9
use Illuminate\Http\RedirectResponse;
10
use Illuminate\Http\Response;
11
use Illuminate\Pagination\LengthAwarePaginator;
12
use Illuminate\Routing\Redirector;
13
use Symfony\Component\HttpFoundation\StreamedResponse;
14
use function array_key_exists;
15
16
final class ResponseFactory implements ResponseFactoryContract
17
{
18
    private Redirector $redirector;
19
20
    public function __construct(Redirector $redirector)
21
    {
22
        $this->redirector = $redirector;
23
    }
24
25
    public function make(string $content = '', int $status = Response::HTTP_OK, array $headers = []): Response
26
    {
27
        return new Response($content, $status, $headers);
28
    }
29
30
    public function noContent(int $status = Response::HTTP_NO_CONTENT, array $headers = []): Response
31
    {
32
        return $this->make('', $status, $headers);
33
    }
34
35
    public function json(
36
        array $data = [],
37
        int $status = Response::HTTP_OK,
38
        array $headers = [],
39
        int $options = 0
40
    ): JsonResponse {
41
        if (! array_key_exists('data', $data)) {
42
            $data = ['data' => $data];
43
        }
44
45
        return new JsonResponse($data, $status, $headers, $options);
46
    }
47
48
    public function paginator(
49
        LengthAwarePaginator $paginator,
50
        int $status = Response::HTTP_OK,
51
        array $headers = [],
52
        int $options = 0
53
    ): JsonResponse {
54
        return $this->json([
55
            'data' => $paginator->items(),
56
            'meta' => [
57
                'current_page' => $paginator->currentPage(),
58
                'last_page'    => $paginator->lastPage(),
59
                'from'         => $paginator->firstItem(),
60
                'to'           => $paginator->lastItem(),
61
                'total'        => $paginator->total(),
62
                'per_page'     => $paginator->perPage(),
63
            ],
64
        ]);
65
    }
66
67
    public function mappedPaginator(
68
        LengthAwarePaginator $paginator,
69
        callable $map,
70
        int $status = Response::HTTP_OK,
71
        array $headers = [],
72
        int $options = 0
73
    ): JsonResponse {
74
        $paginator->setCollection($paginator->getCollection()->map($map));
75
76
        return $this->paginator($paginator, $status, $headers, $options);
77
    }
78
79
    public function stream(callable $callback, int $status = Response::HTTP_OK, array $headers = []): StreamedResponse
80
    {
81
        return new StreamedResponse($callback, $status, $headers);
82
    }
83
84
    public function redirectTo(string $path, int $status = Response::HTTP_FOUND, array $headers = []): RedirectResponse
85
    {
86
        return $this->redirector->to($path, $status, $headers, true);
87
    }
88
}
89