RemoveWishlistHandler   A
last analyzed

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
/*
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\CommandHandler\Wishlist;
12
13
use BitBag\SyliusWishlistPlugin\Command\Wishlist\RemoveWishlist;
14
use BitBag\SyliusWishlistPlugin\Exception\WishlistNotFoundException;
15
use BitBag\SyliusWishlistPlugin\Repository\WishlistRepositoryInterface;
16
use Doctrine\Persistence\ObjectManager;
17
use Symfony\Component\Messenger\Handler\MessageHandlerInterface;
18
19
final class RemoveWishlistHandler implements MessageHandlerInterface
20
{
21
    private WishlistRepositoryInterface $wishlistRepository;
22
23
    private ObjectManager $wishlistManager;
24
25
    public function __construct(
26
        WishlistRepositoryInterface $wishlistRepository,
27
        ObjectManager $wishlistManager
28
    ) {
29
        $this->wishlistRepository = $wishlistRepository;
30
        $this->wishlistManager = $wishlistManager;
31
    }
32
33
    public function __invoke(RemoveWishlist $removeWishlist): void
34
    {
35
        $token = $removeWishlist->getWishlistTokenValue();
36
        $wishlist = $this->wishlistRepository->findByToken($token);
37
38
        if (null === $wishlist) {
39
            throw new WishlistNotFoundException(
40
                sprintf('The Wishlist %s does not exist', $token)
41
            );
42
        }
43
44
        $this->wishlistManager->remove($wishlist);
45
        $this->wishlistManager->flush();
46
    }
47
}
48