Completed
Push — master ( 1839ce...4b984d )
by Laurent
03:35
created

DefaultController::testEntities()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 21
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 5
Bugs 2 Features 1
Metric Value
c 5
b 2
f 1
dl 0
loc 21
rs 9.0534
cc 4
eloc 14
nc 6
nop 0
1
<?php
2
/**
3
 * DefaultController controller de l'application GLSR.
4
 *
5
 * PHP Version 5
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 since 1.0.0
12
 *
13
 * @link      https://github.com/Dev-Int/glsr
14
 */
15
namespace AppBundle\Controller;
16
17
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
18
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
19
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
20
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
21
use Symfony\Component\HttpFoundation\Response;
22
23
/**
24
 * Default controller.
25
 *
26
 * @category Controller
27
 *
28
 * @Route("/")
29
 */
30
class DefaultController extends Controller
31
{
32
    private $entities;
33
    
34
    public function __construct()
35
    {
36
        // Tableau des entitées
37
        $this->entities = array(
38
            'AppBundle:User',
39
            'AppBundle:Company',
40
            'AppBundle:Settings',
41
            'AppBundle:FamilyLog',
42
            'AppBundle:ZoneStorage',
43
            'AppBundle:UnitStorage',
44
            'AppBundle:Tva',
45
            'AppBundle:Supplier',
46
            'AppBundle:Article');
47
48
    }
49
50
    /**
51
     * @Route("/", name="_home")
52
     * @Method("GET")
53
     * @Template()
54
     */
55
    public function indexAction()
56
    {
57
        /**
58
         * Test d'installation
59
         */
60
        $url = $this->testEntities();
61
        if (empty($url)) {
62
            $url = $this->render('default/index.html.twig');
63
        } else {
64
            $url = $this->redirectToRoute($url);
65
        }
66
67
        return $url;
68
    }
69
70
    /**
71
     * Test des entités
72
     *
73
     * @return array
0 ignored issues
show
Documentation introduced by
Should the return type not be string|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
74
     */
75
    private function testEntities()
76
    {
77
        $url = null;
78
        $etm = $this->getDoctrine()->getManager();
79
        // vérifie que les Entitées ne sont pas vides
80
        $nbEntities = count($this->entities);
81
        for ($index = 0; $index < $nbEntities; $index++) {
82
            $entity = $etm->getRepository($this->entities[$index]);
83
            $entityData = $entity->find(1);
84
85
            if (empty($entityData)) {
86
                $message = 'gestock.install.none';
87
                $url = 'gs_install';
88
                break;
89
            }
90
        }
91
        if (isset($message)) {
92
            $this->addFlash('warning', $message);
93
        }
94
        return $url;
95
    }
96
97
    /**
98
     * Récupère les FamilyLog de la requête post.
99
     *
100
     * @Route("/getfamilylog", name="getfamilylog")
101
     * @Method("POST")
102
     * @return \Symfony\Component\HttpFoundation\Response
103
     */
104
    public function getFamilyLogAction()
105
    {
106
        $id = '';
0 ignored issues
show
Unused Code introduced by
$id 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...
107
        $request = $this->getRequest();
0 ignored issues
show
Deprecated Code introduced by
The method Symfony\Bundle\Framework...ontroller::getRequest() has been deprecated with message: since version 2.4, to be removed in 3.0. Ask Symfony to inject the Request object into your controller method instead by type hinting it in the method's signature.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
108
        $etm = $this->getDoctrine()->getManager();
109
        if ($request->isXmlHttpRequest()) {
110
            $id = $request->get('id');
111
            if ($id != '') {
112
                $supplier = $etm
113
                    ->getRepository('AppBundle:Supplier')
114
                    ->find($id);
115
                $familyLog['familylog'] = $supplier->getFamilyLog()->getId();
0 ignored issues
show
Coding Style Comprehensibility introduced by
$familyLog was never initialized. Although not strictly required by PHP, it is generally a good practice to add $familyLog = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
116
                $response = new Response();
117
                $data = json_encode($familyLog);
118
                $response->headers->set('Content-Type', 'application/json');
119
                $response->setContent($data);
120
                return $response;
121
            }
122
        }
123
        return new Response('Error');
124
    }
125
}
126