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

WishlistExtension::getFunctions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 6
rs 10
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\Twig;
12
13
use BitBag\SyliusWishlistPlugin\Entity\WishlistInterface;
14
use BitBag\SyliusWishlistPlugin\Repository\WishlistRepositoryInterface;
15
use Symfony\Component\HttpFoundation\RequestStack;
16
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage;
17
use Sylius\Component\Core\Model\ShopUserInterface;
18
use Twig\Extension\AbstractExtension;
19
use Twig\TwigFunction;
20
21
class WishlistExtension extends AbstractExtension
22
{
23
    private WishlistRepositoryInterface $wishlistRepository;
24
25
    private TokenStorage $tokenStorage;
26
27
    private RequestStack $requestStack;
28
29
    public function __construct(
30
31
        WishlistRepositoryInterface $wishlistRepository,
32
        TokenStorage $tokenStorage,
33
        RequestStack $requestStack
34
    ) {
35
        $this->wishlistRepository = $wishlistRepository;
36
        $this->tokenStorage = $tokenStorage;
37
        $this->requestStack = $requestStack;
38
    }
39
40
    public function getFunctions()
41
    {
42
        return [
43
            new TwigFunction('getWishlists', [$this, 'getWishlists']),
44
            new TwigFunction('findAllByShopUser', [$this, 'findAllByShopUser']),
45
            new TwigFunction('findAllByAnonymous', [$this, 'findAllByAnonymous'])
46
        ];
47
    }
48
49
    public function getWishlists()
50
    {
51
        /** @var WishlistInterface $wishlists */
52
        $wishlists = $this->wishlistRepository->findAll();
53
54
        return $wishlists;
55
    }
56
57
    public function findAllByShopUser()
58
    {
59
        $user = $this->tokenStorage->getToken()->getUser();
60
        if ($user instanceof ShopUserInterface) {
61
            return $this->wishlistRepository->findAllByShopUser($user->getId());
62
        }
63
    }
64
65
    public function findAllByAnonymous()
66
    {
67
        $request = $this->requestStack->getCurrentRequest();
68
        $cookie = $request->cookies->get('PHPSESSID');
69
        return $this->wishlistRepository->findAllByAnonymous($cookie);
70
    }
71
}