Completed
Push — 1.4 ( eba84d...d3bf7f )
by Rafał
08:33
created

AbstractWebhookEventSubscriber   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 7
dl 0
loc 51
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
B getWebhooks() 0 23 6
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 2018 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 2018 Sourcefabric z.ú
14
 * @license http://www.superdesk.org/license
15
 */
16
17
namespace SWP\Bundle\CoreBundle\EventSubscriber;
18
19
use SWP\Bundle\CoreBundle\Repository\WebhookRepositoryInterface;
20
use SWP\Bundle\MultiTenancyBundle\MultiTenancyEvents;
21
use SWP\Component\MultiTenancy\Context\TenantContextInterface;
22
use SWP\Component\MultiTenancy\Model\TenantAwareInterface;
23
use SWP\Component\MultiTenancy\Repository\TenantRepositoryInterface;
24
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
25
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
26
27
abstract class AbstractWebhookEventSubscriber implements EventSubscriberInterface
28
{
29
    /**
30
     * @var WebhookRepositoryInterface
31
     */
32
    private $webhooksRepository;
33
34
    /**
35
     * @var TenantContextInterface
36
     */
37
    private $tenantContext;
38
39
    /**
40
     * @var TenantRepositoryInterface
41
     */
42
    private $tenantRepository;
43
44
    public function __construct(
45
        WebhookRepositoryInterface $webhooksRepository,
46
        TenantContextInterface $tenantContext,
47
        TenantRepositoryInterface $tenantRepository
48
    ) {
49
        $this->webhooksRepository = $webhooksRepository;
50
        $this->tenantContext = $tenantContext;
51
        $this->tenantRepository = $tenantRepository;
52
    }
53
54
    public function getWebhooks($subject, string $webhookEventName, EventDispatcherInterface $dispatcher): array
55
    {
56
        $originalTenant = null;
57
        if (
58
            $subject instanceof TenantAwareInterface
59
            && $subject->getTenantCode() !== $this->tenantContext->getTenant()->getCode()
60
            && null !== $subject->getTenantCode()
61
            && null !== ($subjectTenant = $this->tenantRepository->findOneByCode($subject->getTenantCode()))
62
        ) {
63
            $originalTenant = $this->tenantContext->getTenant();
64
            $this->tenantContext->setTenant($subjectTenant);
65
        } else {
66
            $dispatcher->dispatch(MultiTenancyEvents::TENANTABLE_ENABLE);
67
        }
68
69
        $webhooks = $this->webhooksRepository->getEnabledForEvent($webhookEventName)->getResult();
70
71
        if (null !== $originalTenant) {
72
            $this->tenantContext->setTenant($originalTenant);
73
        }
74
75
        return $webhooks;
76
    }
77
}
78