GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( fb340c...1ac73a )
by Odiseo
04:26
created

CustomerSubscriber::getListIdByChannel()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 4
nc 3
nop 1
dl 0
loc 9
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Odiseo\SyliusMailchimpPlugin\EventListener;
6
7
use Doctrine\Common\EventSubscriber;
8
use Doctrine\ORM\Event\LifecycleEventArgs;
9
use Odiseo\SyliusMailchimpPlugin\Handler\CustomerNewsletterSubscriptionHandlerInterface;
10
use Odiseo\SyliusMailchimpPlugin\Handler\CustomerRegisterHandlerInterface;
11
use Odiseo\SyliusMailchimpPlugin\Provider\ListIdProviderInterface;
12
use Sylius\Component\Channel\Context\ChannelContextInterface;
13
use Sylius\Component\Channel\Repository\ChannelRepositoryInterface;
14
use Sylius\Component\Core\Model\ChannelInterface;
15
use Sylius\Component\Core\Model\CustomerInterface;
16
17
final class CustomerSubscriber implements EventSubscriber
18
{
19
    /**
20
     * @var ChannelRepositoryInterface
21
     */
22
    private $channelRepository;
23
24
    /**
25
     * @var ChannelContextInterface
26
     */
27
    private $channelContext;
28
29
    /**
30
     * @var CustomerRegisterHandlerInterface
31
     */
32
    private $customerRegisterHandler;
33
34
    /**
35
     * @var CustomerNewsletterSubscriptionHandlerInterface
36
     */
37
    private $customerNewsletterSubscriptionHandler;
38
39
    /**
40
     * @param ChannelRepositoryInterface $channelRepository
41
     * @param ChannelContextInterface $channelContext
42
     * @param CustomerRegisterHandlerInterface $customerRegisterHandler
43
     * @param CustomerNewsletterSubscriptionHandlerInterface $customerNewsletterSubscriptionHandler
44
     */
45
    public function __construct(
46
        ChannelRepositoryInterface $channelRepository,
47
        ChannelContextInterface $channelContext,
48
        CustomerRegisterHandlerInterface $customerRegisterHandler,
49
        CustomerNewsletterSubscriptionHandlerInterface $customerNewsletterSubscriptionHandler
50
    ) {
51
        $this->channelRepository = $channelRepository;
52
        $this->customerRegisterHandler = $customerRegisterHandler;
53
        $this->channelContext = $channelContext;
54
        $this->customerNewsletterSubscriptionHandler = $customerNewsletterSubscriptionHandler;
55
    }
56
57
    public function getSubscribedEvents()
58
    {
59
        return [
60
            'postPersist',
61
            'postUpdate',
62
            'postRemove',
63
        ];
64
    }
65
66
    /**
67
     * @param LifecycleEventArgs $args
68
     */
69
    public function postPersist(LifecycleEventArgs $args)
70
    {
71
        $customer = $args->getEntity();
72
73
        if ($customer instanceof CustomerInterface) {
74
            $this->register($customer);
75
        }
76
    }
77
78
    /**
79
     * @param LifecycleEventArgs $args
80
     */
81
    public function postUpdate(LifecycleEventArgs $args)
82
    {
83
        $customer = $args->getEntity();
84
85
        if ($customer instanceof CustomerInterface) {
86
            $this->register($customer);
87
        }
88
    }
89
90
    /**
91
     * @param LifecycleEventArgs $args
92
     */
93
    public function postRemove(LifecycleEventArgs $args)
94
    {
95
        $customer = $args->getEntity();
96
97
        if ($customer instanceof CustomerInterface) {
98
            $this->unregister($customer);
99
        }
100
    }
101
102
    /**
103
     * @param CustomerInterface $customer
104
     */
105
    private function register(CustomerInterface $customer)
106
    {
107
        /** @var ChannelInterface|null $subscribedChannel */
108
        $subscribedChannel = null;
0 ignored issues
show
Unused Code introduced by
The assignment to $subscribedChannel is dead and can be removed.
Loading history...
109
        try {
110
            $subscribedChannel = (true === $customer->isSubscribedToNewsletter())?$this->channelContext->getChannel():null;
111
        } catch (\Exception $e) {}
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
112
113
        $channels = $this->channelRepository->findAll();
114
        /** @var ChannelInterface $channel */
115
        foreach ($channels as $channel) {
116
            $isSubscribed = !!($subscribedChannel && $subscribedChannel->getCode() == $channel->getCode());
117
            $this->customerRegisterHandler->register($customer, $channel, $isSubscribed);
118
        }
119
    }
120
121
    /**
122
     * @param CustomerInterface $customer
123
     */
124
    private function unregister(CustomerInterface $customer)
125
    {
126
        $channels = $this->channelRepository->findAll();
127
128
        /** @var ChannelInterface $channel */
129
        foreach ($channels as $channel) {
130
            $this->customerRegisterHandler->unregister($customer, $channel);
131
        }
132
    }
133
}
134