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 ( 5543fb...d29718 )
by Andreas
03:19
created

ContactRepository   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 109
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 11
eloc 31
c 0
b 0
f 0
dl 0
loc 109
rs 10

7 Methods

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