Passed
Pull Request — master (#58)
by
unknown
04:56
created

RemoveProductVariantFromWishlistHandler   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 23
c 1
b 0
f 0
dl 0
loc 49
rs 10
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 1
A __invoke() 0 26 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace BitBag\SyliusWishlistPlugin\CommandHandler\Wishlist;
6
7
use BitBag\SyliusWishlistPlugin\Command\Wishlist\RemoveProductVariantFromWishlist;
8
use BitBag\SyliusWishlistPlugin\Exception\ProductVariantNotFoundException;
9
use BitBag\SyliusWishlistPlugin\Exception\WishlistNotFoundException;
10
use BitBag\SyliusWishlistPlugin\Repository\WishlistRepositoryInterface;
11
use Doctrine\Persistence\ObjectManager;
12
use Sylius\Component\Core\Repository\ProductVariantRepositoryInterface;
13
use Sylius\Component\Resource\Repository\RepositoryInterface;
14
use Symfony\Component\Messenger\Handler\MessageHandlerInterface;
15
16
final class RemoveProductVariantFromWishlistHandler implements MessageHandlerInterface
17
{
18
    private WishlistRepositoryInterface $wishlistRepository;
19
20
    private ProductVariantRepositoryInterface $productVariantRepository;
21
22
    private RepositoryInterface $wishlistProductRepository;
23
24
    private ObjectManager $wishlistManager;
25
26
    public function __construct(
27
        WishlistRepositoryInterface $wishlistRepository,
28
        ProductVariantRepositoryInterface $productVariantRepository,
29
        RepositoryInterface $wishlistProductRepository,
30
        ObjectManager $wishlistManager
31
    )
32
    {
33
        $this->wishlistRepository = $wishlistRepository;
34
        $this->productVariantRepository = $productVariantRepository;
35
        $this->wishlistProductRepository = $wishlistProductRepository;
36
        $this->wishlistManager = $wishlistManager;
37
    }
38
39
    public function __invoke(RemoveProductVariantFromWishlist $removeProductVariantFromWishlist)
40
    {
41
        $variantId = $removeProductVariantFromWishlist->getProductVariantIdValue();
42
        $token = $removeProductVariantFromWishlist->getWishlistTokenValue();
43
44
        $variant = $this->productVariantRepository->find($variantId);
45
        $wishlistProduct = $this->wishlistProductRepository->findOneBy(['variant' => $variant]);
46
47
        $wishlist = $this->wishlistRepository->findByToken($token);
48
49
        if(null === $variant || null === $wishlistProduct) {
50
            throw new ProductVariantNotFoundException(
51
                sprintf("The Product %s does not exist", $variantId)
52
            );
53
        }
54
55
        if (null === $wishlist) {
56
            throw new WishlistNotFoundException(
57
                sprintf("The Wishlist %s does not exist", $token)
58
            );
59
        }
60
61
        $wishlist->removeProductVariant($variant);
62
        $this->wishlistManager->flush();
63
64
        return $wishlist;
65
    }
66
}
67