MergeUserWishlistItemsListener::resolveWishlist()   A
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 25
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 5
eloc 11
nc 5
nop 2
dl 0
loc 25
rs 9.6111
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\EventListener;
12
13
use BitBag\SyliusWishlistPlugin\Entity\WishlistInterface;
14
use BitBag\SyliusWishlistPlugin\Factory\WishlistFactoryInterface;
15
use BitBag\SyliusWishlistPlugin\Repository\WishlistRepositoryInterface;
16
use Doctrine\Persistence\ObjectManager;
17
use Sylius\Component\Core\Model\ShopUserInterface;
18
use Symfony\Component\HttpFoundation\Request;
19
use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
20
21
final class MergeUserWishlistItemsListener
22
{
23
    /** @var WishlistRepositoryInterface */
24
    private $wishlistRepository;
25
26
    /** @var WishlistFactoryInterface */
27
    private $wishlistFactory;
28
29
    /** @var ObjectManager */
30
    private $wishlistManager;
31
32
    /** @var string */
33
    private $wishlistCookieToken;
34
35
    public function __construct(
36
        WishlistRepositoryInterface $wishlistRepository,
37
        WishlistFactoryInterface $wishlistFactory,
38
        ObjectManager $wishlistManager,
39
        string $wishlistCookieToken
40
    ) {
41
        $this->wishlistRepository = $wishlistRepository;
42
        $this->wishlistFactory = $wishlistFactory;
43
        $this->wishlistManager = $wishlistManager;
44
        $this->wishlistCookieToken = $wishlistCookieToken;
45
    }
46
47
    public function onInteractiveLogin(InteractiveLoginEvent $interactiveLoginEvent): void
48
    {
49
        $user = $interactiveLoginEvent->getAuthenticationToken()->getUser();
50
51
        if (!$user instanceof ShopUserInterface) {
52
            return;
53
        }
54
55
        $this->resolveWishlist($interactiveLoginEvent->getRequest(), $user);
56
    }
57
58
    private function resolveWishlist(Request $request, ShopUserInterface $shopUser): void
59
    {
60
        /** @var string $cookieWishlistToken */
61
        $cookieWishlistToken = $request->cookies->get($this->wishlistCookieToken, '');
62
63
        /** @var WishlistInterface|null $cookieWishlist */
64
        $cookieWishlist = $this->wishlistRepository->findByToken($cookieWishlistToken);
65
66
        if (null === $cookieWishlist) {
67
            return;
68
        }
69
70
        $userWishlist = $this->wishlistRepository->findOneByShopUser($shopUser);
71
72
        if (null !== $userWishlist) {
73
            foreach ($cookieWishlist->getWishlistProducts() as $wishlistProduct) {
74
                $userWishlist->addWishlistProduct($wishlistProduct);
75
            }
76
        }
77
78
        if (null === $userWishlist) {
79
            $cookieWishlist->setShopUser($shopUser);
80
        }
81
82
        $this->wishlistManager->flush();
83
    }
84
}
85