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.

ExportContactConsumer::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 5
nc 1
nop 5
dl 0
loc 12
rs 10
c 1
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
/**
4
 */
5
namespace CommerceLeague\ActiveCampaign\MessageQueue\Customer;
6
7
use CommerceLeague\ActiveCampaign\Api\ContactRepositoryInterface;
8
use CommerceLeague\ActiveCampaign\Gateway\Client;
9
use CommerceLeague\ActiveCampaign\Gateway\Request\ContactBuilder as ContactRequestBuilder;
10
use CommerceLeague\ActiveCampaign\Logger\Logger;
11
use CommerceLeague\ActiveCampaign\MessageQueue\ConsumerInterface;
12
use CommerceLeague\ActiveCampaignApi\Exception\HttpException;
13
use CommerceLeague\ActiveCampaignApi\Exception\UnprocessableEntityHttpException;
14
use Magento\Customer\Api\CustomerRepositoryInterface as MagentoCustomerRepositoryInterface;
15
use Magento\Framework\Exception\CouldNotSaveException;
16
use Magento\Framework\Exception\LocalizedException;
17
use Magento\Framework\Exception\NoSuchEntityException;
18
19
/**
20
 * Class ExportContactConsumer
21
 */
22
class ExportContactConsumer implements ConsumerInterface
23
{
24
    /**
25
     * @var MagentoCustomerRepositoryInterface
26
     */
27
    private $magentoCustomerRepository;
28
29
    /**
30
     * @var Logger
31
     */
32
    private $logger;
33
34
    /**
35
     * @var ContactRepositoryInterface
36
     */
37
    private $contactRepository;
38
39
    /**
40
     * @var ContactRequestBuilder
41
     */
42
    private $contactRequestBuilder;
43
44
    /**
45
     * @var Client
46
     */
47
    private $client;
48
49
    /**
50
     * @param MagentoCustomerRepositoryInterface $magentoCustomerRepository
51
     * @param Logger $logger
52
     * @param ContactRequestBuilder $contactRequestBuilder
53
     * @param ContactRepositoryInterface $contactRepository
54
     * @param Client $client
55
     */
56
    public function __construct(
57
        MagentoCustomerRepositoryInterface $magentoCustomerRepository,
58
        Logger $logger,
59
        ContactRepositoryInterface $contactRepository,
60
        ContactRequestBuilder $contactRequestBuilder,
61
        Client $client
62
    ) {
63
        $this->magentoCustomerRepository = $magentoCustomerRepository;
64
        $this->logger = $logger;
65
        $this->contactRepository = $contactRepository;
66
        $this->contactRequestBuilder = $contactRequestBuilder;
67
        $this->client = $client;
68
    }
69
70
    /**
71
     * @param string $message
72
     * @throws CouldNotSaveException
73
     */
74
    public function consume(string $message): void
75
    {
76
        $message = json_decode($message, true);
77
78
        try {
79
            $magentoCustomer = $this->magentoCustomerRepository->getById($message['magento_customer_id']);
80
        } catch (NoSuchEntityException|LocalizedException $e) {
81
            $this->logger->error($e->getMessage());
82
            return;
83
        }
84
85
        $contact = $this->contactRepository->getOrCreateByEmail($magentoCustomer->getEmail());
86
        $request = $this->contactRequestBuilder->buildWithMagentoCustomer($magentoCustomer);
87
88
        try {
89
            $apiResponse = $this->client->getContactApi()->upsert(['contact' => $request]);
90
        } catch (UnprocessableEntityHttpException $e) {
91
            $this->logger->error($e->getMessage());
92
            $this->logger->error(print_r($e->getResponseErrors(), true));
93
            return;
94
        } catch (HttpException $e) {
95
            $this->logger->error($e->getMessage());
96
            return;
97
        }
98
99
        $contact->setActiveCampaignId($apiResponse['contact']['id']);
100
        $this->contactRepository->save($contact);
101
    }
102
}
103