Completed
Push — master ( 678bac...955920 )
by Paweł
09:19 queued 10s
created

SlideshowController   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 74
Duplicated Lines 16.22 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 8
dl 12
loc 74
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A listAction() 12 12 1
A getAction() 0 13 2
A findArticleOr404() 0 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 Core 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\CoreBundle\Controller;
16
17
use Nelmio\ApiDocBundle\Annotation\Model;
18
use Nelmio\ApiDocBundle\Annotation\Operation;
19
use Swagger\Annotations as SWG;
20
use SWP\Component\Common\Response\ResourcesListResponseInterface;
21
use SWP\Component\Common\Response\SingleResourceResponseInterface;
22
use Symfony\Component\Routing\Annotation\Route;
23
use SWP\Component\Common\Criteria\Criteria;
24
use SWP\Component\Common\Pagination\PaginationData;
25
use SWP\Component\Common\Response\ResourcesListResponse;
26
use SWP\Component\Common\Response\SingleResourceResponse;
27
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
28
use Symfony\Component\HttpFoundation\Request;
29
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
30
31
class SlideshowController extends Controller
0 ignored issues
show
Deprecated Code introduced by
The class Symfony\Bundle\Framework...e\Controller\Controller has been deprecated with message: since Symfony 4.2, use "Symfony\Bundle\FrameworkBundle\Controller\AbstractController" instead.

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
32
{
33
    /**
34
     * @Operation(
35
     *     tags={"slideshow"},
36
     *     summary="List all slideshows",
37
     *     @SWG\Parameter(
38
     *         name="sorting",
39
     *         in="query",
40
     *         description="todo",
41
     *         required=false,
42
     *         type="string"
43
     *     ),
44
     *     @SWG\Response(
45
     *         response="200",
46
     *         description="Returned on success.",
47
     *         @SWG\Schema(
48
     *             type="array",
49
     *             @SWG\Items(ref=@Model(type=\SWP\Bundle\CoreBundle\Model\Slideshow::class, groups={"api"}))
50
     *         )
51
     *     )
52
     * )
53
     *
54
     * @Route("/api/{version}/content/slideshows/{articleId}", options={"expose"=true}, defaults={"version"="v2"}, methods={"GET"}, name="swp_api_slideshows_list")
55
     */
56 View Code Duplication
    public function listAction(Request $request, string $articleId): ResourcesListResponseInterface
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...
57
    {
58
        $repository = $this->get('swp.repository.slideshow');
59
60
        $article = $this->findArticleOr404($articleId);
61
62
        $slideshows = $repository->getPaginatedByCriteria(new Criteria([
63
            'article' => $article,
64
        ]), $request->query->get('sorting', []), new PaginationData($request));
65
66
        return new ResourcesListResponse($slideshows);
67
    }
68
69
    /**
70
     * @Operation(
71
     *     tags={"slideshow"},
72
     *     summary="Get single slideshow",
73
     *     @SWG\Response(
74
     *         response="200",
75
     *         description="Returned on success.",
76
     *         @Model(type=\SWP\Bundle\CoreBundle\Model\Slideshow::class, groups={"api"})
77
     *     )
78
     * )
79
     *
80
     * @Route("/api/{version}/content/slideshows/{articleId}/{id}", options={"expose"=true}, defaults={"version"="v2"}, methods={"GET"}, name="swp_api_get_slideshow", requirements={"id"="\d+"})
81
     */
82
    public function getAction($id, string $articleId): SingleResourceResponseInterface
83
    {
84
        $article = $this->findArticleOr404($articleId);
85
86
        if (null === $list = $this->get('swp.repository.slideshow')->findOneBy([
87
                'id' => $id,
88
                'article' => $article,
89
            ])) {
90
            throw new NotFoundHttpException(sprintf('Slideshow with id "%s" was not found.', $id));
91
        }
92
93
        return new SingleResourceResponse($list);
94
    }
95
96
    private function findArticleOr404($id)
97
    {
98
        if (null === $article = $this->get('swp.repository.article')->findOneById($id)) {
99
            throw new NotFoundHttpException(sprintf('Article with id "%s" was not found.', $id));
100
        }
101
102
        return $article;
103
    }
104
}
105