CreateCustomerAction::supports()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
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\Action\Api;
13
14
use BitBag\SyliusMolliePlugin\Entity\MollieCustomer;
15
use BitBag\SyliusMolliePlugin\Entity\MollieCustomerInterface;
16
use BitBag\SyliusMolliePlugin\Logger\MollieLoggerActionInterface;
17
use BitBag\SyliusMolliePlugin\Request\Api\CreateCustomer;
18
use Mollie\Api\Exceptions\ApiException;
19
use Payum\Core\Action\ActionInterface;
20
use Payum\Core\ApiAwareInterface;
21
use Payum\Core\Bridge\Spl\ArrayObject;
22
use Payum\Core\Exception\RequestNotSupportedException;
23
use Sylius\Component\Resource\Repository\RepositoryInterface;
24
25
final class CreateCustomerAction extends BaseApiAwareAction implements ActionInterface, ApiAwareInterface
26
{
27
    /** @var MollieLoggerActionInterface */
28
    private $loggerAction;
29
30
    /** @var RepositoryInterface */
31
    private $mollieCustomerRepository;
32
33
    public function __construct(MollieLoggerActionInterface $loggerAction, RepositoryInterface $mollieCustomerRepository)
34
    {
35
        $this->loggerAction = $loggerAction;
36
        $this->mollieCustomerRepository = $mollieCustomerRepository;
37
    }
38
39
    /** @param CreateCustomer $request */
40
    public function execute($request): void
41
    {
42
        RequestNotSupportedException::assertSupports($this, $request);
43
        $model = ArrayObject::ensureArrayObject($request->getModel());
44
45
        $data = [
46
            'name' => $model['fullName'],
47
            'email' => $model['email'],
48
        ];
49
50
        /** @var MollieCustomerInterface $customer */
51
        $customer = $this->mollieCustomerRepository->findOneBy(['email' => $model['email']]);
52
53
        if (null === $customer) {
54
            $customer = new MollieCustomer();
55
            $customer->setEmail($model['email']);
56
        }
57
58
        try {
59
            if (empty($customer->getProfileId())) {
60
                $customerMollie = $this->mollieApiClient->customers->create($data);
61
                $customer->setProfileId($customerMollie->id);
62
63
                $this->mollieCustomerRepository->add($customer);
64
            }
65
        } catch (\Exception $e) {
66
            $this->loggerAction->addNegativeLog(sprintf('Error with create customer:  %s', $e->getMessage()));
67
68
            throw new ApiException('Error with create customer with' . $e->getMessage());
69
        }
70
71
        $this->loggerAction->addLog(sprintf('Create customer action with id:  %s', $customer->getProfileId()));
72
73
        $model['customer_mollie_id'] = $customer->getProfileId();
74
    }
75
76
    public function supports($request): bool
77
    {
78
        return
79
            $request instanceof CreateCustomer &&
80
            $request->getModel() instanceof \ArrayAccess
81
            ;
82
    }
83
}
84