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
|
|
|
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 |
|
|
|
|
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; |
|
|
|
|
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
|
|
|
|
Let?s assume that you have a directory layout like this:
and let?s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/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 beforeOtherDir/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: