Completed
Push — master ( 63d470...84e35a )
by Adam
18:27
created

InvoiceController::addAction()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 30
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 30
rs 8.5806
c 0
b 0
f 0
cc 4
eloc 19
nc 4
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\InvoiceBundle\Controller\Admin;
14
15
use Symfony\Component\HttpFoundation\Request;
16
use Symfony\Component\HttpFoundation\Response;
17
use WellCommerce\Bundle\CoreBundle\Controller\Admin\AbstractAdminController;
18
use WellCommerce\Bundle\InvoiceBundle\Entity\Invoice;
19
use WellCommerce\Bundle\InvoiceBundle\Manager\InvoiceManager;
20
use WellCommerce\Bundle\InvoiceBundle\Processor\InvoiceProcessorCollection;
21
use WellCommerce\Bundle\InvoiceBundle\Processor\InvoiceProcessorInterface;
22
use WellCommerce\Bundle\OrderBundle\Entity\Order;
23
24
/**
25
 * Class InvoiceController
26
 *
27
 * @author  Adam Piotrowski <[email protected]>
28
 */
29
class InvoiceController extends AbstractAdminController
30
{
31
    /**
32
     * @var InvoiceManager
33
     */
34
    protected $manager;
35
    
36
    public function addAction(Request $request): Response
37
    {
38
        $orderId = $request->get('order');
39
        $order   = $this->get('order.repository')->find($orderId);
40
        
41
        if (!$order instanceof Order) {
42
            return $this->redirectToAction('index');
43
        }
44
        
45
        $this->getOrderProvider()->setCurrentOrder($order);
46
        $invoice    = $this->manager->prepareInvoiceForOrder($order);
47
        $form       = $this->formBuilder->createForm($invoice);
48
        $redirectTo = null;
49
        
50
        if ($form->handleRequest()->isSubmitted()) {
51
            if ($form->isValid()) {
52
                $this->getInvoiceProcessor($invoice)->save($invoice);
53
                $this->getManager()->createResource($invoice);
54
                $redirectTo = $this->getRouterHelper()->generateUrl('admin.order.edit', ['id' => $orderId]);
55
                $this->getFlashHelper()->addSuccess('invoice.flash.success');
56
            }
57
            
58
            return $this->createFormDefaultJsonResponse($form, $redirectTo);
59
        }
60
        
61
        return $this->displayTemplate('add', [
62
            'form'    => $form,
63
            'invoice' => $invoice,
64
        ]);
65
    }
66
    
67
    public function editAction(int $id): Response
68
    {
69
        $invoice = $this->getManager()->getRepository()->find($id);
70
        
71
        if ($invoice instanceof Invoice) {
72
            return $this->redirectToAction('print', ['guid' => $invoice->getGuid()]);
73
        }
74
        
75
        return $this->redirectToAction('index');
76
    }
77
    
78
    public function printAction(string $guid): Response
79
    {
80
        $invoice = $this->getManager()->getRepository()->findOneBy(['guid' => $guid]);
81
        if ($invoice instanceof Invoice) {
82
            return $this->getInvoiceProcessor($invoice)->download($invoice);
83
        }
84
        
85
        return $this->redirectToAction('index');
86
    }
87
    
88
    private function getInvoiceProcessor(Invoice $invoice): InvoiceProcessorInterface
89
    {
90
        /** @var InvoiceProcessorCollection $collection */
91
        $collection = $this->get('invoice.processor.collection');
92
        
93
        return $collection->get($invoice->getProcessor());
94
    }
95
}
96