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

CustomerRepository::delete()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 2
eloc 5
c 1
b 0
f 1
nc 2
nop 1
dl 0
loc 9
rs 10
1
<?php
2
declare(strict_types=1);
3
/**
4
 */
5
6
namespace CommerceLeague\ActiveCampaign\Model;
7
8
use CommerceLeague\ActiveCampaign\Api\CustomerRepositoryInterface;
9
use CommerceLeague\ActiveCampaign\Api\Data;
10
use CommerceLeague\ActiveCampaign\Model\ResourceModel\Customer as CustomerResource;
11
use Magento\Customer\Model\Customer as MagentoCustomer;
12
use Magento\Framework\Exception\CouldNotDeleteException;
13
use Magento\Framework\Exception\CouldNotSaveException;
14
use Magento\Framework\Exception\NoSuchEntityException;
15
use Magento\Framework\Model\AbstractModel;
16
17
/**
18
 * Class CustomerRepository
19
 */
20
class CustomerRepository implements CustomerRepositoryInterface
21
{
22
    /**
23
     * @var CustomerResource
24
     */
25
    private $customerResource;
26
27
    /**
28
     * @var CustomerFactory
0 ignored issues
show
Bug introduced by
The type CommerceLeague\ActiveCam...n\Model\CustomerFactory 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...
29
     */
30
    private $customerFactory;
31
32
    /**
33
     * @param CustomerResource $customerResource
34
     * @param CustomerFactory $customerFactory
35
     */
36
    public function __construct(
37
        CustomerResource $customerResource,
38
        CustomerFactory $customerFactory
39
    ) {
40
        $this->customerResource = $customerResource;
41
        $this->customerFactory = $customerFactory;
42
    }
43
44
    /**
45
     * @param Data\CustomerInterface|AbstractModel $customer
46
     * @return Data\CustomerInterface
47
     * @throws CouldNotSaveException
48
     */
49
    public function save(Data\CustomerInterface $customer): Data\CustomerInterface
50
    {
51
        try {
52
            $this->customerResource->save($customer);
53
        } catch (\Exception $e) {
54
            throw new CouldNotSaveException(__($e->getMessage()));
55
        }
56
57
        return $customer;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $customer returns the type Magento\Framework\Model\AbstractModel which is incompatible with the type-hinted return CommerceLeague\ActiveCam...\Data\CustomerInterface.
Loading history...
58
    }
59
60
    /**
61
     * @inheritDoc
62
     */
63
    public function getById($entityId): Data\CustomerInterface
64
    {
65
        /** @var Customer $customer */
66
        $customer = $this->customerFactory->create();
67
        $this->customerResource->load($customer, $entityId);
68
69
        return $customer;
70
    }
71
72
    public function getByMagentoCustomerId($magentoCustomerId): Data\CustomerInterface
73
    {
74
        $customer = $this->customerFactory->create();
75
        $this->customerResource->load(
76
            $customer,
77
            $magentoCustomerId,
78
            Data\CustomerInterface::MAGENTO_CUSTOMER_ID
79
        );
80
81
        return $customer;
82
    }
83
84
    /**
85
     * @inheritDoc
86
     */
87
    public function getOrCreateByMagentoCustomerId($magentoCustomerId): Data\CustomerInterface
88
    {
89
        $customer = $this->getByMagentoCustomerId($magentoCustomerId);
90
91
        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...
92
            $customer->setMagentoCustomerId($magentoCustomerId);
93
            $this->save($customer);
94
        }
95
96
        return $customer;
97
    }
98
99
    /**
100
     * @param Data\CustomerInterface|AbstractModel $customer
101
     * @return bool
102
     * @throws CouldNotDeleteException
103
     */
104
    public function delete(Data\CustomerInterface $customer): bool
105
    {
106
        try {
107
            $this->customerResource->delete($customer);
108
        } catch (\Exception $e) {
109
            throw new CouldNotDeleteException(__($e->getMessage()));
110
        }
111
112
        return true;
113
    }
114
115
    /**
116
     * @inheritDoc
117
     */
118
    public function deleteById($entityId): bool
119
    {
120
        $customer = $this->getById($entityId);
121
122
        if (!$customer->getId()) {
123
            throw new NoSuchEntityException(
124
                __('The Customer with the "%1" ID doesn\'t exist', $entityId)
125
            );
126
        }
127
128
        return $this->delete($customer);
129
    }
130
}
131