Passed
Pull Request — master (#58)
by
unknown
03:30
created

RemoveProductFromWishlistHandler::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 2
b 0
f 0
nc 1
nop 3
dl 0
loc 9
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace BitBag\SyliusWishlistPlugin\CommandHandler\Wishlist;
6
7
use BitBag\SyliusWishlistPlugin\Command\Wishlist\RemoveProductFromWishlist;
8
use BitBag\SyliusWishlistPlugin\Exception\ProductNotFoundException;
9
use BitBag\SyliusWishlistPlugin\Repository\WishlistRepositoryInterface;
10
use BitBag\SyliusWishlistPlugin\Updater\WishlistUpdaterInterface;
11
use Sylius\Component\Core\Repository\ProductRepositoryInterface;
12
use Symfony\Component\Messenger\Handler\MessageHandlerInterface;
13
14
final class RemoveProductFromWishlistHandler implements MessageHandlerInterface
15
{
16
    private ProductRepositoryInterface $productRepository;
17
18
    private WishlistRepositoryInterface $wishlistRepository;
19
20
    private WishlistUpdaterInterface $wishlistUpdater;
21
22
    public function __construct(
23
        ProductRepositoryInterface $productRepository,
24
        WishlistRepositoryInterface $wishlistRepository,
25
        WishlistUpdaterInterface $wishlistUpdater
26
    )
27
    {
28
        $this->productRepository = $productRepository;
29
        $this->wishlistRepository = $wishlistRepository;
30
        $this->wishlistUpdater = $wishlistUpdater;
31
    }
32
33
    public function __invoke(RemoveProductFromWishlist $removeProductFromWishlist)
34
    {
35
        $product = $this->productRepository->find($removeProductFromWishlist->getProductIdValue());
36
        $wishlist = $this->wishlistRepository->findByToken($removeProductFromWishlist->getWishlistTokenValue());
37
38
        if (null === $product) {
39
            throw new ProductNotFoundException(
40
                sprintf("The Product %s does not exist", $removeProductFromWishlist->getProductIdValue())
41
            );
42
        }
43
44
        $this->wishlistUpdater->removeProductFromWishlist($wishlist, $product);
0 ignored issues
show
Bug introduced by
It seems like $wishlist can also be of type null; however, parameter $wishlist of BitBag\SyliusWishlistPlu...veProductFromWishlist() does only seem to accept BitBag\SyliusWishlistPlu...ntity\WishlistInterface, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

44
        $this->wishlistUpdater->removeProductFromWishlist(/** @scrutinizer ignore-type */ $wishlist, $product);
Loading history...
45
    }
46
}
47