1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Sylius package. |
5
|
|
|
* |
6
|
|
|
* (c) Paweł Jędrzejewski |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
declare(strict_types=1); |
13
|
|
|
|
14
|
|
|
namespace Sylius\Component\Core\Model; |
15
|
|
|
|
16
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
17
|
|
|
use Doctrine\Common\Collections\Collection; |
18
|
|
|
use Sylius\Component\Customer\Model\Customer as BaseCustomer; |
19
|
|
|
use Sylius\Component\User\Model\UserInterface as BaseUserInterface; |
20
|
|
|
use Webmozart\Assert\Assert; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @author Michał Marcinkowski <[email protected]> |
24
|
|
|
*/ |
25
|
|
|
class Customer extends BaseCustomer implements CustomerInterface |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* @var Collection|OrderInterface[] |
29
|
|
|
*/ |
30
|
|
|
protected $orders; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var AddressInterface |
34
|
|
|
*/ |
35
|
|
|
protected $defaultAddress; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var Collection|AddressInterface[] |
39
|
|
|
*/ |
40
|
|
|
protected $addresses; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var ShopUserInterface |
44
|
|
|
*/ |
45
|
|
|
protected $user; |
46
|
|
|
|
47
|
|
|
public function __construct() |
48
|
|
|
{ |
49
|
|
|
parent::__construct(); |
50
|
|
|
|
51
|
|
|
$this->orders = new ArrayCollection(); |
52
|
|
|
$this->addresses = new ArrayCollection(); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* {@inheritdoc} |
57
|
|
|
*/ |
58
|
|
|
public function getOrders(): Collection |
59
|
|
|
{ |
60
|
|
|
return $this->orders; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* {@inheritdoc} |
65
|
|
|
*/ |
66
|
|
|
public function getDefaultAddress(): ?AddressInterface |
67
|
|
|
{ |
68
|
|
|
return $this->defaultAddress; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* {@inheritdoc} |
73
|
|
|
*/ |
74
|
|
|
public function setDefaultAddress(?AddressInterface $defaultAddress): void |
75
|
|
|
{ |
76
|
|
|
$this->defaultAddress = $defaultAddress; |
77
|
|
|
|
78
|
|
|
if (null !== $defaultAddress) { |
79
|
|
|
$this->addAddress($defaultAddress); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* {@inheritdoc} |
85
|
|
|
*/ |
86
|
|
|
public function addAddress(AddressInterface $address): void |
87
|
|
|
{ |
88
|
|
|
if (!$this->hasAddress($address)) { |
89
|
|
|
$this->addresses[] = $address; |
90
|
|
|
$address->setCustomer($this); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* {@inheritdoc} |
96
|
|
|
*/ |
97
|
|
|
public function removeAddress(AddressInterface $address): void |
98
|
|
|
{ |
99
|
|
|
$this->addresses->removeElement($address); |
100
|
|
|
$address->setCustomer(null); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* {@inheritdoc} |
105
|
|
|
*/ |
106
|
|
|
public function hasAddress(AddressInterface $address): bool |
107
|
|
|
{ |
108
|
|
|
return $this->addresses->contains($address); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* {@inheritdoc} |
113
|
|
|
*/ |
114
|
|
|
public function getAddresses(): Collection |
115
|
|
|
{ |
116
|
|
|
return $this->addresses; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* {@inheritdoc} |
121
|
|
|
*/ |
122
|
|
|
public function getUser(): ?BaseUserInterface |
123
|
|
|
{ |
124
|
|
|
return $this->user; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* {@inheritdoc} |
129
|
|
|
*/ |
130
|
|
|
public function setUser(?BaseUserInterface $user): void |
131
|
|
|
{ |
132
|
|
|
if ($this->user === $user) { |
133
|
|
|
return; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** @var ShopUserInterface|null $user */ |
137
|
|
|
Assert::nullOrIsInstanceOf($user, ShopUserInterface::class); |
138
|
|
|
|
139
|
|
|
$previousUser = $this->user; |
140
|
|
|
$this->user = $user; |
141
|
|
|
|
142
|
|
|
if ($previousUser instanceof ShopUserInterface) { |
143
|
|
|
$previousUser->setCustomer(null); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
if ($user instanceof ShopUserInterface) { |
147
|
|
|
$user->setCustomer($this); |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* {@inheritdoc} |
153
|
|
|
*/ |
154
|
|
|
public function hasUser(): bool |
155
|
|
|
{ |
156
|
|
|
return null !== $this->user; |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
|