Completed
Push — Recipes ( 630f49...c8afb0 )
by Laurent
12:15 queued 03:48
created

DeliveriesController::editAction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
/**
3
 * DeliveriesController controller of deliveries.
4
 *
5
 * PHP Version 7
6
 *
7
 * @author    Quétier Laurent <[email protected]>
8
 * @copyright 2014 Dev-Int GLSR
9
 * @license   http://opensource.org/licenses/gpl-license.php GNU Public License
10
 *
11
 * @version GIT: <git_id>
12
 *
13
 * @link      https://github.com/Dev-Int/glsr
14
 */
15
namespace App\Controller\Orders;
16
17
use Symfony\Component\HttpFoundation\Request;
18
use App\Controller\Orders\AbstractOrdersController;
19
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
20
use Symfony\Component\Routing\Annotation\Route;
21
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
22
23
use App\Entity\Orders\Orders;
24
use App\Form\Type\Orders\OrdersEditType;
25
26
/**
27
 * Deliveries controller.
28
 *
29
 * @Route("/deliveries")
30
 */
31
class DeliveriesController extends AbstractOrdersController
32
{
33
    /**
34
     * Lists all Orders entities.
35
     *
36
     * @Route("/", name="deliveries")
37
     * @Method("GET")
38
     * @Template()
39
     */
40
    public function indexAction(Request $request)
41
    {
42
        $etm = $this->getDoctrine()->getManager();
43
        $item = $this->container->getParameter('knp_paginator.page_range');
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Psr\Container\ContainerInterface as the method getParameter() does only exist in the following implementations of said interface: Container14\ProjectServiceContainer, ProjectServiceContainer, Symfony\Component\Depend...urationContainerBuilder, Symfony\Component\DependencyInjection\Container, Symfony\Component\Depend...ection\ContainerBuilder, Symfony\Component\Depend...\NoConstructorContainer, Symfony\Component\Depend...tainers\CustomContainer, Symfony\Component\Depend...ProjectServiceContainer, Symfony\Component\Depend...ProjectServiceContainer, Symfony_DI_PhpDumper_Test_Almost_Circular_Private, Symfony_DI_PhpDumper_Test_Almost_Circular_Public, Symfony_DI_PhpDumper_Test_Base64Parameters, Symfony_DI_PhpDumper_Test_Deep_Graph, Symfony_DI_PhpDumper_Test_EnvParameters, Symfony_DI_PhpDumper_Test_Inline_Self_Ref, Symfony_DI_PhpDumper_Test_Legacy_Privates, Symfony_DI_PhpDumper_Test_Rot13Parameters, Symfony_DI_PhpDumper_Test_Uninitialized_Reference.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
44
        $qbd = $etm->getRepository('App:Orders\Orders')->createQueryBuilder('o');
45
        $qbd->where('o.delivdate <= ' . date('Y-m-d'));
46
        $qbd->andWhere('o.status = 1');
47
        
48
        $paginator = $this->get('knp_paginator')->paginate($qbd, $request->query->get('page', 1), $item);
49
        return array(
50
            'paginator' => $paginator,
51
        );
52
    }
53
54
    /**
55
     * Finds and displays a Orders entity.
56
     *
57
     * @Route("/{id}/show", name="deliveries_show", requirements={"id"="\d+"})
58
     * @Method("GET")
59
     * @Template()
60
     */
61
    public function showAction(Orders $orders)
62
    {
63
        return array(
64
            'orders' => $orders,
65
        );
66
    }
67
68
    /**
69
     * Displays a form to edit an existing Orders entity.
70
     *
71
     * @Route("/admin/{id}/edit", name="deliveries_edit", requirements={"id"="\d+"})
72
     * @Method("GET")
73
     * @Template()
74
     */
75
    public function editAction(Orders $orders)
76
    {
77
        $return = $this->abstractEditAction($orders, 'deliveries', OrdersEditType::class);
78
79
        return $return;
80
    }
81
82
    /**
83
     * Edits an existing Orders entity.
84
     *
85
     * @Route("/admin/{id}/update", name="deliveries_update", requirements={"id"="\d+"})
86
     * @Method("PUT")
87
     * @Template("orders/deliveries/edit.html.twig")
88
     */
89
    public function updateAction(Orders $orders, Request $request)
90
    {
91
        $return = $this->abstractUpdateAction(
92
            $orders,
93
            $request,
94
            'deliveries',
95
            OrdersEditType::class
96
        );
97
98
        return $return;
99
    }
100
101
    /**
102
     * Print the current delivery.<br />Creating a `PDF` file for viewing on paper
103
     *
104
     * @Route("/{id}/print/", name="deliveries_print", requirements={"id"="\d+"})
105
     * @Method("GET")
106
     * @Template()
107
     *
108
     * @param \App\Entity\Orders\Orders $orders Order item to print
109
     * @return \Symfony\Component\HttpFoundation\Response
110
     */
111
    public function printAction(Orders $orders)
112
    {
113
        $return = $this->abstractPrintAction($orders, 'Deliveries');
114
        
115
        return $return;
116
    }
117
}
118