Completed
Push — feature/middleware ( a35412...bb427e )
by Derek Stephen
03:50
created

DragonController   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 3
dl 0
loc 81
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A create() 0 15 2
A read() 0 3 1
A update() 0 3 1
A delete() 0 3 1
A getJsonPost() 0 4 1
A jsonResponse() 0 8 1
1
<?php
2
3
namespace BoneMvc\Module\Dragon\Controller;
4
5
use BoneMvc\Module\Dragon\Entity\Dragon;
6
use BoneMvc\Module\Dragon\Form\DragonForm;
7
use BoneMvc\Module\Dragon\Service\DragonService;
8
use Psr\Http\Message\RequestInterface;
9
use Psr\Http\Message\ResponseInterface;
10
11
class DragonController
12
{
13
    /** @var DragonService */
14
    public $service;
15
16
    /**
17
     * @param DragonService $service
18
     */
19
    public function __construct(DragonService $service)
20
    {
21
        $this->service = $service;
22
    }
23
24
    /**
25
     * @param RequestInterface $request
26
     * @return ResponseInterface $response
27
     * @throws \Doctrine\ORM\OptimisticLockException
28
     * @throws \Doctrine\ORM\ORMException
29
     */
30
    public function create(RequestInterface $request, ResponseInterface $response): ResponseInterface
31
    {
32
        $post = $this->getJsonPost($request);
33
        $form = new DragonForm('create');
34
        $form->populate($post);
35
        if ($form->isValid()) {
36
            $data = $form->getValues();
37
            $dragon = $this->service->createFromArray($data);
38
            $this->service->saveDragon($dragon);
39
40
            return $this->jsonResponse($response, $dragon->toArray());
41
        } else {
42
            // handle errors
43
        }
44
    }
45
46
    /**
47
     * @param RequestInterface $request
48
     * @return ResponseInterface $response
49
     */
50
    public function read(RequestInterface $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...
51
    {
52
    }
53
54
    /**
55
     * @param RequestInterface $request
56
     * @return ResponseInterface $response
57
     */
58
    public function update(RequestInterface $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...
59
    {
60
    }
61
62
    /**
63
     * @param RequestInterface $request
64
     * @return ResponseInterface $response
65
     */
66
    public function delete(RequestInterface $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...
67
    {
68
    }
69
70
    /**
71
     * @param RequestInterface $request
72
     * @return array
73
     */
74
    protected function getJsonPost(RequestInterface $request): array
75
    {
76
        return json_decode($request->getBody()->getContents(), true);
77
    }
78
79
    /**
80
     * @param array $data
81
     * @return ResponseInterface $response
82
     */
83
    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...
84
    {
85
        $json = json_encode($data);
86
        // create proper $response later
87
        header('Content-Type: application/json');
88
        echo $json;
89
        exit;
90
    }
91
}
92