Passed
Pull Request — master (#81)
by
unknown
15:22
created

WishlistExtension::getFunctions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
cc 1
eloc 4
c 2
b 1
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 BitBag\SyliusWishlistPlugin\Resolver\WishlistCookieTokenResolverInterface;
16
use Sylius\Component\Core\Model\ShopUserInterface;
17
use Sylius\Component\User\Model\UserInterface;
18
use Symfony\Component\Security\Core\Exception\UserNotFoundException;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Securi...n\UserNotFoundException was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
19
use Twig\Extension\AbstractExtension;
20
use Twig\TwigFunction;
21
22
class WishlistExtension extends AbstractExtension
23
{
24
    private WishlistRepositoryInterface $wishlistRepository;
25
26
    private WishlistCookieTokenResolverInterface $wishlistCookieTokenResolver;
27
28
    public function __construct(
29
        WishlistRepositoryInterface $wishlistRepository,
30
        WishlistCookieTokenResolverInterface $wishlistCookieTokenResolver
31
    ) {
32
        $this->wishlistRepository = $wishlistRepository;
33
        $this->wishlistCookieTokenResolver = $wishlistCookieTokenResolver;
34
    }
35
36
    public function getFunctions(): array
37
    {
38
        return [
39
            new TwigFunction('getWishlists', [$this, 'getWishlists']),
40
            new TwigFunction('findAllByShopUser', [$this, 'findAllByShopUser']),
41
            new TwigFunction('findAllByAnonymous', [$this, 'findAllByAnonymous']),
42
        ];
43
    }
44
45
    public function getWishlists(): ?array
46
    {
47
        /** @var WishlistInterface[] $wishlists */
48
        $wishlists = $this->wishlistRepository->findAll();
49
50
        return $wishlists;
51
    }
52
53
    public function findAllByShopUser(UserInterface $user = null): ?array
54
    {
55
        if (!$user instanceof ShopUserInterface) {
56
            throw new UserNotFoundException();
57
        }
58
59
        return $this->wishlistRepository->findAllByShopUser($user->getId());
60
    }
61
62
    public function findAllByAnonymous(): ?array
63
    {
64
        $wishlistCookieToken = $this->wishlistCookieTokenResolver->resolve();
65
66
        return $this->wishlistRepository->findAllByAnonymous($wishlistCookieToken);
67
    }
68
}
69