Completed
Push — master ( 4c9fb6...4ad644 )
by
unknown
25s queued 11s
created

RemoveProductFromWishlistHandler   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 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\RemoveProductFromWishlist;
8
use BitBag\SyliusWishlistPlugin\Entity\WishlistInterface;
9
use BitBag\SyliusWishlistPlugin\Exception\ProductNotFoundException;
10
use BitBag\SyliusWishlistPlugin\Exception\WishlistNotFoundException;
11
use BitBag\SyliusWishlistPlugin\Repository\WishlistRepositoryInterface;
12
use Doctrine\Persistence\ObjectManager;
13
use Sylius\Component\Core\Repository\ProductRepositoryInterface;
14
use Sylius\Component\Resource\Repository\RepositoryInterface;
15
use Symfony\Component\Messenger\Handler\MessageHandlerInterface;
16
17
final class RemoveProductFromWishlistHandler implements MessageHandlerInterface
18
{
19
    private ProductRepositoryInterface $productRepository;
20
21
    private WishlistRepositoryInterface $wishlistRepository;
22
23
    private RepositoryInterface $wishlistProductRepository;
24
25
    private ObjectManager $wishlistManager;
26
27
    public function __construct(
28
        ProductRepositoryInterface $productRepository,
29
        WishlistRepositoryInterface $wishlistRepository,
30
        RepositoryInterface $wishlistProductRepository,
31
        ObjectManager $wishlistManager
32
    ) {
33
        $this->productRepository = $productRepository;
34
        $this->wishlistRepository = $wishlistRepository;
35
        $this->wishlistProductRepository = $wishlistProductRepository;
36
        $this->wishlistManager = $wishlistManager;
37
    }
38
39
    public function __invoke(RemoveProductFromWishlist $removeProductFromWishlist): WishlistInterface
40
    {
41
        $productId = $removeProductFromWishlist->getProductIdValue();
42
        $token = $removeProductFromWishlist->getWishlistTokenValue();
43
44
        $product = $this->productRepository->find($productId);
45
        $wishlistProduct = $this->wishlistProductRepository->findOneBy(['product' => $product]);
46
47
        $wishlist = $this->wishlistRepository->findByToken($token);
48
49
        if (null === $product || null === $wishlistProduct) {
50
            throw new ProductNotFoundException(
51
                sprintf('The Product %s does not exist', $productId)
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 = $wishlist->removeProduct($wishlistProduct);
62
        $this->wishlistManager->flush();
63
64
        return $wishlist;
65
    }
66
}
67