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

PostsController   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 193
Duplicated Lines 10.88 %

Coupling/Cohesion

Components 1
Dependencies 8

Test Coverage

Coverage 92.05%

Importance

Changes 0
Metric Value
dl 21
loc 193
ccs 81
cts 88
cp 0.9205
rs 10
c 0
b 0
f 0
wmc 12
lcom 1
cbo 8

2 Methods

Rating   Name   Duplication   Size   Complexity  
B getAction() 0 31 5
C cgetAction() 21 114 7

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
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\PostsResponse;
14
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
15
16
/**
17
 * @RouteResource("Post")
18
 */
19
class PostsController extends Controller
20
{
21
    /**
22
     * @ApiDoc(
23
     *  resource=true,
24
     *  description="Returns a collection of Posts",
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\PostsResponse>"
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
     * @QueryParam(name="tag", description="You can receive posts by Tag slug, without Tag you will receive all posts")
41
     *
42
     * @RestView
43
     * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
44
     */
45 3
    public function cgetAction(ParamFetcher $paramFetcher)
46
    {
47 3
        $em = $this->getDoctrine()->getManager();
48
49 3
        $posts = $em->getRepository('AppBundle:Post')
50 3
            ->findAllOrByTag(
51 3
                $paramFetcher->get('limit'),
52 3
                ($paramFetcher->get('page')-1) * $paramFetcher->get('limit'),
53 3
                $paramFetcher->get('tag')
54
            )
55
        ;
56
57 3
        $postsTranslated = [];
58
59 3 View Code Duplication
        foreach ($posts as $post) {
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...
60 3
            $post->setLocale($paramFetcher->get('locale'));
61 3
            $em->refresh($post);
62
63 3
            if ($post->getTranslations()) {
64 3
                $post->unsetTranslations();
65
            }
66
67 3
            $tags = $post->getTags();
68
69 3
            foreach ($tags as $tag) {
70 3
                $tag->setLocale($paramFetcher->get('locale'));
71 3
                $em->refresh($tag);
72
73 3
                if ($tag->getTranslations()) {
74 3
                    $tag->unsetTranslations();
75
                }
76
            }
77
78 3
            $postsTranslated[] = $post;
79
        }
80
81 3
        $posts = $postsTranslated;
82
83 3
        $postsResponse = new PostsResponse();
84 3
        $postsResponse->setPosts($posts);
85 3
        $postsResponse->setTotalCount(
86 3
            $this->getDoctrine()->getManager()->getRepository('AppBundle:Post')->getCount($paramFetcher->get('tag'))
87
        );
88 3
        $postsResponse->setPageCount(ceil($postsResponse->getTotalCount() / $paramFetcher->get('limit')));
89 3
        $postsResponse->setPage($paramFetcher->get('page'));
90
91 3
        $self = $this->generateUrl(
92 3
            'get_posts',
93
            [
94 3
                'locale' => $paramFetcher->get('locale'),
95 3
                'limit' => $paramFetcher->get('limit'),
96 3
                'page' => $paramFetcher->get('page'),
97 3
                'tag' => $paramFetcher->get('tag'),
98
            ],
99 3
            UrlGeneratorInterface::ABSOLUTE_URL
100
        );
101
102 3
        $first = $this->generateUrl(
103 3
            'get_posts',
104
            [
105 3
                'locale' => $paramFetcher->get('locale'),
106 3
                'limit' => $paramFetcher->get('limit'),
107 3
                'tag' => $paramFetcher->get('tag'),
108
            ],
109 3
            UrlGeneratorInterface::ABSOLUTE_URL
110
        );
111
112 3
        $nextPage = $paramFetcher->get('page') < $postsResponse->getPageCount() ?
113 3
            $this->generateUrl(
114 3
                'get_posts',
115
                [
116 3
                    'locale' => $paramFetcher->get('locale'),
117 3
                    'limit' => $paramFetcher->get('limit'),
118 3
                    'page' => $paramFetcher->get('page')+1,
119 3
                    'tag' => $paramFetcher->get('tag'),
120
                ],
121 3
                UrlGeneratorInterface::ABSOLUTE_URL
122
            ) :
123 3
            'false';
124
125 3
        $previsiousPage = $paramFetcher->get('page') > 1 ?
126
            $this->generateUrl(
127
                'get_posts',
128
                [
129
                    'locale' => $paramFetcher->get('locale'),
130
                    'limit' => $paramFetcher->get('limit'),
131
                    'page' => $paramFetcher->get('page')-1,
132
                    'tag' => $paramFetcher->get('tag'),
133
                ],
134
                UrlGeneratorInterface::ABSOLUTE_URL
135
            ) :
136 3
            'false';
137
138 3
        $last = $this->generateUrl(
139 3
            'get_posts',
140
            [
141 3
                'locale' => $paramFetcher->get('locale'),
142 3
                'limit' => $paramFetcher->get('limit'),
143 3
                'page' => $postsResponse->getPageCount(),
144 3
                'tag' => $paramFetcher->get('tag'),
145
            ],
146 3
            UrlGeneratorInterface::ABSOLUTE_URL
147
        );
148
149 3
        $links = new PaginationLinks();
150
151 3
        $postsResponse->setLinks($links->setSelf(new Link($self)));
152 3
        $postsResponse->setLinks($links->setFirst(new Link($first)));
153 3
        $postsResponse->setLinks($links->setNext(new Link($nextPage)));
154 3
        $postsResponse->setLinks($links->setPrev(new Link($previsiousPage)));
155 3
        $postsResponse->setLinks($links->setLast(new Link($last)));
156
157 3
        return $postsResponse;
158
    }
159
160
    /**
161
     * @ApiDoc(
162
     *  resource=true,
163
     *  description="Returns an Post by unique property {slug}",
164
     *  statusCodes={
165
     *      200="Returned when Post by {slug} was found",
166
     *      404="Returned when Post by {slug} was not found",
167
     *  },
168
     *  output = "AppBundle\Entity\Post"
169
     * )
170
     *
171
     * @QueryParam(
172
     *     name="locale",
173
     *     requirements="^[a-zA-Z]+",
174
     *     default="uk",
175
     *     description="Selects language of data you want to receive"
176
     * )
177
     *
178
     * @RestView
179
     */
180 1
    public function getAction(ParamFetcher $paramFetcher, $slug)
181
    {
182 1
        $em = $this->getDoctrine()->getManager();
183
184
        $post = $em
185 1
            ->getRepository('AppBundle:Post')->findOneByslug($slug);
186
187 1
        if (!$post) {
188 1
            throw $this->createNotFoundException('Unable to find '.$slug.' entity');
189
        }
190
191 1
        $post->setLocale($paramFetcher->get('locale'));
192 1
        $em->refresh($post);
193
194 1
        if ($post->getTranslations()) {
195 1
            $post->unsetTranslations();
196
        }
197
198 1
        $tags = $post->getTags();
199
200 1
        foreach ($tags as $tag) {
201 1
            $tag->setLocale($paramFetcher->get('locale'));
202 1
            $em->refresh($tag);
203
204 1
            if ($tag->getTranslations()) {
205 1
                $tag->unsetTranslations();
206
            }
207
        }
208
209 1
        return $post;
210
    }
211
}
212