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 ( d8327a...c6d836 )
by Andreas
03:23
created

CreateConsumer   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 22
c 1
b 0
f 0
dl 0
loc 59
rs 10
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
6
namespace CommerceLeague\ActiveCampaign\MessageQueue\Customer;
7
8
use CommerceLeague\ActiveCampaign\Api\CustomerRepositoryInterface;
9
use CommerceLeague\ActiveCampaign\Helper\Client as ClientHelper;
10
use CommerceLeague\ActiveCampaign\Logger\Logger;
11
use CommerceLeague\ActiveCampaignApi\Exception\HttpException;
12
use Magento\Framework\Exception\CouldNotSaveException;
13
14
/**
15
 * Class CreateConsumer
16
 */
17
class CreateConsumer
18
{
19
    /**
20
     * @var CustomerRepositoryInterface
21
     */
22
    private $customerRepository;
23
24
    /**
25
     * @var Logger
26
     */
27
    private $logger;
28
29
    /**
30
     * @var ClientHelper
31
     */
32
    private $clientHelper;
33
34
    /**
35
     * @param CustomerRepositoryInterface $customerRepository
36
     * @param Logger $logger
37
     * @param ClientHelper $clientHelper
38
     */
39
    public function __construct(
40
        CustomerRepositoryInterface $customerRepository,
41
        Logger $logger,
42
        ClientHelper $clientHelper
43
    ) {
44
        $this->customerRepository = $customerRepository;
45
        $this->logger = $logger;
46
        $this->clientHelper = $clientHelper;
47
    }
48
49
    /**
50
     * @param CreateMessage $message
51
     */
52
    public function consume(CreateMessage $message): void
53
    {
54
        $customer = $this->customerRepository->getById($message->getEntityId());
55
56
        if (!$customer->getId()) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $customer->getId() of type integer|null is loosely compared to false; this is ambiguous if the integer can be 0. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
57
            $this->logger->error(__('Unable to find customer with id "%1".', $message->getEntityId()));
58
            return;
59
        }
60
61
        $request = json_decode($message->getSerializedRequest(), true);
62
63
        try {
64
            $apiResponse = $this->clientHelper->getCustomerApi()->create(['ecomCustomer' => $request]);
65
        } catch (HttpException $e) {
66
            $this->logger->error($e->getMessage());
67
            return;
68
        }
69
70
        $customer->setActiveCampaignId($apiResponse['ecomCustomer']['id']);
71
72
        try {
73
            $this->customerRepository->save($customer);
74
        } catch (CouldNotSaveException $e) {
75
            $this->logger->error($e->getMessage());
76
        }
77
    }
78
}
79