Completed
Push — master ( 25e5a7...6469d9 )
by Paweł
62:31
created

ArticleController   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 146
Duplicated Lines 25.34 %

Coupling/Cohesion

Components 1
Dependencies 10

Test Coverage

Coverage 84.85%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 10
dl 37
loc 146
ccs 28
cts 33
cp 0.8485
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A listAction() 0 10 1
A getAction() 10 10 2
A updateAction() 19 19 2
A deleteAction() 0 8 1
A findOr404() 8 8 2

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
/*
4
 * This file is part of the Superdesk Web Publisher Content Bundle.
5
 *
6
 * Copyright 2015 Sourcefabric z.u. and contributors.
7
 *
8
 * For the full copyright and license information, please see the
9
 * AUTHORS and LICENSE files distributed with this source code.
10
 *
11
 * @copyright 2015 Sourcefabric z.ú
12
 * @license http://www.superdesk.org/license
13
 */
14
15
namespace SWP\Bundle\ContentBundle\Controller;
16
17
use Nelmio\ApiDocBundle\Annotation\ApiDoc;
18
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
19
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
20
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Cache;
21
use SWP\Component\Common\Criteria\Criteria;
22
use SWP\Component\Common\Pagination\PaginationData;
23
use SWP\Component\Common\Response\ResourcesListResponse;
24
use SWP\Component\Common\Response\ResponseContext;
25
use SWP\Component\Common\Response\SingleResourceResponse;
26
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
27
use Symfony\Component\HttpFoundation\Request;
28
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
29
use SWP\Bundle\ContentBundle\Form\Type\ArticleType;
30
31
class ArticleController extends Controller
32
{
33
    /**
34
     * List all articles for current tenant.
35
     *
36
     * @ApiDoc(
37
     *     resource=true,
38
     *     description="List all articles for current tenant",
39
     *     statusCodes={
40
     *         200="Returned on success.",
41
     *     },
42
     *     filters={
43
     *         {"name"="status", "dataType"="string", "pattern"="new|published|unpublished|canceled"},
44
     *         {"name"="route", "dataType"="integer"}
45
     *     }
46
     * )
47
     * @Route("/api/{version}/content/articles/", options={"expose"=true}, defaults={"version"="v1"}, name="swp_api_content_list_articles")
48
     * @Method("GET")
49
     *
50
     * @Cache(expires="10 minutes", public=true)
51
     *
52
     * @param Request $request
53
     *
54
     * @return ResourcesListResponse
55
     */
56
    public function listAction(Request $request)
57
    {
58
        $articles = $this->get('swp.repository.article')
59
            ->getPaginatedByCriteria(new Criteria([
60
                'status' => $request->query->get('status', ''),
61
                'route' => $request->query->get('route', ''),
62
            ]), [], new PaginationData($request));
63
64
        return new ResourcesListResponse($articles);
65
    }
66
67
    /**
68
     * Show single tenant article.
69
     *
70
     * @ApiDoc(
71
     *     resource=true,
72
     *     description="Show single tenant article",
73
     *     statusCodes={
74
     *         200="Returned on success."
75
     *     }
76 10
     * )
77
     * @Route("/api/{version}/content/articles/{id}", options={"expose"=true}, defaults={"version"="v1"}, name="swp_api_content_show_articles", requirements={"id"=".+"})
78 10
     * @Method("GET")
79
     *
80 10
     * @Cache(expires="10 minutes", public=true)
81
     */
82 View Code Duplication
    public function getAction($id)
0 ignored issues
show
Duplication introduced by
This method 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...
83
    {
84 10
        $article = $this->get('swp.provider.article')->getOneById($id);
85
86
        if (null === $article) {
87
            throw new NotFoundHttpException('Article was not found.');
88
        }
89
90
        return new SingleResourceResponse($article);
91
    }
92
93
    /**
94
     * Updates articles.
95
     *
96
     * Possible article statuses are:
97
     *
98
     *  * new
99
     *  * published
100
     *  * unpublished
101
     *  * canceled
102
     *
103
     * Changing status from any status to `published` will make article visible for every user.
104
     *
105
     * Changing status from `published` to any other will make article hidden for user who don't have rights to see unpublished articles.
106
     *
107
     * @ApiDoc(
108
     *     resource=true,
109
     *     description="Updates articles",
110
     *     statusCodes={
111
     *         200="Returned on success.",
112
     *         400="Returned when validation failed.",
113
     *         500="Returned when unexpected error."
114 4
     *     },
115
     *     input="SWP\Bundle\ContentBundle\Form\Type\ArticleType"
116 4
     * )
117 4
     * @Route("/api/{version}/content/articles/{id}", options={"expose"=true}, defaults={"version"="v1"}, name="swp_api_content_update_articles", requirements={"id"=".+"})
118 4
     * @Method("PATCH")
119
     */
120 4 View Code Duplication
    public function updateAction(Request $request, $id)
0 ignored issues
show
Duplication introduced by
This method 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...
121
    {
122 4
        $objectManager = $this->get('swp.object_manager.article');
123 4
        $article = $this->findOr404($id);
124 4
        $originalArticleStatus = $article->getStatus();
125 4
126 4
        $form = $this->createForm(ArticleType::class, $article, ['method' => $request->getMethod()]);
127 4
128
        $form->handleRequest($request);
129 4
        if ($form->isValid()) {
130
            $this->get('swp.service.article')->reactOnStatusChange($originalArticleStatus, $article);
131
            $objectManager->flush();
132
            $objectManager->refresh($article);
133
134
            return new SingleResourceResponse($article);
135 4
        }
136
137 4
        return new SingleResourceResponse($form, new ResponseContext(500));
138 4
    }
139 4
140
    /**
141
     * Delete Article.
142 1
     *
143
     * @ApiDoc(
144 1
     *     resource=true,
145 1
     *     description="Deletes articles",
146 1
     *     statusCodes={
147
     *         204="Returned on success.",
148
     *         404="Returned when article not found.",
149
     *         500="Returned when unexpected error."
150
     *     }
151 1
     * )
152
     * @Route("/api/{version}/content/articles/{id}", options={"expose"=true}, defaults={"version"="v1"}, name="swp_api_content_delete_articles", requirements={"id"=".+"})
153 4
     * @Method("DELETE")
154
     *
155 4
     * @param int $id
156
     *
157
     * @return SingleResourceResponse
158
     */
159 4
    public function deleteAction($id)
160
    {
161
        $objectManager = $this->get('swp.object_manager.article');
162
        $objectManager->remove($this->findOr404($id));
163
        $objectManager->flush();
164
165
        return new SingleResourceResponse(null, new ResponseContext(204));
166
    }
167
168 View Code Duplication
    private function findOr404($id)
0 ignored issues
show
Duplication introduced by
This method 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...
169
    {
170
        if (null === $article = $this->get('swp.provider.article')->getOneById($id)) {
171
            throw new NotFoundHttpException('Article was not found.');
172
        }
173
174
        return $article;
175
    }
176
}
177