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

AddProductVariantToWishlistHandler::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
c 2
b 0
f 0
nc 1
nop 4
dl 0
loc 10
rs 10
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