Completed
Push — master ( 8f2642...cafe6d )
by Adam
06:17
created

onOrderStatusHistoryPreCreate()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 21
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 21
rs 9.3142
c 0
b 0
f 0
cc 3
eloc 14
nc 3
nop 1
1
<?php
2
/*
3
 * WellCommerce Open-Source E-Commerce Platform
4
 *
5
 * This file is part of the WellCommerce package.
6
 *
7
 * (c) Adam Piotrowski <[email protected]>
8
 *
9
 * For the full copyright and license information,
10
 * please view the LICENSE file that was distributed with this source code.
11
 */
12
13
namespace WellCommerce\Bundle\OrderBundle\EventListener;
14
15
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
16
use WellCommerce\Bundle\CoreBundle\Doctrine\Event\EntityEvent;
17
use WellCommerce\Bundle\CoreBundle\Helper\Mailer\MailerHelper;
18
use WellCommerce\Bundle\CoreBundle\Helper\Translator\TranslatorHelperInterface;
19
use WellCommerce\Bundle\OrderBundle\Entity\OrderStatusHistory;
20
21
/**
22
 * Class OrderStatusHistorySubscriber
23
 *
24
 * @author  Adam Piotrowski <[email protected]>
25
 */
26
final class OrderStatusHistorySubscriber implements EventSubscriberInterface
27
{
28
    /**
29
     * @var MailerHelper
30
     */
31
    private $mailerHelper;
32
    
33
    /**
34
     * @var TranslatorHelperInterface
35
     */
36
    private $translatorHelper;
37
    
38
    public function __construct(MailerHelper $mailerHelper, TranslatorHelperInterface $translatorHelper)
39
    {
40
        $this->mailerHelper     = $mailerHelper;
41
        $this->translatorHelper = $translatorHelper;
42
    }
43
    
44
    public static function getSubscribedEvents()
45
    {
46
        return [
47
            'order_status_history.pre_create' => ['onOrderStatusHistoryPreCreate', 0],
48
        ];
49
    }
50
    
51
    public function onOrderStatusHistoryPreCreate(EntityEvent $event)
52
    {
53
        $history = $event->getEntity();
54
        if ($history instanceof OrderStatusHistory) {
55
            $order = $history->getOrder();
56
            if ($history->isNotify()) {
57
                $this->mailerHelper->sendEmail([
58
                    'recipient'     => $order->getContactDetails()->getEmail(),
59
                    'subject'       => $this->translatorHelper->trans('order_status_history.email.status_changed'),
60
                    'template'      => 'WellCommerceOrderBundle:Email:order_status_change.html.twig',
61
                    'parameters'    => [
62
                        'history' => $history,
63
                        'order'   => $order,
64
                    ],
65
                    'configuration' => $order->getShop()->getMailerConfiguration(),
66
                ]);
67
            }
68
            
69
            $order->setCurrentStatus($history->getOrderStatus());
70
        }
71
    }
72
}
73