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

RemoveProductFromWishlistHandler::__invoke()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 27
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 4
eloc 15
c 2
b 0
f 0
nc 3
nop 1
dl 0
loc 27
rs 9.7666
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
        dump($removeProductFromWishlist);
42
        $productId = $removeProductFromWishlist->getProductIdValue();
43
        $token = $removeProductFromWishlist->getWishlistTokenValue();
44
45
        $product = $this->productRepository->find($productId);
46
        $wishlistProduct = $this->wishlistProductRepository->findOneBy(['product' => $product]);
47
48
        $wishlist = $this->wishlistRepository->findByToken($token);
49
50
        if (null === $product || null === $wishlistProduct) {
51
            throw new ProductNotFoundException(
52
                sprintf('The Product %s does not exist', $productId)
53
            );
54
        }
55
56
        if (null === $wishlist) {
57
            throw new WishlistNotFoundException(
58
                sprintf('The Wishlist %s does not exist', $token)
59
            );
60
        }
61
62
        $wishlist = $wishlist->removeProduct($wishlistProduct);
63
        $this->wishlistManager->flush();
64
65
        return $wishlist;
66
    }
67
}
68