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

AddProductToWishlistHandler::__invoke()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

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