Passed
Pull Request — master (#58)
by
unknown
11:33 queued 52s
created

AddProductToWishlistHandler   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 12 3
A __construct() 0 10 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
        $this->wishlistProductFactory = $wishlistProductFactory;
33
        $this->wishlistUpdater = $wishlistUpdater;
34
        $this->productRepository = $productRepository;
35
        $this->wishlistRepository = $wishlistRepository;
36
    }
37
38
    public function __invoke(AddProductToWishlist $addProductToWishlist): WishlistInterface
39
    {
40
        $product = $this->productRepository->find($addProductToWishlist->product);
41
        $wishlist = $this->wishlistRepository->findByToken($addProductToWishlist->getWishlistTokenValue());
42
43
        if (!$product || !$wishlist) {
44
            throw new NotFoundHttpException();
45
        }
46
47
        $wishlistProduct = $this->wishlistProductFactory->createForWishlistAndProduct($wishlist, $product);
48
49
        return $this->wishlistUpdater->addProductToWishlist($wishlist, $wishlistProduct);
50
    }
51
}
52