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

ListWishlistsAction   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A __invoke() 0 15 3
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
19
use Twig\Environment;
20
21
final class ListWishlistsAction
22
{
23
    private WishlistRepositoryInterface $wishlistRepository;
24
25
    private Environment $twigEnvironment;
26
27
    private TokenStorage $tokenStorage;
28
29
    public function __construct(
30
        WishlistRepositoryInterface $wishlistRepository,
31
        Environment $twigEnvironment,
32
        TokenStorage $tokenStorage
33
    ) {
34
        $this->wishlistRepository = $wishlistRepository;
35
        $this->twigEnvironment = $twigEnvironment;
36
        $this->tokenStorage = $tokenStorage;
37
    }
38
39
    public function __invoke(Request $request): Response
40
    {
41
        $user = $this->tokenStorage->getToken() ? $this->tokenStorage->getToken()->getUser() : null;
42
        $token = $this->tokenStorage->getToken();
43
44
        if ($user instanceof ShopUserInterface) {
45
            $wishlists = $this->wishlistRepository->findAllByShopUser($user);
46
        } else {
47
            $wishlists = $this->wishlistRepository->findAllByAnonymous();
48
        }
49
50
        //dd($wishlists);
51
        return new Response(
52
            $this->twigEnvironment->render('@BitBagSyliusWishlistPlugin/WishlistGroup/index.html.twig', [
53
                'wishlist' => $wishlists,
54
55
            ])
56
        );
57
    }
58
59
}
60