RemoveProductFromWishlistHandler   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 1
A __invoke() 0 28 4
1
<?php
2
3
/*
4
 * This file was created by developers working at BitBag
5
 * Do you need more information about us and what we do? Visit our https://bitbag.io website!
6
 * We are hiring developers from all over the world. Join us and start your new, exciting adventure and become part of us: https://bitbag.io/career
7
*/
8
9
declare(strict_types=1);
10
11
namespace BitBag\SyliusWishlistPlugin\CommandHandler\Wishlist;
12
13
use BitBag\SyliusWishlistPlugin\Command\Wishlist\RemoveProductFromWishlist;
14
use BitBag\SyliusWishlistPlugin\Entity\WishlistInterface;
15
use BitBag\SyliusWishlistPlugin\Entity\WishlistProductInterface;
16
use BitBag\SyliusWishlistPlugin\Exception\ProductNotFoundException;
17
use BitBag\SyliusWishlistPlugin\Exception\WishlistNotFoundException;
18
use BitBag\SyliusWishlistPlugin\Repository\WishlistRepositoryInterface;
19
use Doctrine\Persistence\ObjectManager;
20
use Sylius\Component\Core\Model\ProductInterface;
21
use Sylius\Component\Core\Repository\ProductRepositoryInterface;
22
use Sylius\Component\Resource\Repository\RepositoryInterface;
23
use Symfony\Component\Messenger\Handler\MessageHandlerInterface;
24
25
final class RemoveProductFromWishlistHandler implements MessageHandlerInterface
26
{
27
    private ProductRepositoryInterface $productRepository;
28
29
    private WishlistRepositoryInterface $wishlistRepository;
30
31
    private RepositoryInterface $wishlistProductRepository;
32
33
    private ObjectManager $wishlistManager;
34
35
    public function __construct(
36
        ProductRepositoryInterface $productRepository,
37
        WishlistRepositoryInterface $wishlistRepository,
38
        RepositoryInterface $wishlistProductRepository,
39
        ObjectManager $wishlistManager
40
    ) {
41
        $this->productRepository = $productRepository;
42
        $this->wishlistRepository = $wishlistRepository;
43
        $this->wishlistProductRepository = $wishlistProductRepository;
44
        $this->wishlistManager = $wishlistManager;
45
    }
46
47
    public function __invoke(RemoveProductFromWishlist $removeProductFromWishlist): WishlistInterface
48
    {
49
        $productId = $removeProductFromWishlist->getProductIdValue();
50
        $token = $removeProductFromWishlist->getWishlistTokenValue();
51
52
        /** @var ?ProductInterface $product */
53
        $product = $this->productRepository->find($productId);
54
        /** @var ?WishlistProductInterface $wishlistProduct */
55
        $wishlistProduct = $this->wishlistProductRepository->findOneBy(['product' => $product]);
56
        /** @var ?WishlistInterface $wishlist */
57
        $wishlist = $this->wishlistRepository->findByToken($token);
58
59
        if (null === $product || null === $wishlistProduct) {
60
            throw new ProductNotFoundException(
61
                sprintf('The Product %s does not exist', $productId)
62
            );
63
        }
64
65
        if (null === $wishlist) {
66
            throw new WishlistNotFoundException(
67
                sprintf('The Wishlist %s does not exist', $token)
68
            );
69
        }
70
71
        $wishlist = $wishlist->removeProduct($wishlistProduct);
72
        $this->wishlistManager->flush();
73
74
        return $wishlist;
75
    }
76
}
77