Failed Conditions
Pull Request — master (#229)
by
unknown
02:46
created

RemoveAddressHandler::handle()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 6
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Sylius\ShopApiPlugin\Handler;
6
7
use Sylius\Component\Core\Model\AddressInterface;
8
use Sylius\Component\Core\Model\OrderInterface;
9
use Sylius\Component\Core\Model\ShopUserInterface;
10
use Sylius\Component\Core\Repository\AddressRepositoryInterface;
11
use Sylius\Component\Core\Repository\OrderRepositoryInterface;
12
use Sylius\Component\Resource\Repository\RepositoryInterface;
13
use Sylius\ShopApiPlugin\Command\RemoveAddress;
14
use Webmozart\Assert\Assert;
15
16
final class RemoveAddressHandler
17
{
18
    /**
19
     * @var AddressRepositoryInterface
20
     */
21
    private $addressRepository;
22
23
    /**
24
     * @var OrderRepositoryInterface
25
     */
26
    private $orderRepository;
27
28
    /**
29
     * @var RepositoryInterface
30
     */
31
    private $shopUserRepository;
32
33
    /**
34
     * @param AddressRepositoryInterface $addressRepository
35
     * @param OrderRepositoryInterface $orderRepository
36
     * @param RepositoryInterface $shopUserRepository
37
     */
38
    public function __construct(AddressRepositoryInterface $addressRepository, OrderRepositoryInterface $orderRepository, RepositoryInterface $shopUserRepository)
39
    {
40
        $this->addressRepository = $addressRepository;
41
        $this->orderRepository = $orderRepository;
42
        $this->shopUserRepository = $shopUserRepository;
43
    }
44
45
    public function handle(RemoveAddress $removeAddress)
46
    {
47
        /** @var ShopUserInterface $shopUser */
48
        $shopUser = $this->shopUserRepository->findOneBy(['username' => $removeAddress->userEmail()]);
49
50
        /** @var AddressInterface $address */
51
        $address = $this->addressRepository->findOneBy(['id' => $removeAddress->id()]);
52
53
        $this->assertCurrentUserIsOwner($address, $shopUser);
54
        $this->assertOrderWithAddressNotExists($address);
55
56
        $this->addressRepository->remove($address);
57
    }
58
59
    private function assertOrderWithAddressNotExists($address)
60
    {
61
        /** @var OrderInterface $orderShippingAddress */
62
        $orderShippingAddress = $this->orderRepository->findBy(['billingAddress' => $address]);
63
        /** @var OrderInterface $orderBillingAddress */
64
        $orderBillingAddress = $this->orderRepository->findBy(['shippingAddress' => $address]);
65
        Assert::allIsEmpty([$orderShippingAddress, $orderBillingAddress], 'Cant delete address because it is associated with one or more orders');
66
    }
67
68
    private function assertCurrentUserIsOwner(AddressInterface $address, ShopUserInterface $user)
69
    {
70
        Assert::eq($address->getCustomer()->getId(), $user->getId(), 'User is not owner of this address');
71
    }
72
}
73