Completed
Push — master ( 3ac99f...7a6ebb )
by Pierre
31:51 queued 29:27
created

Restful::setResponse()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 10
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 16
ccs 10
cts 10
cp 1
crap 1
rs 9.9332
1
<?php
2
3
namespace App\Controllers\Api\V1;
4
5
use App\Interfaces\Controllers\IApi;
6
use App\Reuse\Controllers\AbstractApi;
7
use App\Http\Response;
8
use App\Container;
9
10
final class Restful extends AbstractApi implements IApi
11
{
12
13
    /**
14
     * instanciate
15
     *
16
     * @param Container $container
17
     */
18 6
    public function __construct(Container $container)
19
    {
20 6
        parent::__construct($container);
21
    }
22
23
    /**
24
     * index
25
     *
26
     * @Request.method GET
27
     * @return Restful
28
     */
29 1
    final public function index(): Restful
30
    {
31 1
        return $this->setResponse(__CLASS__, __FUNCTION__);
32
    }
33
34
    /**
35
     * store
36
     *
37
     * @Request.method POST
38
     * @return Restful
39
     */
40 1
    final public function store(): Restful
41
    {
42 1
        return $this->setResponse(__CLASS__, __FUNCTION__);
43
    }
44
45
    /**
46
     * update
47
     *
48
     * @Request.method PUT/PATCH
49
     * @return Restful
50
     */
51 1
    final public function update(): Restful
52
    {
53 1
        return $this->setResponse(__CLASS__, __FUNCTION__);
54
    }
55
56
    /**
57
     * delete
58
     *
59
     * @Request.method DELETE
60
     * @return Restful
61
     */
62 1
    final public function delete(): Restful
63
    {
64 1
        return $this->setResponse(__CLASS__, __FUNCTION__);
65
    }
66
67
    /**
68
     * set response with for a classname and action
69
     *
70
     * @param string $classname
71
     * @param string $action
72
     * @return Restful
73
     */
74 1
    protected function setResponse(string $classname, string $action): Restful
75
    {
76 1
        $this->response
77 1
            ->setCode(Response::HTTP_OK)
78 1
            ->setContent(
79
                [
80 1
                    'error' => false,
81
                    'datas' => [
82 1
                        'method' => $this->request->getMethod(),
83 1
                        'params' => $this->request->getParams(),
84 1
                        'controller' => $classname,
85 1
                        'action' => $action,
86
                    ]
87
                ]
88
            );
89 1
        return $this;
90
    }
91
}
92