Completed
Push — feature/middleware ( 5df78a...fa2898 )
by Derek Stephen
03:13
created

DragonApiController::deleteAction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
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\Form\DragonForm;
7
use BoneMvc\Module\Dragon\Service\DragonService;
8
use Psr\Http\Message\ResponseInterface;
9
use Psr\Http\Message\ServerRequestInterface;
10
use Zend\Diactoros\Response\JsonResponse;
11
12
class DragonApiController
13
{
14
    /** @var DragonService $service */
15
    private $service;
16
17
    /**
18
     * DragonController constructor.
19
     * @param DragonService $service
20
     */
21
    public function __construct(DragonService $service)
22
    {
23
        $this->service = $service;
24
    }
25
26
    /**
27
     * Controller.
28
     *
29
     * @param \Psr\Http\Message\ServerRequestInterface $request
30
     *
31
     * @return \Psr\Http\Message\ResponseInterface
32
     */
33
    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...
Unused Code introduced by
The parameter $args 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...
34
    {
35
        $db = $this->service->getRepository();
36
        $dragons = new DragonCollection($db->findAll());
37
38
        return new JsonResponse($dragons->toArray());
39
    }
40
41
    /**
42
     * @param ServerRequestInterface $request
43
     * @param array $args
44
     * @return ResponseInterface
45
     * @throws \Doctrine\ORM\ORMException
46
     * @throws \Doctrine\ORM\OptimisticLockException
47
     */
48
    public function createAction(ServerRequestInterface $request, array $args) : ResponseInterface
0 ignored issues
show
Unused Code introduced by
The parameter $args 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...
49
    {
50
        $post = json_decode($request->getBody()->getContents(), true) ?: $request->getParsedBody();
51
        $form = new DragonForm('create');
52
        $form->populate($post);
53
54 View Code Duplication
        if ($form->isValid()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
55
            $data = $form->getValues();
56
            $dragon = $this->service->createFromArray($data);
57
            $this->service->saveDragon($dragon);
58
59
            return new JsonResponse($dragon->toArray());
60
        }
61
62
        return new JsonResponse([
63
            'error' => [
64
                'error_messages' => $form->render(),
65
            ],
66
        ]);
67
    }
68
69
    /**
70
     * @param ServerRequestInterface $request
71
     * @param array $args
72
     * @return ResponseInterface
73
     * @throws \Doctrine\ORM\EntityNotFoundException
74
     */
75
    public function viewAction(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...
76
    {
77
        $dragon = $this->service->getRepository()->find($args['id']);
78
79
        return new JsonResponse($dragon->toArray());
80
    }
81
82
    /**
83
     * @param ServerRequestInterface $request
84
     * @param array $args
85
     * @return ResponseInterface
86
     * @throws \Doctrine\ORM\EntityNotFoundException
87
     * @throws \Doctrine\ORM\ORMException
88
     * @throws \Doctrine\ORM\OptimisticLockException
89
     */
90
    public function updateAction(ServerRequestInterface $request, array $args) : ResponseInterface
91
    {
92
        $db = $this->service->getRepository();
93
        $dragon = $db->find($args['id']);
94
95
        $post = json_decode($request->getBody()->getContents(), true) ?: $request->getParsedBody();
96
        $form = new DragonForm('update');
97
        $form->populate($post);
98
99 View Code Duplication
        if ($form->isValid()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
100
            $data = $form->getValues();
101
            $dragon = $this->service->updateFromArray($dragon, $data);
102
            $this->service->saveDragon($dragon);
103
104
            return new JsonResponse($dragon->toArray());
105
        }
106
107
        return new JsonResponse([
108
            'error' => [
109
                'error_messages' => $form->render(),
110
            ],
111
        ]);
112
    }
113
114
    /**
115
     * @param ServerRequestInterface $request
116
     * @param array $args
117
     * @return ResponseInterface
118
     * @throws \Doctrine\ORM\EntityNotFoundException
119
     * @throws \Doctrine\ORM\ORMException
120
     * @throws \Doctrine\ORM\OptimisticLockException
121
     */
122
    public function deleteAction(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...
123
    {
124
        $db = $this->service->getRepository();
125
        $dragon = $db->find($args['id']);
126
        $this->service->deleteDragon($dragon);
127
        return new JsonResponse(['deleted' => true]);
128
    }
129
}