1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file has been created by developers from BitBag. |
5
|
|
|
* Feel free to contact us once you face any issues or want to start |
6
|
|
|
* You can find more information about us on https://bitbag.io and write us |
7
|
|
|
* an email on [email protected]. |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
declare(strict_types=1); |
11
|
|
|
|
12
|
|
|
namespace BitBag\SyliusMolliePlugin\Resolver\Address; |
13
|
|
|
|
14
|
|
|
use BitBag\SyliusMolliePlugin\Validator\ApplePayDirect\ApplePayAddressValidatorInterface; |
15
|
|
|
use Sylius\Component\Core\Model\AddressInterface; |
16
|
|
|
use Sylius\Component\Core\Model\CustomerInterface; |
17
|
|
|
use Sylius\Component\Resource\Factory\FactoryInterface; |
18
|
|
|
use Sylius\Component\Resource\Repository\RepositoryInterface; |
19
|
|
|
|
20
|
|
|
final class AddressResolver implements AddressResolverInterface |
21
|
|
|
{ |
22
|
|
|
/** @var ApplePayAddressValidatorInterface */ |
23
|
|
|
private $addressValidator; |
24
|
|
|
|
25
|
|
|
/** @var RepositoryInterface */ |
26
|
|
|
private $customerRepository; |
27
|
|
|
|
28
|
|
|
/** @var FactoryInterface */ |
29
|
|
|
private $addressFactory; |
30
|
|
|
|
31
|
|
|
/** @var FactoryInterface */ |
32
|
|
|
private $customerFactory; |
33
|
|
|
|
34
|
|
|
public function __construct( |
35
|
|
|
ApplePayAddressValidatorInterface $addressValidator, |
36
|
|
|
RepositoryInterface $customerRepository, |
37
|
|
|
FactoryInterface $addressFactory, |
38
|
|
|
FactoryInterface $customerFactory |
39
|
|
|
) { |
40
|
|
|
$this->addressValidator = $addressValidator; |
41
|
|
|
$this->customerRepository = $customerRepository; |
42
|
|
|
$this->addressFactory = $addressFactory; |
43
|
|
|
$this->customerFactory = $customerFactory; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function resolve(array $applePayDirectAddress): AddressInterface |
47
|
|
|
{ |
48
|
|
|
$this->addressValidator->validate($applePayDirectAddress); |
49
|
|
|
|
50
|
|
|
/** @var AddressInterface $address */ |
51
|
|
|
$address = $this->addressFactory->createNew(); |
52
|
|
|
|
53
|
|
|
$address->setCity($applePayDirectAddress['locality']); |
54
|
|
|
$address->setStreet(implode(' ', $applePayDirectAddress['addressLines'])); |
55
|
|
|
$address->setPostcode($applePayDirectAddress['postalCode']); |
56
|
|
|
$address->setCountryCode($applePayDirectAddress['countryCode']); |
57
|
|
|
$address->setFirstName($applePayDirectAddress['givenName']); |
58
|
|
|
$address->setLastName($applePayDirectAddress['familyName']); |
59
|
|
|
|
60
|
|
|
/** @var CustomerInterface $customer */ |
61
|
|
|
$customer = $this->customerRepository->findOneBy(['email' => $applePayDirectAddress['emailAddress']]); |
62
|
|
|
|
63
|
|
|
if (null === $customer) { |
64
|
|
|
$customer = $this->customerFactory->createNew(); |
65
|
|
|
$customer->setEmail($applePayDirectAddress['emailAddress']); |
66
|
|
|
|
67
|
|
|
$this->customerRepository->add($customer); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
$address->setCustomer($customer); |
71
|
|
|
|
72
|
|
|
return $address; |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|