Completed
Pull Request — master (#162)
by
unknown
03:18
created

HistoryController::cgetAction()   B

Complexity

Conditions 5
Paths 12

Size

Total Lines 97
Code Lines 60

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 48
CRAP Score 5.2

Importance

Changes 0
Metric Value
cc 5
eloc 60
nc 12
nop 1
dl 0
loc 97
ccs 48
cts 60
cp 0.8
crap 5.2
rs 8.183
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace AppBundle\Controller;
4
5
use AppBundle\Model\Link;
6
use AppBundle\Model\PaginationLinks;
7
use FOS\RestBundle\Request\ParamFetcher;
8
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
9
use FOS\RestBundle\Controller\Annotations\View as RestView;
10
use FOS\RestBundle\Controller\Annotations\RouteResource;
11
use FOS\RestBundle\Controller\Annotations\QueryParam;
12
use Nelmio\ApiDocBundle\Annotation\ApiDoc;
13
use AppBundle\Model\HistoryResponse;
14
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
15
16
/**
17
 * @RouteResource("History")
18
 */
19
class HistoryController extends Controller
20
{
21
    /**
22
     * @ApiDoc(
23
     *  resource=true,
24
     *  description="Returns a collection of History",
25
     *  statusCodes={
26
     *      200="Returned when all parameters were correct",
27
     *      404="Returned when the entities with given limit and offset are not found",
28
     *  },
29
     *  output = "array<AppBundle\Model\HistoryResponse>"
30
     * )
31
     *
32
     * @QueryParam(name="limit", requirements="\d+", default="10", description="Count entries at one page")
33
     * @QueryParam(name="page", requirements="\d+", default="1", description="Number of page to be shown")
34
     * @QueryParam(
35
     *     name="locale",
36
     *     requirements="^[a-zA-Z]+",
37
     *     default="uk",
38
     *     description="Selects language of data you want to receive"
39
     * )
40
     *
41
     * @RestView
42
     */
43 1
    public function cgetAction(ParamFetcher $paramFetcher)
44
    {
45 1
        $em = $this->getDoctrine()->getManager();
46
47 1
        $history = $em->getRepository('AppBundle:History')
48 1
            ->findAllHistory(
49 1
                $paramFetcher->get('limit'),
50 1
                ($paramFetcher->get('page')-1) * $paramFetcher->get('limit')
51
            )
52
        ;
53
54 1
        $historyTranslated = [];
55
56 1
        foreach ($history as $hist) {
57 1
            $hist->setLocale($paramFetcher->get('locale'));
58 1
            $em->refresh($hist);
59
60 1
            if ($hist->getTranslations()) {
61 1
                $hist->unsetTranslations();
62
            }
63
64 1
            $historyTranslated[] = $hist;
65
        }
66
67 1
        $history = $historyTranslated;
68
69 1
        $historyResponse = new HistoryResponse();
70 1
        $historyResponse->setHistory($history);
71 1
        $historyResponse->setTotalCount(
72 1
            $this->getDoctrine()->getManager()->getRepository('AppBundle:History')->getCount()
73
        );
74 1
        $historyResponse->setPageCount(ceil($historyResponse->getTotalCount() / $paramFetcher->get('limit')));
75 1
        $historyResponse->setPage($paramFetcher->get('page'));
76
77 1
        $self = $this->generateUrl(
78 1
            'get_histories',
79
            [
80 1
                'locale' => $paramFetcher->get('locale'),
81 1
                'limit' => $paramFetcher->get('limit'),
82 1
                'page' => $paramFetcher->get('page'),
83
            ],
84 1
            UrlGeneratorInterface::ABSOLUTE_URL
85
        );
86
87 1
        $first = $this->generateUrl(
88 1
            'get_histories',
89
            [
90 1
                'locale' => $paramFetcher->get('locale'),
91 1
                'limit' => $paramFetcher->get('limit'),
92
            ],
93 1
            UrlGeneratorInterface::ABSOLUTE_URL
94
        );
95
96 1
        $nextPage = $paramFetcher->get('page') < $historyResponse->getPageCount() ?
97
            $this->generateUrl(
98
                'get_histories',
99
                [
100
                    'locale' => $paramFetcher->get('locale'),
101
                    'limit' => $paramFetcher->get('limit'),
102
                    'page' => $paramFetcher->get('page')+1,
103
                ],
104
                true
0 ignored issues
show
Documentation introduced by
true is of type boolean, but the function expects a integer.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
105
            ) :
106 1
            'false';
107
108 1
        $previsiousPage = $paramFetcher->get('page') > 1 ?
109
            $this->generateUrl(
110
                'get_histories',
111
                [
112
                    'locale' => $paramFetcher->get('locale'),
113
                    'limit' => $paramFetcher->get('limit'),
114
                    'page' => $paramFetcher->get('page')-1,
115
                ],
116
                UrlGeneratorInterface::ABSOLUTE_URL
117
            ) :
118 1
            'false';
119
120 1
        $last = $this->generateUrl(
121 1
            'get_histories',
122
            [
123 1
                'locale' => $paramFetcher->get('locale'),
124 1
                'limit' => $paramFetcher->get('limit'),
125 1
                'page' => $historyResponse->getPageCount(),
126
            ],
127 1
            UrlGeneratorInterface::ABSOLUTE_URL
128
        );
129
130 1
        $links = new PaginationLinks();
131
132 1
        $historyResponse->setLinks($links->setSelf(new Link($self)));
133 1
        $historyResponse->setLinks($links->setFirst(new Link($first)));
134 1
        $historyResponse->setLinks($links->setNext(new Link($nextPage)));
135 1
        $historyResponse->setLinks($links->setPrev(new Link($previsiousPage)));
136 1
        $historyResponse->setLinks($links->setLast(new Link($last)));
137
138 1
        return $historyResponse;
139
    }
140
141
    /**
142
     * @ApiDoc(
143
     *  resource=true,
144
     *  description="Returns an History by unique property {slug}",
145
     *  statusCodes={
146
     *      200="Returned when History by {slug} was found",
147
     *      404="Returned when History by {slug} was not found",
148
     *  },
149
     *  output = "AppBundle\Entity\History"
150
     * )
151
     *
152
     * @QueryParam(
153
     *     name="locale",
154
     *     requirements="^[a-zA-Z]+",
155
     *     default="uk",
156
     *     description="Selects language of data you want to receive"
157
     * )
158
     *
159
     * @RestView
160
     */
161 1
    public function getAction(ParamFetcher $paramFetcher, $slug)
162
    {
163 1
        $em = $this->getDoctrine()->getManager();
164
165
        $history = $em
166 1
            ->getRepository('AppBundle:History')->findOneByslug($slug);
167
168 1
        if (!$history) {
169 1
            throw $this->createNotFoundException('Unable to find '.$slug.' entity');
170
        }
171
172 1
        $history->setLocale($paramFetcher->get('locale'));
173 1
        $em->refresh($history);
174
175 1
        if ($history->getTranslations()) {
176 1
            $history->unsetTranslations();
177
        }
178
179 1
        return $history;
180
    }
181
}
182