Completed
Push — master ( 05bb5a...5289b8 )
by Rafał
34:32 queued 04:15
created

RelatedArticleOrganizationController   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 55
Duplicated Lines 5.45 %

Coupling/Cohesion

Components 0
Dependencies 5

Importance

Changes 0
Metric Value
wmc 8
lcom 0
cbo 5
dl 3
loc 55
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B getAction() 3 41 8

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\RelatedArticleList;
23
use SWP\Bundle\CoreBundle\Model\RelatedArticleListItem;
24
use SWP\Bundle\MultiTenancyBundle\MultiTenancyEvents;
25
use SWP\Component\Bridge\Model\GroupInterface;
26
use SWP\Component\Common\Response\SingleResourceResponse;
27
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
28
use Symfony\Component\HttpFoundation\Request;
29
30
class RelatedArticleOrganizationController extends Controller
31
{
32
    /**
33
     * @ApiDoc(
34
     *     resource=true,
35
     *     description="Returns a list of related articles",
36
     *     statusCodes={
37
     *         200="Returned on success"
38
     *     }
39
     * )
40
     * @Route("/api/{version}/organization/articles/related/", options={"expose"=true}, defaults={"version"="v1"}, name="swp_api_core_organization_related_articles")
41
     * @Method("POST")
42
     */
43
    public function getAction(Request $request)
44
    {
45
        $content = $request->getContent();
46
        $package = $this->get('swp_bridge.transformer.json_to_package')->transform($content);
47
48
        $relatedItemsGroups = $package->getGroups()->filter(function ($group) {
49
            return GroupInterface::TYPE_RELATED === $group->getType();
50
        });
51
52 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...
53
            return;
54
        }
55
56
        $this->get('event_dispatcher')->dispatch(MultiTenancyEvents::TENANTABLE_DISABLE);
57
        $articleRepository = $this->get('swp.repository.article');
58
59
        $relatedArticlesList = new RelatedArticleList();
60
        foreach ($relatedItemsGroups as $relatedItemsGroup) {
61
            foreach ($relatedItemsGroup->getItems() as $item) {
62
                if (null === ($existingArticles = $articleRepository->findBy(['code' => $item->getGuid()]))) {
63
                    continue;
64
                }
65
66
                $tenants = [];
67
                foreach ($existingArticles as $existingArticle) {
68
                    $tenantCode = $existingArticle->getTenantCode();
69
                    $tenant = $this->get('swp.repository.tenant')->findOneByCode($tenantCode);
70
71
                    $tenants[] = $tenant;
72
                }
73
74
                $relatedArticleListItem = new RelatedArticleListItem();
75
                $relatedArticleListItem->setTenants($tenants);
76
                $relatedArticleListItem->setTitle($item->getHeadline());
77
78
                $relatedArticlesList->addRelatedArticleItem($relatedArticleListItem);
79
            }
80
        }
81
82
        return new SingleResourceResponse($relatedArticlesList);
83
    }
84
}
85