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

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