Completed
Push — feature/middleware ( 4ff047...9d8017 )
by Derek Stephen
03:15
created

DragonApiController::jsonResponse()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php declare(strict_types=1);
2
3
namespace BoneMvc\Module\Dragon\Controller;
4
5
use BoneMvc\Module\Dragon\Collection\DragonCollection;
6
use BoneMvc\Module\Dragon\Entity\Dragon;
7
use BoneMvc\Module\Dragon\Form\DragonForm;
8
use BoneMvc\Module\Dragon\Service\DragonService;
9
use Psr\Http\Message\ResponseInterface;
10
use Psr\Http\Message\ServerRequestInterface;
11
use Zend\Diactoros\Response\JsonResponse;
12
use Zend\Diactoros\Response\HtmlResponse;
13
14
class DragonApiController
15
{
16
    /** @var DragonService $service */
17
    private $service;
18
19
    /**
20
     * DragonController constructor.
21
     * @param DragonService $service
22
     */
23
    public function __construct(DragonService $service)
24
    {
25
        $this->service = $service;
26
    }
27
28
    /**
29
     * Controller.
30
     *
31
     * @param \Psr\Http\Message\ServerRequestInterface $request
32
     *
33
     * @return \Psr\Http\Message\ResponseInterface
34
     */
35
    public function indexAction(ServerRequestInterface $request, array $args) : ResponseInterface
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
36
    {
37
38
        $dragons = $this->service->getRepository();
39
40
        if (isset($args['id'])) {
41
            $id = $args['id'];
42
            /** @var Dragon $data */
43
            $data = $this->service->getRepository()->find($id);
44
        } else {
45
            $data = new DragonCollection($dragons->findAll());
46
        }
47
        $response = new JsonResponse($data->toArray());
48
49
        return $response;
50
    }
51
52
    /**
53
     * @param ServerRequestInterface $request
54
     * @return ResponseInterface $response
55
     * @throws \Doctrine\ORM\OptimisticLockException
56
     * @throws \Doctrine\ORM\ORMException
57
     */
58
    public function create(ServerRequestInterface $request, ResponseInterface $response): ResponseInterface
59
    {
60
        $post = $this->getJsonPost($request);
61
        $form = new DragonForm('create');
62
        $form->populate($post);
63
        if ($form->isValid()) {
64
            $data = $form->getValues();
65
            $dragon = $this->service->createFromArray($data);
66
            $this->service->saveDragon($dragon);
67
68
            return $this->jsonResponse($response, $dragon->toArray());
69
        } else {
70
            // handle errors
71
        }
72
    }
73
74
    /**
75
     * @param ServerRequestInterface $request
76
     * @return ResponseInterface $response
77
     */
78
    public function read(ServerRequestInterface $request, ResponseInterface $response): ResponseInterface
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $response is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
79
    {
80
81
    }
82
83
    /**
84
     * @param ServerRequestInterface $request
85
     * @return ResponseInterface $response
86
     */
87
    public function update(ServerRequestInterface $request, ResponseInterface $response): ResponseInterface
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $response is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
88
    {
89
    }
90
91
    /**
92
     * @param ServerRequestInterface $request
93
     * @return ResponseInterface $response
94
     */
95
    public function delete(ServerRequestInterface $request, ResponseInterface $response): ResponseInterface
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $response is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
96
    {
97
    }
98
99
    /**
100
     * @param ServerRequestInterface $request
101
     * @return array
102
     */
103
    protected function getJsonPost(ServerRequestInterface $request): array
104
    {
105
        return json_decode($request->getParsedBody(), true);
106
    }
107
108
    /**
109
     * @param array $data
110
     * @return ResponseInterface $response
111
     */
112
    public function jsonResponse(ResponseInterface $response, array $data): ResponseInterface
0 ignored issues
show
Unused Code introduced by
The parameter $response is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
113
    {
114
        $json = json_encode($data);
115
        // create proper $response later
116
        header('Content-Type: application/json');
117
        echo $json;
118
        exit;
119
    }
120
}