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 ( f1cd39...fb8f78 )
by Andreas
03:42
created

SyncContactConsumer   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 23
dl 0
loc 59
rs 10
c 0
b 0
f 0
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A consume() 0 24 4
1
<?php
2
declare(strict_types=1);
3
/**
4
 */
5
namespace CommerceLeague\ActiveCampaign\MessageQueue;
6
7
use CommerceLeague\ActiveCampaign\Api\ContactRepositoryInterface;
8
use CommerceLeague\ActiveCampaign\Helper\Client as ClientHelper;
9
use CommerceLeague\ActiveCampaign\Logger\Logger;
10
use CommerceLeague\ActiveCampaignApi\Exception\HttpException;
11
use Magento\Framework\Exception\CouldNotSaveException;
12
13
/**
14
 * Class CreateUpdateConsumer
15
 */
16
class SyncContactConsumer
17
{
18
    /**
19
     * @var ContactRepositoryInterface
20
     */
21
    private $contactRepository;
22
23
    /**
24
     * @var Logger
25
     */
26
    private $logger;
27
28
    /**
29
     * @var ClientHelper
30
     */
31
    private $clientHelper;
32
33
    /**
34
     * @param ContactRepositoryInterface $contactRepository
35
     * @param Logger $logger
36
     * @param ClientHelper $clientHelper
37
     */
38
    public function __construct(
39
        ContactRepositoryInterface $contactRepository,
40
        Logger $logger,
41
        ClientHelper $clientHelper
42
    ) {
43
        $this->contactRepository = $contactRepository;
44
        $this->logger = $logger;
45
        $this->clientHelper = $clientHelper;
46
    }
47
48
    /**
49
     * @param string $message
50
     */
51
    public function consume(string $message): void
52
    {
53
        $message = json_decode($message, true);
54
55
        try {
56
            $contact = $this->contactRepository->getOrCreateByEmail($message['email']);
57
        } catch (CouldNotSaveException $e) {
58
            $this->logger->error(__('Unable to find contact with email "%1".', $message['email']));
59
            return;
60
        }
61
62
        try {
63
            $apiResponse = $this->clientHelper->getContactApi()->upsert(['contact' => $message['request']]);
64
        } catch (HttpException $e) {
65
            $this->logger->error($e->getMessage());
66
            return;
67
        }
68
69
        $contact->setActiveCampaignId($apiResponse['contact']['id']);
70
71
        try {
72
            $this->contactRepository->save($contact);
73
        } catch (CouldNotSaveException $e) {
74
            $this->logger->error($e->getMessage());
75
        }
76
    }
77
}
78