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

Controller/Admin/ShipmentController.php (1 issue)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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\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);
0 ignored issues
show
It seems like $this->getFormBuilder() can be null; however, addFormFields() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
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);
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