Completed
Pull Request — 2.1 (#1090)
by Paweł
09:07
created

UnpublishFromAppleNewsHandler   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 43
Duplicated Lines 100 %

Coupling/Cohesion

Components 0
Dependencies 7

Importance

Changes 0
Metric Value
wmc 4
lcom 0
cbo 7
dl 43
loc 43
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 9 9 1
A __invoke() 24 24 3

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 2020 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 2020 Sourcefabric z.ú
14
 * @license http://www.superdesk.org/license
15
 */
16
17
namespace SWP\Bundle\CoreBundle\MessageHandler;
18
19
use SWP\Bundle\CoreBundle\AppleNews\AppleNewsPublisher;
20
use SWP\Bundle\CoreBundle\MessageHandler\Message\UnpublishFromAppleNews;
21
use SWP\Bundle\CoreBundle\Model\ArticleInterface;
22
use SWP\Bundle\CoreBundle\Model\TenantInterface;
23
use SWP\Bundle\CoreBundle\Repository\ArticleRepositoryInterface;
24
use SWP\Component\Common\Exception\ArticleNotFoundException;
25
use SWP\Component\MultiTenancy\Exception\TenantNotFoundException;
26
use SWP\Component\MultiTenancy\Repository\TenantRepositoryInterface;
27
use Symfony\Component\Messenger\Handler\MessageHandlerInterface;
28
29 View Code Duplication
class UnpublishFromAppleNewsHandler implements MessageHandlerInterface
30
{
31
    private $appleNewsPublisher;
32
33
    private $articleRepository;
34
35
    private $tenantRepository;
36
37
    public function __construct(
38
        AppleNewsPublisher $appleNewsPublisher,
39
        ArticleRepositoryInterface $articleRepository,
40
        TenantRepositoryInterface $tenantRepository
41
    ) {
42
        $this->appleNewsPublisher = $appleNewsPublisher;
43
        $this->articleRepository = $articleRepository;
44
        $this->tenantRepository = $tenantRepository;
45
    }
46
47
    public function __invoke(UnpublishFromAppleNews $unpublishFromAppleNews)
48
    {
49
        $articleId = $unpublishFromAppleNews->getArticleId();
50
        $tenantId = $unpublishFromAppleNews->getTenantId();
51
52
        /** @var ArticleInterface $article */
53
        $article = $this->articleRepository->findOneBy(['id' => $articleId]);
54
55
        if (null === $article) {
56
            throw new ArticleNotFoundException();
57
        }
58
59
        /** @var TenantInterface $tenant */
60
        $tenant = $this->tenantRepository->findOneBy(['id' => $tenantId]);
61
62
        if (null === $tenant) {
63
            throw new TenantNotFoundException($tenant->getName());
64
        }
65
66
        $this->appleNewsPublisher->unpublish($article, $tenant);
67
        $article->setAppleNewsArticle(null);
0 ignored issues
show
Documentation introduced by
null is of type null, but the function expects a object<SWP\Bundle\CoreBu...leNewsArticleInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
68
69
        $this->articleRepository->flush();
70
    }
71
}
72