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

ContactRepository::getOrCreateByEmail()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 1
dl 0
loc 10
rs 10
c 0
b 0
f 0
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;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, CommerceLeague\ActiveCampaign\Model\Customer. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
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
use Magento\Newsletter\Model\Subscriber;
16
17
/**
18
 * Class ContactRepository
19
 */
20
class ContactRepository implements ContactRepositoryInterface
21
{
22
    /**
23
     * @var ContactResource
24
     */
25
    private $contactResource;
26
27
    /**
28
     * @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...
29
     */
30
    private $contactFactory;
31
32
    /**
33
     * @param ContactResource $contactResource
34
     * @param ContactFactory $contactFactory
35
     */
36
    public function __construct(
37
        ContactResource $contactResource,
38
        ContactFactory $contactFactory
39
    ) {
40
        $this->contactResource = $contactResource;
41
        $this->contactFactory = $contactFactory;
42
    }
43
44
    /**
45
     * @param Data\ContactInterface|AbstractModel $contact
46
     * @return Data\ContactInterface
47
     * @throws CouldNotSaveException
48
     */
49
    public function save(Data\ContactInterface $contact): Data\ContactInterface
50
    {
51
        try {
52
            $this->contactResource->save($contact);
53
        } catch (\Exception $e) {
54
            throw new CouldNotSaveException(__($e->getMessage()));
55
        }
56
57
        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...
58
    }
59
60
    /**
61
     * @inheritDoc
62
     */
63
    public function getById($entityId): Data\ContactInterface
64
    {
65
        /** @var Contact $contact */
66
        $contact = $this->contactFactory->create();
67
        $this->contactResource->load($contact, $entityId);
68
69
        return $contact;
70
    }
71
72
    /**
73
     * @inheritDoc
74
     */
75
    public function getByEmail($email): Data\ContactInterface
76
    {
77
        /** @var Contact $contact */
78
        $contact = $this->contactFactory->create();
79
        $this->contactResource->load($contact, $email, Data\ContactInterface::EMAIL);
80
81
        return $contact;
82
    }
83
84
    /**
85
     * @inheritDoc
86
     */
87
    public function getOrCreateByEmail($email): Data\ContactInterface
88
    {
89
        $contact = $this->getByEmail($email);
90
91
        if (!$contact->getId()) {
92
            $contact->setEmail($email);
93
            $this->save($contact);
94
        }
95
96
        return $contact;
97
    }
98
99
    /**
100
     * @param Data\ContactInterface|AbstractModel $contact
101
     * @return bool
102
     * @throws CouldNotDeleteException
103
     */
104
    public function delete(Data\ContactInterface $contact): bool
105
    {
106
        try {
107
            $this->contactResource->delete($contact);
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
        $contact = $this->getById($entityId);
121
122
        if (!$contact->getId()) {
123
            throw new NoSuchEntityException(
124
                __('The Contact with the "%1" ID doesn\'t exist', $entityId)
125
            );
126
        }
127
128
        return $this->delete($contact);
129
    }
130
}
131