Passed
Pull Request — master (#81)
by
unknown
03:27
created

ListWishlistsAction::__invoke()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 3
eloc 9
c 3
b 0
f 0
nc 4
nop 1
dl 0
loc 14
rs 9.9666
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\Request;
16
use Symfony\Component\HttpFoundation\Response;
17
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage;
18
use Twig\Environment;
19
20
final class ListWishlistsAction
21
{
22
    private WishlistRepositoryInterface $wishlistRepository;
23
24
    private Environment $twigEnvironment;
25
26
    private TokenStorage $tokenStorage;
27
28
    public function __construct(
29
        WishlistRepositoryInterface $wishlistRepository,
30
        Environment $twigEnvironment,
31
        TokenStorage $tokenStorage
32
    ) {
33
        $this->wishlistRepository = $wishlistRepository;
34
        $this->twigEnvironment = $twigEnvironment;
35
        $this->tokenStorage = $tokenStorage;
36
    }
37
38
    public function __invoke(Request $request): Response
39
    {
40
        $user = $this->tokenStorage->getToken() ? $this->tokenStorage->getToken()->getUser() : null;
41
        $cookie = $request->cookies->get('PHPSESSID');
42
43
        if ($user instanceof ShopUserInterface) {
44
            $wishlists = $this->wishlistRepository->findAllByShopUser($user->getId());
45
        } else {
46
            $wishlists = $this->wishlistRepository->findAllByAnonymous($cookie);
47
        }
48
49
        return new Response(
50
            $this->twigEnvironment->render('@BitBagSyliusWishlistPlugin/WishlistGroup/index.html.twig', [
51
                'wishlist' => $wishlists,
52
53
            ])
54
        );
55
    }
56
}
57