Passed
Push — main ( 662c49...013415 )
by Iain
18:11
created

CustomerCreatedSubscriber::buildPayload()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 5
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 9
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * Copyright (C) 2020-2024 Iain Cambridge
7
 *
8
 *     This program is free software: you can redistribute it and/or modify
9
 *     it under the terms of the GNU General Public License as published by
10
 *     the Free Software Foundation, either version 3 of the License, or
11
 *     (at your option) any later version.
12
 *
13
 *     This program is distributed in the hope that it will be useful,
14
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 *     GNU General Public License for more details.
17
 *
18
 *     You should have received a copy of the GNU General Public License
19
 *     along with this program.  If not, see <https://www.gnu.org/licenses/>.
20
 */
21
22
namespace Parthenon\Billing\Subscriber;
23
24
use Parthenon\Billing\BillaBear\CustomerInterface;
25
use Parthenon\Billing\BillaBear\SdkFactory;
26
use Parthenon\Billing\Exception\NoCustomerException;
27
use Parthenon\Common\Address;
28
use Parthenon\User\Entity\MemberInterface;
29
use Parthenon\User\Entity\UserInterface;
30
use Parthenon\User\Event\PostUserSignupEvent;
31
use Parthenon\User\Repository\UserRepositoryInterface;
32
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
33
34
final class CustomerCreatedSubscriber implements EventSubscriberInterface
35
{
36
    public function __construct(
37
        private UserRepositoryInterface $userRepository,
38
        private SdkFactory $sdkFactory,
39
    ) {
40
    }
41
42
    public static function getSubscribedEvents()
43
    {
44
        return [
45
            PostUserSignupEvent::NAME => ['userCreated', 0],
46
        ];
47
    }
48
49
    public function userCreated(PostUserSignupEvent $event)
50
    {
51
        $user = $event->getUser();
52
53
        if ($user instanceof MemberInterface && $user->getTeam() instanceof CustomerInterface) {
54
            $customer = $user->getTeam();
55
        } elseif ($user instanceof CustomerInterface) {
56
            $customer = $user;
57
        } else {
58
            throw new NoCustomerException(sprintf('There is no BillaBear customer'));
59
        }
60
61
        $payload = $this->buildPayload($user, $customer);
62
        $response = $this->sdkFactory->createCustomersApi()->createCustomer($payload);
63
        $customer->setCustomerId($response->getId());
64
65
        $this->userRepository->save($customer);
66
    }
67
68
    private function buildPayload(UserInterface $user, CustomerInterface $internalCustomer): \BillaBear\Model\Customer
0 ignored issues
show
Bug introduced by
The type BillaBear\Model\Customer 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...
69
    {
70
        $customer = new \BillaBear\Model\Customer();
71
        $customer->setEmail($user->getEmail());
72
        if ($internalCustomer->hasBillingAddress()) {
73
            $customer->setAddress($this->convertBillingAddress($internalCustomer->getBillingAddress()));
74
        }
75
76
        return $customer;
77
    }
78
79
    private function convertBillingAddress(Address $address): \BillaBear\Model\Address
0 ignored issues
show
Bug introduced by
The type BillaBear\Model\Address 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...
80
    {
81
        $apiAddress = new \BillaBear\Model\Address();
82
        $apiAddress->setStreetLineOne($address->getStreetLineOne());
83
        $apiAddress->setStreetLineTwo($address->getStreetLineTwo());
84
        $apiAddress->setCity($address->getCity());
85
        $apiAddress->setCountry($address->getCountry());
86
        $apiAddress->setPostcode($address->getPostcode());
87
88
        return $apiAddress;
89
    }
90
}
91