Completed
Push — master ( e0db1d...82088e )
by Paweł
11:28
created

createDestinations()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 10
nc 2
nop 1
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\EventSubscriber;
18
19
use SWP\Bundle\CoreBundle\Matcher\RulesMatcherInterface;
20
use SWP\Bundle\CoreBundle\Model\CompositePublishAction;
21
use SWP\Bundle\CoreBundle\Model\PublishDestination;
22
use SWP\Bundle\CoreBundle\Provider\PublishDestinationProviderInterface;
23
use SWP\Bundle\CoreBundle\Service\ArticlePublisherInterface;
24
use SWP\Bundle\MultiTenancyBundle\MultiTenancyEvents;
25
use SWP\Component\Bridge\Events;
26
use SWP\Component\Rule\Processor\RuleProcessorInterface;
27
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
28
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
29
use Symfony\Component\EventDispatcher\GenericEvent;
30
31
class ProcessOrganizationRulesSubscriber implements EventSubscriberInterface
32
{
33
    /**
34
     * @var RuleProcessorInterface
35
     */
36
    private $ruleProcessor;
37
38
    /**
39
     * @var EventDispatcherInterface
40
     */
41
    private $eventDispatcher;
42
43
    /**
44
     * @var PublishDestinationProviderInterface
45
     */
46
    private $publishDestinationProvider;
47
48
    /**
49
     * @var ArticlePublisherInterface
50
     */
51
    private $articlePublisher;
52
53
    /**
54
     * @var RulesMatcherInterface
55
     */
56
    private $rulesMatcher;
57
58
    /**
59
     * ProcessOrganizationRulesSubscriber constructor.
60
     *
61
     * @param RuleProcessorInterface              $ruleProcessor
62
     * @param EventDispatcherInterface            $eventDispatcher
63
     * @param PublishDestinationProviderInterface $publishDestinationProvider
64
     * @param ArticlePublisherInterface           $articlePublisher
65
     * @param RulesMatcherInterface               $rulesMatcher
66
     */
67
    public function __construct(
68
        RuleProcessorInterface $ruleProcessor,
69
        EventDispatcherInterface $eventDispatcher,
70
        PublishDestinationProviderInterface $publishDestinationProvider,
71
        ArticlePublisherInterface $articlePublisher,
72
        RulesMatcherInterface $rulesMatcher
73
    ) {
74
        $this->ruleProcessor = $ruleProcessor;
75
        $this->eventDispatcher = $eventDispatcher;
76
        $this->publishDestinationProvider = $publishDestinationProvider;
77
        $this->articlePublisher = $articlePublisher;
78
        $this->rulesMatcher = $rulesMatcher;
79
    }
80
81
    /**
82
     * {@inheritdoc}
83
     */
84
    public static function getSubscribedEvents()
85
    {
86
        return [
87
            Events::PACKAGE_POST_CREATE => 'processRules',
88
            Events::PACKAGE_POST_UPDATE => 'processRules',
89
        ];
90
    }
91
92
    /**
93
     * @param GenericEvent $event
94
     */
95
    public function processRules(GenericEvent $event)
96
    {
97
        $package = $event->getSubject();
98
        $destinationsCount = $this->publishDestinationProvider->countDestinationsByPackageGuid($package);
99
100
        if (0 < $destinationsCount) {
101
            $this->eventDispatcher->dispatch(MultiTenancyEvents::TENANTABLE_DISABLE);
102
            $result = $this->rulesMatcher->getMatchedRules($package);
103
            $publishAction = new CompositePublishAction($this->createDestinations($result));
104
105
            $this->articlePublisher->publish($package, $publishAction);
106
            $this->eventDispatcher->dispatch(MultiTenancyEvents::TENANTABLE_ENABLE);
107
108
            return;
109
        }
110
111
        $this->eventDispatcher->dispatch(MultiTenancyEvents::TENANTABLE_DISABLE);
112
        $this->ruleProcessor->process($package);
113
        $this->eventDispatcher->dispatch(MultiTenancyEvents::TENANTABLE_ENABLE);
114
    }
115
116
    private function createDestinations(array $result): array
117
    {
118
        $destinations = [];
119
        foreach ((array) $result['tenants'] as $tenant) {
120
            $destination = new PublishDestination();
121
            $destination->setTenant($tenant['tenant']);
122
            $destination->setRoute($tenant['route']);
123
            $destination->setPublished($tenant['published']);
124
            $destination->setFbia($tenant['fbia']);
125
126
            $destinations[] = $destination;
127
        }
128
129
        return $destinations;
130
    }
131
}
132