Passed
Pull Request — master (#81)
by
unknown
04:00
created

AddProductToSelectedWishlistHandler::__invoke()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 2
eloc 7
nc 2
nop 1
dl 0
loc 14
rs 10
c 1
b 0
f 1
1
<?php
2
3
/*
4
 * This file was created by developers working at BitBag
5
 * Do you need more information about us and what we do? Visit our https://bitbag.io website!
6
 * We are hiring developers from all over the world. Join us and start your new, exciting adventure and become part of us: https://bitbag.io/career
7
*/
8
9
declare(strict_types=1);
10
11
namespace BitBag\SyliusWishlistPlugin\CommandHandler\Wishlist;
12
13
use BitBag\SyliusWishlistPlugin\Repository\WishlistRepositoryInterface;
14
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
15
use BitBag\SyliusWishlistPlugin\Command\Wishlist\AddProductToSelectedWishlist;
16
use BitBag\SyliusWishlistPlugin\Entity\WishlistProductInterface;
17
use BitBag\SyliusWishlistPlugin\Factory\WishlistProductFactoryInterface;
18
use Symfony\Component\Messenger\Handler\MessageHandlerInterface;
19
20
21
final class AddProductToSelectedWishlistHandler implements MessageHandlerInterface
22
{
23
    private WishlistProductFactoryInterface $wishlistProductFactory;
24
25
    private WishlistRepositoryInterface $wishlistRepository;
26
27
    public function __construct(
28
        WishlistProductFactoryInterface $wishlistProductFactory,
29
        WishlistRepositoryInterface $wishlistRepository
30
    ) {
31
        $this->wishlistProductFactory = $wishlistProductFactory;
32
        $this->wishlistRepository = $wishlistRepository;
33
    }
34
35
    public function __invoke(AddProductToSelectedWishlist $addProductToSelectedWishlist)
36
    {
37
        $product = $addProductToSelectedWishlist->getProduct();
38
        $wishlist = $addProductToSelectedWishlist->getWishlist();
39
40
        if (null === $product) {
41
            throw new NotFoundHttpException();
42
        }
43
44
        /** @var WishlistProductInterface $wishlistProduct */
45
        $wishlistProduct = $this->wishlistProductFactory->createForWishlistAndProduct($wishlist, $product);
46
47
        $wishlist->addWishlistProduct($wishlistProduct);
48
        $this->wishlistRepository->add($wishlist);
49
    }
50
}