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

PublishToAppleNewsHandler   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 41
Duplicated Lines 100 %

Coupling/Cohesion

Components 0
Dependencies 5

Importance

Changes 0
Metric Value
wmc 4
lcom 0
cbo 5
dl 41
loc 41
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 9 9 1
A __invoke() 22 22 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\PublishToAppleNews;
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\MultiTenancy\Exception\TenantNotFoundException;
25
use SWP\Component\MultiTenancy\Repository\TenantRepositoryInterface;
26
use Symfony\Component\Messenger\Handler\MessageHandlerInterface;
27
28 View Code Duplication
class PublishToAppleNewsHandler implements MessageHandlerInterface
29
{
30
    private $appleNewsPublisher;
31
32
    private $articleRepository;
33
34
    private $tenantRepository;
35
36
    public function __construct(
37
        AppleNewsPublisher $appleNewsPublisher,
38
        ArticleRepositoryInterface $articleRepository,
39
        TenantRepositoryInterface $tenantRepository
40
    ) {
41
        $this->appleNewsPublisher = $appleNewsPublisher;
42
        $this->articleRepository = $articleRepository;
43
        $this->tenantRepository = $tenantRepository;
44
    }
45
46
    public function __invoke(PublishToAppleNews $publishToAppleNews)
47
    {
48
        $articleId = $publishToAppleNews->getArticleId();
49
        $tenantId = $publishToAppleNews->getTenantId();
50
51
        /** @var ArticleInterface $article */
52
        $article = $this->articleRepository->findOneBy(['id' => $articleId]);
53
54
        if (null === $article) {
55
            throw new ArticleNotFoundException();
56
        }
57
58
        /** @var TenantInterface $tenant */
59
        $tenant = $this->tenantRepository->findOneBy(['id' => $tenantId]);
60
61
        if (null === $tenant) {
62
            throw new TenantNotFoundException($tenant->getName());
63
        }
64
65
        $this->appleNewsPublisher->publish($article, $tenant);
66
        $this->articleRepository->flush();
67
    }
68
}
69