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 ( c8c4e6...db9143 )
by Andreas
03:30
created

ExportContactConsumer::__construct()   A

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 Magento\Customer\Api\CustomerRepositoryInterface as MagentoCustomerRepositoryInterface;
14
use Magento\Framework\Exception\CouldNotSaveException;
15
use Magento\Framework\Exception\LocalizedException;
16
use Magento\Framework\Exception\NoSuchEntityException;
17
18
/**
19
 * Class ExportContactConsumer
20
 */
21
class ExportContactConsumer implements ConsumerInterface
22
{
23
    /**
24
     * @var MagentoCustomerRepositoryInterface
25
     */
26
    private $magentoCustomerRepository;
27
28
    /**
29
     * @var Logger
30
     */
31
    private $logger;
32
33
    /**
34
     * @var ContactRepositoryInterface
35
     */
36
    private $contactRepository;
37
38
    /**
39
     * @var ContactRequestBuilder
40
     */
41
    private $contactRequestBuilder;
42
43
    /**
44
     * @var Client
45
     */
46
    private $client;
47
48
    /**
49
     * @param MagentoCustomerRepositoryInterface $magentoCustomerRepository
50
     * @param Logger $logger
51
     * @param ContactRequestBuilder $contactRequestBuilder
52
     * @param ContactRepositoryInterface $contactRepository
53
     * @param Client $client
54
     */
55
    public function __construct(
56
        MagentoCustomerRepositoryInterface $magentoCustomerRepository,
57
        Logger $logger,
58
        ContactRepositoryInterface $contactRepository,
59
        ContactRequestBuilder $contactRequestBuilder,
60
        Client $client
61
    ) {
62
        $this->magentoCustomerRepository = $magentoCustomerRepository;
63
        $this->logger = $logger;
64
        $this->contactRepository = $contactRepository;
65
        $this->contactRequestBuilder = $contactRequestBuilder;
66
        $this->client = $client;
67
    }
68
69
    /**
70
     * @param string $message
71
     * @throws CouldNotSaveException
72
     */
73
    public function consume(string $message): void
74
    {
75
        $message = json_decode($message, true);
76
77
        try {
78
            $magentoCustomer = $this->magentoCustomerRepository->getById($message['magento_customer_id']);
79
        } catch (NoSuchEntityException|LocalizedException $e) {
80
            $this->logger->error($e->getMessage());
81
            return;
82
        }
83
84
        $contact = $this->contactRepository->getOrCreateByEmail($magentoCustomer->getEmail());
85
        $request = $this->contactRequestBuilder->buildWithMagentoCustomer($magentoCustomer);
86
87
        try {
88
            $apiResponse = $this->client->getContactApi()->upsert(['contact' => $request]);
89
        } catch (HttpException $e) {
90
            $this->logger->error($e->getMessage());
91
            return;
92
        }
93
94
        $contact->setActiveCampaignId($apiResponse['contact']['id']);
95
        $this->contactRepository->save($contact);
96
    }
97
}
98