Passed
Pull Request — master (#58)
by
unknown
13:18 queued 09:31
created

AddProductVariantToWishlistHandler::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
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 11
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace BitBag\SyliusWishlistPlugin\CommandHandler\Wishlist;
6
7
use BitBag\SyliusWishlistPlugin\Command\Wishlist\AddProductVariantToWishlist;
8
use BitBag\SyliusWishlistPlugin\Entity\WishlistInterface;
9
use BitBag\SyliusWishlistPlugin\Exception\ProductVariantNotFoundException;
10
use BitBag\SyliusWishlistPlugin\Factory\WishlistProductFactoryInterface;
11
use BitBag\SyliusWishlistPlugin\Repository\WishlistRepositoryInterface;
12
use BitBag\SyliusWishlistPlugin\Updater\WishlistUpdaterInterface;
13
use Sylius\Component\Core\Repository\ProductVariantRepositoryInterface;
14
use Symfony\Component\Messenger\Handler\MessageHandlerInterface;
15
16
final class AddProductVariantToWishlistHandler implements MessageHandlerInterface
17
{
18
    private WishlistProductFactoryInterface $wishlistProductFactory;
19
20
    private WishlistUpdaterInterface $wishlistUpdater;
21
22
    private ProductVariantRepositoryInterface $productVariantRepository;
23
24
    private WishlistRepositoryInterface $wishlistRepository;
25
26
    public function __construct(
27
        WishlistProductFactoryInterface $wishlistProductFactory,
28
        WishlistRepositoryInterface $wishlistRepository,
29
        WishlistUpdaterInterface $wishlistUpdater,
30
        ProductVariantRepositoryInterface $productVariantRepository
31
    )
32
    {
33
        $this->wishlistProductFactory = $wishlistProductFactory;
34
        $this->wishlistUpdater = $wishlistUpdater;
35
        $this->productVariantRepository = $productVariantRepository;
36
        $this->wishlistRepository = $wishlistRepository;
37
    }
38
39
    public function __invoke(AddProductVariantToWishlist $addProductVariantToWishlist): WishlistInterface
40
    {
41
        $variant = $this->productVariantRepository->find($addProductVariantToWishlist->productVariantId);
42
        $wishlist = $this->wishlistRepository->findByToken($addProductVariantToWishlist->getWishlistTokenValue());
43
44
        if (null === $variant) {
45
            throw new ProductVariantNotFoundException(
46
                sprintf("The ProductVariant %s does not exist", $addProductVariantToWishlist->productVariantId)
47
            );
48
        }
49
50
        $wishlistProduct = $this->wishlistProductFactory->createForWishlistAndVariant($wishlist, $variant);
0 ignored issues
show
Bug introduced by
It seems like $wishlist can also be of type null; however, parameter $wishlist of BitBag\SyliusWishlistPlu...ForWishlistAndVariant() 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

50
        $wishlistProduct = $this->wishlistProductFactory->createForWishlistAndVariant(/** @scrutinizer ignore-type */ $wishlist, $variant);
Loading history...
51
52
        return $this->wishlistUpdater->addProductToWishlist($wishlist, $wishlistProduct);
0 ignored issues
show
Bug introduced by
It seems like $wishlist can also be of type null; however, parameter $wishlist of BitBag\SyliusWishlistPlu...:addProductToWishlist() 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

52
        return $this->wishlistUpdater->addProductToWishlist(/** @scrutinizer ignore-type */ $wishlist, $wishlistProduct);
Loading history...
53
    }
54
}
55