Completed
Push — 1.5 ( 76c4d0...20e78d )
by Rafał
17:41 queued 08:46
created

RelatedArticleController::listAction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16

Duplication

Lines 16
Ratio 100 %

Importance

Changes 0
Metric Value
dl 16
loc 16
rs 9.7333
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Superdesk Web Publisher Content Bundle.
7
 *
8
 * Copyright 2019 Sourcefabric z.ú. and contributors.
9
 *
10
 * For the full copyright and license information, please see the
11
 * AUTHORS and LICENSE files distributed with this source code.
12
 *
13
 * @copyright 2019 Sourcefabric z.ú
14
 * @license http://www.superdesk.org/license
15
 */
16
17
namespace SWP\Bundle\ContentBundle\Controller;
18
19
use Nelmio\ApiDocBundle\Annotation\ApiDoc;
20
use SWP\Bundle\ContentBundle\Model\ArticleInterface;
21
use SWP\Component\Common\Criteria\Criteria;
22
use SWP\Component\Common\Exception\NotFoundHttpException;
23
use SWP\Component\Common\Pagination\PaginationData;
24
use SWP\Component\Common\Response\ResourcesListResponse;
25
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
26
use Symfony\Component\HttpFoundation\Request;
27
28
class RelatedArticleController extends Controller
29
{
30
    /**
31
     * List related articles.
32
     *
33
     * @ApiDoc(
34
     *     resource=true,
35
     *     description="List related articles",
36
     *     statusCodes={
37
     *         200="Returned on success.",
38
     *         500="Unexpected error."
39
     *     },
40
     *     filters={
41
     *         {"name"="sorting", "dataType"="string", "pattern"="[updatedAt]=asc|desc"}
42
     *     }
43
     * )
44
     */
45 View Code Duplication
    public function listAction(Request $request, string $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...
46
    {
47
        $article = $this->findOr404($id);
48
49
        $repository = $this->get('swp.repository.related_article');
50
51
        $items = $repository->getPaginatedByCriteria(
52
            new Criteria([
53
                'article' => $article,
54
            ]),
55
            $request->query->get('sorting', []),
56
            new PaginationData($request)
57
        );
58
59
        return new ResourcesListResponse($items);
60
    }
61
62
    private function findOr404(string $id): ArticleInterface
63
    {
64
        $article = $this->get('swp.provider.article')->getOneById($id);
65
66
        if (null === $article) {
67
            throw new NotFoundHttpException(sprintf('Article "%s" was not found.', $id));
68
        }
69
70
        return $article;
71
    }
72
}
73