Completed
Push — master ( 0fb6c2...a0d538 )
by Adam
19:02
created

ShipmentController::addAction()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 33
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 33
rs 8.8571
c 0
b 0
f 0
cc 3
eloc 20
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\ShipmentBundle\Controller\Admin;
14
15
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
16
use Symfony\Component\HttpFoundation\Request;
17
use Symfony\Component\HttpFoundation\Response;
18
use WellCommerce\Bundle\CoreBundle\Controller\Admin\AbstractAdminController;
19
use WellCommerce\Bundle\OrderBundle\Adapter\ShipmentAdapterInterface;
20
use WellCommerce\Bundle\OrderBundle\Entity\ShipmentInterface;
21
22
/**
23
 * Class ShipmentController
24
 *
25
 * @author  Adam Piotrowski <[email protected]>
26
 */
27
class ShipmentController extends AbstractAdminController
28
{
29
    public function addAction(Request $request): Response
30
    {
31
        $courier = $request->get('courier');
32
        $orderId = $request->get('orderId');
33
        $order   = $this->get('order.manager')->getRepository()->find($orderId);
34
        $this->getOrderProvider()->setCurrentOrder($order);
35
        
36
        /** @var ShipmentInterface $shipment */
37
        $shipment = $this->getManager()->initResource();
38
        $shipment->setOrder($order);
39
        $shipment->setCourier($courier);
40
        
41
        $form = $this->formBuilder->createForm($shipment, [
42
            'ajax_enabled' => false,
43
        ]);
44
        
45
        $adapter = $this->getAdapter($courier);
46
        $adapter->addFormFields($form->getChildren()->get('required_data'), $this->getFormBuilder(), $shipment);
47
        
48
        if ($form->handleRequest()->isSubmitted()) {
49
            if ($form->isValid()) {
50
                $response = $adapter->addShipment($shipment, $form->getValue());
51
                $this->getManager()->createResource($shipment);
52
                
53
                return $response;
54
            }
55
        }
56
        
57
        return $this->displayTemplate('add', [
58
            'form'  => $form,
59
            'order' => $order,
60
        ]);
61
    }
62
    
63
    public function downloadLabelsAction(Request $request)
64
    {
65
        $courier = $request->get('courier');
66
        $date    = $request->get('date');
67
        $adapter = $this->getAdapter($courier);
68
        $labels  = $adapter->getLabels($date);
0 ignored issues
show
Unused Code introduced by
$labels is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
69
    }
70
    
71
    private function getAdapter(string $courier): ShipmentAdapterInterface
72
    {
73
        if (false === $this->has($courier . '.shipment.adapter')) {
74
            throw new ServiceNotFoundException($courier . '.shipment.adapter');
75
        }
76
        
77
        return $this->get($courier . '.shipment.adapter');
78
    }
79
}
80