Passed
Pull Request — master (#58)
by
unknown
03:29
created

AddProductToWishlistHandler   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 15
c 2
b 0
f 0
dl 0
loc 35
rs 10
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 12 3
A __construct() 0 11 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace BitBag\SyliusWishlistPlugin\CommandHandler\Wishlist;
6
7
use BitBag\SyliusWishlistPlugin\Command\Wishlist\AddProductToWishlist;
8
use BitBag\SyliusWishlistPlugin\Entity\WishlistInterface;
9
use BitBag\SyliusWishlistPlugin\Factory\WishlistProductFactoryInterface;
10
use BitBag\SyliusWishlistPlugin\Repository\WishlistRepositoryInterface;
11
use BitBag\SyliusWishlistPlugin\Updater\WishlistUpdaterInterface;
12
use Sylius\Component\Core\Repository\ProductRepositoryInterface;
13
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
14
use Symfony\Component\Messenger\Handler\MessageHandlerInterface;
15
16
class AddProductToWishlistHandler implements MessageHandlerInterface
17
{
18
    private WishlistProductFactoryInterface $wishlistProductFactory;
19
20
    private WishlistUpdaterInterface $wishlistUpdater;
21
22
    private ProductRepositoryInterface $productRepository;
23
24
    private WishlistRepositoryInterface $wishlistRepository;
25
26
    public function __construct(
27
        WishlistProductFactoryInterface $wishlistProductFactory,
28
        WishlistRepositoryInterface $wishlistRepository,
29
        WishlistUpdaterInterface $wishlistUpdater,
30
        ProductRepositoryInterface $productRepository
31
    )
32
    {
33
        $this->wishlistProductFactory = $wishlistProductFactory;
34
        $this->wishlistUpdater = $wishlistUpdater;
35
        $this->productRepository = $productRepository;
36
        $this->wishlistRepository = $wishlistRepository;
37
    }
38
39
    public function __invoke(AddProductToWishlist $addProductToWishlist): WishlistInterface
40
    {
41
        $product = $this->productRepository->find($addProductToWishlist->productId);
42
        $wishlist = $this->wishlistRepository->findByToken($addProductToWishlist->getWishlistTokenValue());
43
44
        if (null === $product || null === $wishlist) {
45
            throw new NotFoundHttpException();
46
        }
47
48
        $wishlistProduct = $this->wishlistProductFactory->createForWishlistAndProduct($wishlist, $product);
49
50
        return $this->wishlistUpdater->addProductToWishlist($wishlist, $wishlistProduct);
51
    }
52
}
53