Completed
Push — currency-cookie ( 8ef560...28b724 )
by Kamil
24:47
created

ExpiredCartsRemover   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 3
c 1
b 0
f 0
lcom 1
cbo 4
dl 0
loc 55
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 1
A remove() 0 14 2
1
<?php
2
3
/*
4
 * This file is part of the Sylius package.
5
 *
6
 * (c) Paweł Jędrzejewski
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Sylius\Bundle\OrderBundle\Remover;
13
14
use Doctrine\Common\Persistence\ObjectManager;
15
use Sylius\Bundle\OrderBundle\SyliusExpiredCartsEvents;
16
use Sylius\Component\Order\Remover\ExpiredCartsRemoverInterface;
17
use Sylius\Component\Order\Repository\OrderRepositoryInterface;
18
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
19
use Symfony\Component\EventDispatcher\GenericEvent;
20
21
/**
22
 * @author Mateusz Zalewski <[email protected]>
23
 */
24
final class ExpiredCartsRemover implements ExpiredCartsRemoverInterface
25
{
26
    /**
27
     * @var OrderRepositoryInterface
28
     */
29
    private $orderRepository;
30
31
    /**
32
     * @var ObjectManager
33
     */
34
    private $orderManager;
35
36
    /**
37
     * @var EventDispatcherInterface
38
     */
39
    private $eventDispatcher;
40
41
    /**
42
     * @var string
43
     */
44
    private $expirationPeriod;
45
46
    /**
47
     * @param OrderRepositoryInterface $orderRepository
48
     * @param ObjectManager $orderManager
49
     * @param EventDispatcherInterface $eventDispatcher
50
     * @param string $expirationPeriod
51
     */
52
    public function __construct(
53
        OrderRepositoryInterface $orderRepository,
54
        ObjectManager $orderManager,
55
        EventDispatcherInterface $eventDispatcher,
56
        $expirationPeriod
57
    ) {
58
        $this->orderRepository = $orderRepository;
59
        $this->orderManager = $orderManager;
60
        $this->eventDispatcher = $eventDispatcher;
61
        $this->expirationPeriod = $expirationPeriod;
62
    }
63
64
    public function remove()
65
    {
66
        $expiredCarts = $this->orderRepository->findCartsNotModifiedSince(new \DateTime('-'.$this->expirationPeriod));
67
68
        $this->eventDispatcher->dispatch(SyliusExpiredCartsEvents::PRE_REMOVE, new GenericEvent($expiredCarts));
69
70
        foreach ($expiredCarts as $expiredCart) {
71
            $this->orderManager->remove($expiredCart);
72
        }
73
74
        $this->orderManager->flush();
75
76
        $this->eventDispatcher->dispatch(SyliusExpiredCartsEvents::POST_REMOVE, new GenericEvent($expiredCarts));
77
    }
78
}
79