Completed
Push — feature/middleware ( e85030...d864e0 )
by Derek Stephen
05:41
created

OrcApiController   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 126
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 5
dl 126
loc 126
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 4 4 1
A indexAction() 19 19 2
A createAction() 18 18 3
A viewAction() 6 6 1
A updateAction() 21 21 3
A deleteAction() 8 8 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace BoneMvc\Module\Orc\Controller;
6
7
use BoneMvc\Module\Orc\Collection\OrcCollection;
8
use BoneMvc\Module\Orc\Form\OrcForm;
9
use BoneMvc\Module\Orc\Service\OrcService;
10
use League\Route\Http\Exception\NotFoundException;
11
use Psr\Http\Message\ResponseInterface;
12
use Psr\Http\Message\ServerRequestInterface;
13
use Zend\Diactoros\Response\JsonResponse;
14
15 View Code Duplication
class OrcApiController
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in 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...
16
{
17
    /** @param OrcService $service */
18
    private $service;
19
20
    /**
21
     * @param OrcService $service
22
     */
23
    public function __construct(OrcService $service)
24
    {
25
        $this->service = $service;
26
    }
27
28
    /**
29
     * @param ServerRequestInterface $request
30
     * @param array $args
31
     * @return ResponseInterface
32
     * @throws NotFoundException
33
     * @throws \Doctrine\ORM\NonUniqueResultException
34
     */
35
    public function indexAction(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...
36
    {
37
        $params = $request->getQueryParams();
38
        $limit = $params['limit'];
39
        $offset = $params['offset'];
40
        $db = $this->service->getRepository();
41
        $orcs = new OrcCollection($db->findBy([], null, $limit, $offset));
42
        $total = $db->getTotalOrcCount();
43
        $count = count($orcs);
44
        if ($count < 1) {
45
            throw new NotFoundException();
46
        }
47
48
        $payload['_embedded'] = $orcs->toArray();
0 ignored issues
show
Coding Style Comprehensibility introduced by
$payload was never initialized. Although not strictly required by PHP, it is generally a good practice to add $payload = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
49
        $payload['count'] = $count;
50
        $payload['total'] = $total;
51
52
        return new JsonResponse($payload);
53
    }
54
55
    /**
56
     * @param ServerRequestInterface $request
57
     * @param array $args
58
     * @return ResponseInterface
59
     * @throws \Doctrine\ORM\ORMException
60
     * @throws \Doctrine\ORM\OptimisticLockException
61
     */
62
    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...
63
    {
64
        $post = json_decode($request->getBody()->getContents(), true) ?: $request->getParsedBody();
65
        $form = new OrcForm('create');
66
        $form->populate($post);
67
68
        if ($form->isValid()) {
69
            $data = $form->getValues();
70
            $orc = $this->service->createFromArray($data);
71
            $this->service->saveOrc($orc);
72
73
            return new JsonResponse($orc->toArray());
74
        }
75
76
        return new JsonResponse([
77
            'error' => $form->getErrorMessages(),
78
        ]);
79
    }
80
81
    /**
82
     * @param ServerRequestInterface $request
83
     * @param array $args
84
     * @return ResponseInterface
85
     * @throws \Doctrine\ORM\EntityNotFoundException
86
     */
87
    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...
88
    {
89
        $orc = $this->service->getRepository()->find($args['id']);
90
91
        return new JsonResponse($orc->toArray());
92
    }
93
94
    /**
95
     * @param ServerRequestInterface $request
96
     * @param array $args
97
     * @return ResponseInterface
98
     * @throws \Doctrine\ORM\EntityNotFoundException
99
     * @throws \Doctrine\ORM\ORMException
100
     * @throws \Doctrine\ORM\OptimisticLockException
101
     */
102
    public function updateAction(ServerRequestInterface $request, array $args): ResponseInterface
103
    {
104
        $db = $this->service->getRepository();
105
        $orc = $db->find($args['id']);
106
107
        $post = json_decode($request->getBody()->getContents(), true) ?: $request->getParsedBody();
108
        $form = new OrcForm('update');
109
        $form->populate($post);
110
111
        if ($form->isValid()) {
112
            $data = $form->getValues();
113
            $orc = $this->service->updateFromArray($orc, $data);
114
            $this->service->saveOrc($orc);
115
116
            return new JsonResponse($orc->toArray());
117
        }
118
119
        return new JsonResponse([
120
            'error' => $form->getErrorMessages(),
121
        ]);
122
    }
123
124
    /**
125
     * @param ServerRequestInterface $request
126
     * @param array $args
127
     * @return ResponseInterface
128
     * @throws \Doctrine\ORM\EntityNotFoundException
129
     * @throws \Doctrine\ORM\ORMException
130
     * @throws \Doctrine\ORM\OptimisticLockException
131
     */
132
    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...
133
    {
134
        $db = $this->service->getRepository();
135
        $orc = $db->find($args['id']);
136
        $this->service->deleteOrc($orc);
137
138
        return new JsonResponse(['deleted' => true]);
139
    }
140
}
141