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

InvoicesController   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5

Importance

Changes 0
Metric Value
wmc 5
lcom 0
cbo 5
dl 0
loc 85
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A indexAction() 0 11 1
A showAction() 0 6 1
A editAction() 0 6 1
A updateAction() 0 11 1
A printAction() 0 6 1
1
<?php
2
/**
3
 * InvoicesController Controller of billing.
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\InvoicesEditType;
25
26
/**
27
 * Invoices controller.
28
 *
29
 * @Route("/invoices")
30
 */
31
class InvoicesController extends AbstractOrdersController
32
{
33
    /**
34
     * Lists all Orders entities.
35
     *
36
     * @Route("/", name="invoices")
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')->findInvoices();
45
        
46
        $paginator = $this->get('knp_paginator')->paginate($qbd, $request->query->get('page', 1), $item);
47
        return array(
48
            'paginator' => $paginator,
49
        );
50
    }
51
52
    /**
53
     * Finds and displays a Orders entity.
54
     *
55
     * @Route("/{id}/show", name="invoices_show", requirements={"id"="\d+"})
56
     * @Method("GET")
57
     * @Template()
58
     */
59
    public function showAction(Orders $orders)
60
    {
61
        return array(
62
            'orders' => $orders,
63
        );
64
    }
65
66
    /**
67
     * Displays a form to edit an existing Orders entity.
68
     *
69
     * @Route("/admin/{id}/edit", name="invoices_edit", requirements={"id"="\d+"})
70
     * @Method("GET")
71
     * @Template()
72
     */
73
    public function editAction(Orders $orders)
74
    {
75
        $return = $this->abstractEditAction($orders, 'invoices', InvoicesEditType::class);
76
77
        return $return;
78
    }
79
80
    /**
81
     * Edits an existing Orders entity.
82
     *
83
     * @Route("/admin/{id}/update", name="invoices_update", requirements={"id"="\d+"})
84
     * @Method("PUT")
85
     * @Template("orders/invoices/edit.html.twig")
86
     */
87
    public function updateAction(Orders $orders, Request $request)
88
    {
89
        $return = $this->abstractUpdateAction(
90
            $orders,
91
            $request,
92
            'invoices',
93
            InvoicesEditType::class
94
        );
95
96
        return $return;
97
    }
98
99
    /**
100
     * Print the current invoice.<br />Creating a `PDF` file for viewing on paper
101
     *
102
     * @Route("/{id}/print/", name="invoices_print", requirements={"id"="\d+"})
103
     * @Method("GET")
104
     * @Template()
105
     *
106
     * @param \App\Entity\Orders $orders Order item to print
107
     * @return \Symfony\Component\HttpFoundation\Response
108
     */
109
    public function printAction(Orders $orders)
110
    {
111
        $return = $this->abstractPrintAction($orders, 'Invoices');
112
        
113
        return $return;
114
    }
115
}
116