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

ListWishlistsAction   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 22
c 3
b 0
f 0
dl 0
loc 39
rs 10
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A __invoke() 0 21 4
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\Controller\Action;
12
13
use BitBag\SyliusWishlistPlugin\Repository\WishlistRepositoryInterface;
14
use Sylius\Component\Core\Model\ShopUserInterface;
15
use Symfony\Component\HttpFoundation\Session\Session;
16
use Symfony\Component\HttpFoundation\Request;
17
use Symfony\Component\HttpFoundation\Response;
18
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
19
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage;
20
use Twig\Environment;
21
22
final class ListWishlistsAction
23
{
24
    private WishlistRepositoryInterface $wishlistRepository;
25
26
    private Environment $twigEnvironment;
27
28
    private TokenStorage $tokenStorage;
29
30
    public function __construct(
31
        WishlistRepositoryInterface $wishlistRepository,
32
        Environment $twigEnvironment,
33
        TokenStorage $tokenStorage
34
    ) {
35
        $this->wishlistRepository = $wishlistRepository;
36
        $this->twigEnvironment = $twigEnvironment;
37
        $this->tokenStorage = $tokenStorage;
38
    }
39
40
    public function __invoke(Request $request, UrlGeneratorInterface $urlGenerator): Response
41
    {
42
        $user = $this->tokenStorage->getToken() ? $this->tokenStorage->getToken()->getUser() : null;
43
        $cookie = $request->cookies->get('PHPSESSID');
44
45
        if ($user instanceof ShopUserInterface) {
46
            $wishlists = $this->wishlistRepository->findAllByShopUser($user->getId());
47
        } else {
48
            if ($cookie == null) {
49
                $session = new Session();
50
                $session->start();
51
                $cookie = $session->getId();
52
                $wishlists = $this->wishlistRepository->findAllByAnonymous($cookie);
53
            } else {
54
                $wishlists = $this->wishlistRepository->findAllByAnonymous($cookie);
55
            }
56
        }
57
58
        return new Response(
59
            $this->twigEnvironment->render('@BitBagSyliusWishlistPlugin/WishlistGroup/index.html.twig', [
60
                'wishlist' => $wishlists,
61
            ])
62
        );
63
    }
64
}
65