Completed
Push — 1.5 ( c5bd05...970e37 )
by Rafał
10:47 queued 10s
created

RelatedArticleOrganizationController   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 94
Duplicated Lines 13.83 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 7
dl 13
loc 94
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getAction() 0 9 1
A getRelatedAction() 0 8 1
B getRelated() 3 39 8
A findOr404() 10 10 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
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))) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
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
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...
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