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   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 26
dl 0
loc 76
rs 10
c 0
b 0
f 0
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 12 1
A consume() 0 25 3
1
<?php
2
declare(strict_types=1);
3
/**
4
 */
5
6
namespace CommerceLeague\ActiveCampaign\MessageQueue\Newsletter;
7
8
use CommerceLeague\ActiveCampaign\Api\ContactRepositoryInterface;
9
use CommerceLeague\ActiveCampaign\Gateway\Client;
10
use CommerceLeague\ActiveCampaign\Gateway\Request\ContactBuilder as ContactRequestBuilder;
11
use CommerceLeague\ActiveCampaign\Logger\Logger;
12
use CommerceLeague\ActiveCampaign\MessageQueue\ConsumerInterface;
13
use CommerceLeague\ActiveCampaignApi\Exception\HttpException;
14
use Magento\Framework\Exception\CouldNotSaveException;
15
use Magento\Newsletter\Model\SubscriberFactory;
0 ignored issues
show
Bug introduced by
The type Magento\Newsletter\Model\SubscriberFactory was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
16
use Magento\Newsletter\Model\Subscriber;
17
18
/**
19
 * Class ExportContactConsumer
20
 */
21
class ExportContactConsumer implements ConsumerInterface
22
{
23
    /**
24
     * @var SubscriberFactory
25
     */
26
    private $subscriberFactory;
27
    /**
28
     * @var Logger
29
     */
30
    private $logger;
31
32
    /**
33
     * @var ContactRepositoryInterface
34
     */
35
    private $contactRepository;
36
37
    /**
38
     * @var ContactRequestBuilder
39
     */
40
    private $contactRequestBuilder;
41
42
    /**
43
     * @var Client
44
     */
45
    private $client;
46
47
    /**
48
     * @param SubscriberFactory $subscriberFactory
49
     * @param Logger $logger
50
     * @param ContactRepositoryInterface $contactRepository
51
     * @param ContactRequestBuilder $contactRequestBuilder
52
     * @param Client $client
53
     */
54
    public function __construct(
55
        SubscriberFactory $subscriberFactory,
56
        Logger $logger,
57
        ContactRepositoryInterface $contactRepository,
58
        ContactRequestBuilder $contactRequestBuilder,
59
        Client $client
60
    ) {
61
        $this->subscriberFactory = $subscriberFactory;
62
        $this->logger = $logger;
63
        $this->contactRepository = $contactRepository;
64
        $this->contactRequestBuilder = $contactRequestBuilder;
65
        $this->client = $client;
66
    }
67
68
    /**
69
     * @param string $message
70
     * @throws CouldNotSaveException
71
     */
72
    public function consume(string $message): void
73
    {
74
        $message = json_decode($message, true);
75
76
        /** @var Subscriber $subscriber */
77
        $subscriber = $this->subscriberFactory->create();
78
        $subscriber = $subscriber->loadByEmail($message['email']);
79
80
        if (!$subscriber->getId()) {
81
            $this->logger->error(__('The Subscriber with the "%1" email doesn\'t exist', $message['email']));
82
            return;
83
        }
84
85
        $contact = $this->contactRepository->getOrCreateByEmail($subscriber->getEmail());
86
        $request = $this->contactRequestBuilder->buildWithSubscriber($subscriber);
87
88
        try {
89
            $apiResponse = $this->client->getContactApi()->upsert(['contact' => $request]);
90
        } catch (HttpException $e) {
91
            $this->logger->error($e->getMessage());
92
            return;
93
        }
94
95
        $contact->setActiveCampaignId($apiResponse['contact']['id']);
96
        $this->contactRepository->save($contact);
97
    }
98
}
99