Passed
Pull Request — master (#58)
by
unknown
05:39
created

RemoveWishlistHandler   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 27
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 12
c 3
b 0
f 0
dl 0
loc 27
rs 10
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 13 2
A __construct() 0 6 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace BitBag\SyliusWishlistPlugin\CommandHandler\Wishlist;
6
7
use BitBag\SyliusWishlistPlugin\Command\Wishlist\RemoveWishlist;
8
use BitBag\SyliusWishlistPlugin\Exception\WishlistNotFoundException;
9
use BitBag\SyliusWishlistPlugin\Repository\WishlistRepositoryInterface;
10
use Doctrine\Persistence\ObjectManager;
11
use Symfony\Component\Messenger\Handler\MessageHandlerInterface;
12
13
final class RemoveWishlistHandler implements MessageHandlerInterface
14
{
15
    private WishlistRepositoryInterface $wishlistRepository;
16
17
    private ObjectManager $wishlistManager;
18
19
    public function __construct(
20
        WishlistRepositoryInterface $wishlistRepository,
21
        ObjectManager $wishlistManager
22
    ) {
23
        $this->wishlistRepository = $wishlistRepository;
24
        $this->wishlistManager = $wishlistManager;
25
    }
26
27
    public function __invoke(RemoveWishlist $removeWishlist)
28
    {
29
        $token = $removeWishlist->getWishlistTokenValue();
30
        $wishlist = $this->wishlistRepository->findByToken($token);
31
32
        if (null === $wishlist) {
33
            throw new WishlistNotFoundException(
34
                sprintf('The Wishlist %s does not exist', $token)
35
            );
36
        }
37
38
        $this->wishlistManager->remove($wishlist);
39
        $this->wishlistManager->flush();
40
    }
41
}
42