Passed
Push — master ( f85738...3d85c1 )
by Peter
04:52
created

ApiResponseTrait   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 107
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 9
eloc 33
dl 0
loc 107
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A handleUpdateSuccess() 0 7 1
A handleGetSuccess() 0 7 1
A handleCreateSuccess() 0 7 1
A handleNotFound() 0 6 1
A handleListSuccess() 0 13 2
A handleUnauthorized() 0 6 1
A handleNotImplemented() 0 6 1
A handleDeleteSuccess() 0 6 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AbterPhp\Admin\Http\Controllers;
6
7
use AbterPhp\Framework\Domain\Entities\IStringerEntity;
8
use Opulence\Http\Responses\Response;
9
use Opulence\Http\Responses\ResponseHeaders;
10
11
trait ApiResponseTrait
12
{
13
    /**
14
     * @param IStringerEntity $entity
15
     *
16
     * @return Response
17
     */
18
    protected function handleGetSuccess(IStringerEntity $entity): Response
19
    {
20
        $response = new Response();
21
        $response->setStatusCode(ResponseHeaders::HTTP_OK);
22
        $response->setContent($entity->toJSON());
23
24
        return $response;
25
    }
26
27
    /**
28
     * @param array $entities
29
     * @param int   $total
30
     *
31
     * @return Response
32
     */
33
    protected function handleListSuccess(array $entities, int $total): Response
34
    {
35
        $data = [];
36
        foreach ($entities as $entity) {
37
            $data[] = $entity->toJSON();
38
        }
39
        $content = sprintf('{"total":%d,"data":[%s]}', $total, implode(',', $data));
40
41
        $response = new Response();
42
        $response->setStatusCode(ResponseHeaders::HTTP_OK);
43
        $response->setContent($content);
44
45
        return $response;
46
    }
47
48
    /**
49
     * @param IStringerEntity $entity
50
     *
51
     * @return Response
52
     */
53
    protected function handleCreateSuccess(IStringerEntity $entity): Response
54
    {
55
        $response = new Response();
56
        $response->setStatusCode(ResponseHeaders::HTTP_CREATED);
57
        $response->setContent($entity->toJSON());
58
59
        return $response;
60
    }
61
62
    /**
63
     * @param IStringerEntity $entity
64
     *
65
     * @return Response
66
     */
67
    protected function handleUpdateSuccess(IStringerEntity $entity): Response
68
    {
69
        $response = new Response();
70
        $response->setStatusCode(ResponseHeaders::HTTP_OK);
71
        $response->setContent($entity->toJSON());
72
73
        return $response;
74
    }
75
76
    /**
77
     * @return Response
78
     */
79
    protected function handleDeleteSuccess(): Response
80
    {
81
        $response = new Response();
82
        $response->setStatusCode(ResponseHeaders::HTTP_NO_CONTENT);
83
84
        return $response;
85
    }
86
87
    /**
88
     * @return Response
89
     */
90
    protected function handleNotFound(): Response
91
    {
92
        $response = new Response();
93
        $response->setStatusCode(ResponseHeaders::HTTP_NOT_FOUND);
94
95
        return $response;
96
    }
97
98
    /**
99
     * @return Response
100
     */
101
    protected function handleUnauthorized(): Response
102
    {
103
        $response = new Response();
104
        $response->setStatusCode(ResponseHeaders::HTTP_UNAUTHORIZED);
105
106
        return $response;
107
    }
108
109
    /**
110
     * @return Response
111
     */
112
    protected function handleNotImplemented(): Response
113
    {
114
        $response = new Response();
115
        $response->setStatusCode(ResponseHeaders::HTTP_NOT_IMPLEMENTED);
116
117
        return $response;
118
    }
119
}
120