Completed
Push — master ( 4da1f4...8b580e )
by Paweł
08:27
created

ArticlePopulator::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 11

Duplication

Lines 13
Ratio 100 %

Importance

Changes 0
Metric Value
dl 13
loc 13
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 11
nc 1
nop 5
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 2017 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 2017 Sourcefabric z.ú
14
 * @license http://www.superdesk.org/license
15
 */
16
17
namespace SWP\Bundle\CoreBundle\Rule\Populator;
18
19
use SWP\Bundle\ContentBundle\ArticleEvents;
20
use SWP\Bundle\ContentBundle\Doctrine\ArticleRepositoryInterface;
21
use SWP\Bundle\ContentBundle\Event\ArticleEvent;
22
use SWP\Bundle\ContentBundle\Factory\ArticleFactoryInterface;
23
use SWP\Bundle\CoreBundle\Model\ArticleInterface;
24
use SWP\Bundle\CoreBundle\Model\ArticleStatisticsInterface;
25
use SWP\Bundle\CoreBundle\Model\PackageInterface;
26
use SWP\Bundle\CoreBundle\Model\TenantInterface;
27
use SWP\Component\Bridge\Events;
28
use SWP\Component\MultiTenancy\Context\TenantContextInterface;
29
use SWP\Component\Storage\Factory\FactoryInterface;
30
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
31
use Symfony\Component\EventDispatcher\GenericEvent;
32
33
final class ArticlePopulator implements ArticlePopulatorInterface
34
{
35
    /**
36
     * @var ArticleFactoryInterface
37
     */
38
    private $articleFactory;
39
40
    /**
41
     * @var FactoryInterface
42
     */
43
    private $articleStatisticsFactory;
44
45
    /**
46
     * @var EventDispatcherInterface
47
     */
48
    private $eventDispatcher;
49
50
    /**
51
     * @var ArticleRepositoryInterface
52
     */
53
    private $articleRepository;
54
55
    /**
56
     * @var TenantContextInterface
57
     */
58
    private $tenantContext;
59
60 View Code Duplication
    public function __construct(
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...
61
        ArticleFactoryInterface $articleFactory,
62
        FactoryInterface $articleStatisticsFactory,
63
        EventDispatcherInterface $eventDispatcher,
64
        ArticleRepositoryInterface $articleRepository,
65
        TenantContextInterface $tenantContext
66
    ) {
67
        $this->articleFactory = $articleFactory;
68
        $this->articleStatisticsFactory = $articleStatisticsFactory;
69
        $this->eventDispatcher = $eventDispatcher;
70
        $this->articleRepository = $articleRepository;
71
        $this->tenantContext = $tenantContext;
72
    }
73
74
    /**
75
     * {@inheritdoc}
76
     */
77
    public function populate(PackageInterface $package, array $tenants): void
78
    {
79
        /** @var TenantInterface $tenant */
80
        foreach ($tenants as $tenant) {
81
            $this->tenantContext->setTenant($tenant);
82
83
            if (null !== ($article = $this->findArticleByTenantAndCode($tenant->getCode(), $package->getGuid()))) {
84
                continue;
85
            }
86
87
            /** @var ArticleInterface $article */
88
            $article = $this->articleFactory->createFromPackage($package);
89
            /** @var ArticleStatisticsInterface $articleStatistics */
90
            $articleStatistics = $this->articleStatisticsFactory->create();
91
            $articleStatistics->setArticle($article);
92
93
            $this->articleRepository->persist($articleStatistics);
94
95
            $this->eventDispatcher->dispatch(Events::SWP_VALIDATION, new GenericEvent($article));
96
97
            $article->setPackage($package);
98
            $article->setArticleStatistics($articleStatistics);
99
            $this->articleRepository->persist($article);
100
            $this->articleRepository->flush();
101
102
            $this->eventDispatcher->dispatch(ArticleEvents::PRE_CREATE, new ArticleEvent($article, $package, ArticleEvents::PRE_CREATE));
103
            $this->articleRepository->flush();
104
        }
105
    }
106
107
    /**
108
     * @param string $tenantCode
109
     * @param string $code
110
     *
111
     * @return ArticleInterface
112
     */
113
    private function findArticleByTenantAndCode(string $tenantCode, string $code)
114
    {
115
        return $this->articleRepository->findOneBy([
116
            'tenantCode' => $tenantCode,
117
            'code' => $code,
118
        ]);
119
    }
120
}
121