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

RemoveWishlistHandler::__invoke()   A

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
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