Passed
Pull Request — master (#58)
by
unknown
06:12
created

RemoveWishlistHandler::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 2
b 0
f 0
nc 1
nop 2
dl 0
loc 7
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
    {
24
        $this->wishlistRepository = $wishlistRepository;
25
        $this->wishlistManager = $wishlistManager;
26
    }
27
28
    public function __invoke(RemoveWishlist $removeWishlist)
29
    {
30
        $token = $removeWishlist->getWishlistTokenValue();
31
        $wishlist = $this->wishlistRepository->findByToken($token);
32
33
        if (null === $wishlist) {
34
            throw new WishlistNotFoundException(
35
                sprintf("The Wishlist %s does not exist", $token)
36
            );
37
        }
38
39
        $this->wishlistManager->remove($wishlist);
40
        $this->wishlistManager->flush();
41
    }
42
}
43