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.

SubscribeToNewsletterAction::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 4
c 1
b 0
f 1
nc 1
nop 4
dl 0
loc 10
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Odiseo\SyliusMailchimpPlugin\Controller\Action;
6
7
use Odiseo\SyliusMailchimpPlugin\Handler\CustomerNewsletterSubscriptionHandler;
8
use Odiseo\SyliusMailchimpPlugin\Entity\MailchimpListIdAwareInterface;
9
use Odiseo\SyliusMailchimpPlugin\Provider\ListIdProviderInterface;
10
use Sylius\Component\Channel\Context\ChannelContextInterface;
11
use Sylius\Component\Channel\Model\ChannelInterface;
12
use Sylius\Component\Core\Model\Customer;
13
use Symfony\Component\HttpFoundation\JsonResponse;
14
use Symfony\Component\HttpFoundation\Request;
15
use Symfony\Contracts\Translation\TranslatorInterface;
16
17
final class SubscribeToNewsletterAction
18
{
19
    /** @var CustomerNewsletterSubscriptionHandler */
20
    private $customerNewsletterSubscriptionHandler;
21
22
    /** @var TranslatorInterface */
23
    private $translator;
24
25
    /** @var ChannelContextInterface */
26
    private $channelContext;
27
28
    /** @var ListIdProviderInterface */
29
    private $listIdProvider;
30
31
    public function __construct(
32
        CustomerNewsletterSubscriptionHandler $customerNewsletterSubscriptionHandler,
33
        TranslatorInterface $translator,
34
        ChannelContextInterface $channelContext,
35
        ListIdProviderInterface $listIdProvider
36
    ) {
37
        $this->customerNewsletterSubscriptionHandler = $customerNewsletterSubscriptionHandler;
38
        $this->translator = $translator;
39
        $this->channelContext = $channelContext;
40
        $this->listIdProvider = $listIdProvider;
41
    }
42
43
    /**
44
     * @param Request $request
45
     * @return JsonResponse
46
     */
47
    public function __invoke(Request $request): JsonResponse
48
    {
49
        $newsletter = $request->request->get('newsletter');
50
        /** @var string $email */
51
        $email = $newsletter['email'];
52
53
        $customer = new Customer();
54
        $customer->setEmail($email);
55
56
        $listId = $this->getListIdByChannel($this->channelContext->getChannel());
57
58
        $response = $this->customerNewsletterSubscriptionHandler->subscribe($customer, $listId);
59
60
        if (!isset($response['id'])) {
61
            return new JsonResponse([
62
                'success' => false,
63
                'message' => $response['detail'],
64
            ]);
65
        }
66
67
        return new JsonResponse([
68
            'success' => true,
69
            'message' => $this->translator->trans('odiseo_sylius_mailchimp_plugin.ui.subscribed_successfully'),
70
        ]);
71
    }
72
73
    /**
74
     * @param ChannelInterface $channel
75
     * @return string
76
     */
77
    private function getListIdByChannel(ChannelInterface $channel): string
78
    {
79
        if ($channel instanceof MailchimpListIdAwareInterface) {
80
            if ($listId = $channel->getListId()) {
81
                return $listId;
82
            }
83
        }
84
85
        return $this->listIdProvider->getListId();
86
    }
87
}
88