1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the Superdesk Web Publisher Core 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\CoreBundle\Controller; |
18
|
|
|
|
19
|
|
|
use Nelmio\ApiDocBundle\Annotation\ApiDoc; |
20
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method; |
21
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; |
22
|
|
|
use SWP\Bundle\CoreBundle\Model\PackageInterface; |
23
|
|
|
use SWP\Bundle\CoreBundle\Model\RelatedArticleList; |
24
|
|
|
use SWP\Bundle\CoreBundle\Model\RelatedArticleListItem; |
25
|
|
|
use SWP\Bundle\MultiTenancyBundle\MultiTenancyEvents; |
26
|
|
|
use SWP\Component\Bridge\Model\GroupInterface; |
27
|
|
|
use SWP\Component\Common\Exception\NotFoundHttpException; |
28
|
|
|
use SWP\Component\Common\Response\SingleResourceResponse; |
29
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller; |
30
|
|
|
use Symfony\Component\HttpFoundation\Request; |
31
|
|
|
|
32
|
|
|
class RelatedArticleOrganizationController extends Controller |
33
|
|
|
{ |
34
|
|
|
/** |
35
|
|
|
* @ApiDoc( |
36
|
|
|
* resource=true, |
37
|
|
|
* description="Returns a list of related articles", |
38
|
|
|
* statusCodes={ |
39
|
|
|
* 200="Returned on success" |
40
|
|
|
* } |
41
|
|
|
* ) |
42
|
|
|
* @Route("/api/{version}/organization/articles/related/", options={"expose"=true}, defaults={"version"="v1"}, name="swp_api_core_organization_related_articles") |
43
|
|
|
* @Method("POST") |
44
|
|
|
*/ |
45
|
|
|
public function getAction(Request $request) |
46
|
|
|
{ |
47
|
|
|
$content = $request->getContent(); |
48
|
|
|
$package = $this->get('swp_bridge.transformer.json_to_package')->transform($content); |
49
|
|
|
|
50
|
|
|
$relatedArticlesList = $this->getRelated($package); |
51
|
|
|
|
52
|
|
|
return new SingleResourceResponse($relatedArticlesList); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @ApiDoc( |
57
|
|
|
* resource=true, |
58
|
|
|
* description="Returns a list of related articles", |
59
|
|
|
* statusCodes={ |
60
|
|
|
* 200="Returned on success" |
61
|
|
|
* } |
62
|
|
|
* ) |
63
|
|
|
* @Route("/api/{version}/packages/{id}/related/", options={"expose"=true}, defaults={"version"="v1"}, name="swp_api_core_packages_related_articles", requirements={"id"="\d+"}) |
64
|
|
|
* @Method("GET") |
65
|
|
|
*/ |
66
|
|
|
public function getRelatedAction(string $id) |
67
|
|
|
{ |
68
|
|
|
$package = $this->findOr404((int) $id); |
69
|
|
|
|
70
|
|
|
$relatedArticlesList = $this->getRelated($package); |
71
|
|
|
|
72
|
|
|
return new SingleResourceResponse($relatedArticlesList); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
private function getRelated(PackageInterface $package): RelatedArticleList |
76
|
|
|
{ |
77
|
|
|
$relatedItemsGroups = $package->getGroups()->filter(function ($group) { |
78
|
|
|
return GroupInterface::TYPE_RELATED === $group->getType(); |
79
|
|
|
}); |
80
|
|
|
|
81
|
|
|
$relatedArticlesList = new RelatedArticleList(); |
82
|
|
|
|
83
|
|
View Code Duplication |
if (null === $package || (null !== $package && 0 === \count($relatedItemsGroups))) { |
|
|
|
|
84
|
|
|
return $relatedArticlesList; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
$this->get('event_dispatcher')->dispatch(MultiTenancyEvents::TENANTABLE_DISABLE); |
88
|
|
|
$articleRepository = $this->get('swp.repository.article'); |
89
|
|
|
|
90
|
|
|
foreach ($relatedItemsGroups as $relatedItemsGroup) { |
91
|
|
|
foreach ($relatedItemsGroup->getItems() as $item) { |
92
|
|
|
if (null === ($existingArticles = $articleRepository->findBy(['code' => $item->getGuid()]))) { |
93
|
|
|
continue; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
$tenants = []; |
97
|
|
|
foreach ($existingArticles as $existingArticle) { |
98
|
|
|
$tenantCode = $existingArticle->getTenantCode(); |
99
|
|
|
$tenant = $this->get('swp.repository.tenant')->findOneByCode($tenantCode); |
100
|
|
|
|
101
|
|
|
$tenants[] = $tenant; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
$relatedArticleListItem = new RelatedArticleListItem(); |
105
|
|
|
$relatedArticleListItem->setTenants($tenants); |
106
|
|
|
$relatedArticleListItem->setTitle($item->getHeadline()); |
107
|
|
|
|
108
|
|
|
$relatedArticlesList->addRelatedArticleItem($relatedArticleListItem); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
return $relatedArticlesList; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
View Code Duplication |
private function findOr404(int $id): PackageInterface |
|
|
|
|
116
|
|
|
{ |
117
|
|
|
$this->get('event_dispatcher')->dispatch(MultiTenancyEvents::TENANTABLE_DISABLE); |
118
|
|
|
$tenantContext = $this->get('swp_multi_tenancy.tenant_context'); |
119
|
|
|
if (null === $package = $this->get('swp.repository.package')->findOneBy(['id' => $id, 'organization' => $tenantContext->getTenant()->getOrganization()])) { |
120
|
|
|
throw new NotFoundHttpException('Package was not found.'); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
return $package; |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|
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.