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

CopySelectedProductsToOtherWishlistHandler   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 13
c 1
b 0
f 0
dl 0
loc 33
rs 10
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 9 1
A __construct() 0 4 1
A copyWishlistProductsToOtherWishlist() 0 10 2
1
<?php
2
3
/*
4
 * This file has been created by developers from BitBag.
5
 * Feel free to contact us once you face any issues or want to start
6
 * You can find more information about us on https://bitbag.io and write us
7
 * an email on [email protected].
8
 */
9
10
declare(strict_types=1);
11
12
namespace BitBag\SyliusWishlistPlugin\CommandHandler\Wishlist;
13
14
use BitBag\SyliusWishlistPlugin\Command\Wishlist\CopySelectedProductsToOtherWishlist;
15
use BitBag\SyliusWishlistPlugin\Command\Wishlist\WishlistItemInterface;
16
use BitBag\SyliusWishlistPlugin\Controller\Action\AddProductVariantToWishlistAction;
17
use Doctrine\Common\Collections\Collection;
18
use Symfony\Component\HttpFoundation\Request;
19
use Symfony\Component\HttpFoundation\RequestStack;
20
21
final class CopySelectedProductsToOtherWishlistHandler
22
{
23
    private AddProductVariantToWishlistAction $addProductVariantToWishlistAction;
24
25
    private RequestStack $requestStack;
26
27
    public function __construct(AddProductVariantToWishlistAction $addProductVariantToWishlistAction, RequestStack $requestStack)
28
    {
29
        $this->addProductVariantToWishlistAction = $addProductVariantToWishlistAction;
30
        $this->requestStack = $requestStack;
31
    }
32
33
    public function __invoke(CopySelectedProductsToOtherWishlist $copySelectedProductsToOtherWishlistCommand): void
34
    {
35
        $destinedWishlistId = $copySelectedProductsToOtherWishlistCommand->getDestinedWishlistId();
36
37
        $currentRequest = $this->requestStack->getCurrentRequest();
38
39
        $this->copyWishlistProductsToOtherWishlist($copySelectedProductsToOtherWishlistCommand->getWishlistProducts(), $currentRequest);
0 ignored issues
show
Bug introduced by
It seems like $currentRequest can also be of type null; however, parameter $request of BitBag\SyliusWishlistPlu...oductsToOtherWishlist() does only seem to accept Symfony\Component\HttpFoundation\Request, 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

39
        $this->copyWishlistProductsToOtherWishlist($copySelectedProductsToOtherWishlistCommand->getWishlistProducts(), /** @scrutinizer ignore-type */ $currentRequest);
Loading history...
40
41
        $this->addProductVariantToWishlistAction->__invoke($destinedWishlistId, $currentRequest);
0 ignored issues
show
Bug introduced by
It seems like $currentRequest can also be of type null; however, parameter $request of BitBag\SyliusWishlistPlu...hlistAction::__invoke() does only seem to accept Symfony\Component\HttpFoundation\Request, 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

41
        $this->addProductVariantToWishlistAction->__invoke($destinedWishlistId, /** @scrutinizer ignore-type */ $currentRequest);
Loading history...
42
    }
43
44
    private function copyWishlistProductsToOtherWishlist(Collection $wishlistProducts, Request $request): void
45
    {
46
        $variantIds = [];
47
48
        /** @var WishlistItemInterface $wishlistProduct */
49
        foreach ($wishlistProducts as $wishlistProduct) {
50
            $variantIds[] = $wishlistProduct->getCartItem()->getCartItem()->getVariant()->getId();
51
        }
52
53
        $request->attributes->set('variantId', $variantIds);
54
    }
55
}
56