|
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) |
|
|
|
|
|
|
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
|
|
|
|
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.