RemoveWishlistHandler::__invoke()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 2
eloc 7
c 2
b 0
f 0
nc 2
nop 1
dl 0
loc 13
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\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