Completed
Push — SF4 ( 211d37...f4ba4a )
by Laurent
02:09
created

DefaultController::getFamilyLogAction()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 20
rs 9.6
c 0
b 0
f 0
cc 3
nc 3
nop 1
1
<?php
2
3
/**
4
 * DefaultController controller de l'application.
5
 *
6
 * PHP Version 7
7
 *
8
 * @author    Quétier Laurent <[email protected]>
9
 * @copyright 2018 Dev-Int GLSR
10
 * @license   http://opensource.org/licenses/gpl-license.php GNU Public License
11
 *
12
 * @version GIT: $Id$
13
 *
14
 * @link      https://github.com/Dev-Int/glsr
15
 */
16
17
namespace App\Controller;
18
19
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
20
use Symfony\Component\Routing\Annotation\Route;
21
use Symfony\Component\HttpFoundation\Request;
22
use Symfony\Component\HttpFoundation\Response;
23
24
/**
25
 * Description of DefaultController
26
 *
27
 * @category Controller
28
 */
29
class DefaultController extends Controller
30
{
31
    /**
32
     * @Route("/", name="home")
33
     */
34
    public function indexAction()
35
    {
36
        return $this->render('default/index.html.twig');
37
    }
38
39
    /**
40
     * Get FamilyLog.
41
     *
42
     * @Route("/getfamilylog", name="getfamilylog", methods="POST")
43
     *
44
     * @param \Symfony\Component\HttpFoundation\Request $request Post request
45
     * @return \Symfony\Component\HttpFoundation\Response
46
     */
47
    public function getFamilyLogAction(Request $request)
48
    {
49
        $return = new Response('Error');
50
51
        if ($request->isXmlHttpRequest()) {
52
            $familyLog = array();
53
            $id = $request->get('id');
54
            if ($id != '') {
55
                // Add directs childrens of $familyLog
56
                $familyLog = $this->testLevel($familyLog, $id);
0 ignored issues
show
Bug introduced by
The method testLevel() does not seem to exist on object<App\Controller\DefaultController>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
57
58
                $response = new Response();
59
                $data = json_encode($familyLog);
60
                $response->headers->set('Content-Type', 'application/json');
61
                $response->setContent($data);
62
                $return = $response;
63
            }
64
        }
65
        return $return;
66
    }
67
68
    /**
69
     * Get the SubFamily of Supplier.
70
     *
71
     * @param array $familyLog
72
     * @param int   $id
73
     */
74
    protected function getSubFamily(array $familyLog, $id)
75
    {
76
        // Get all datas
77
        $etm = $this->getDoctrine()->getManager();
78
        $supplier = $etm->getRepository('App:Settings\Supplier')->find($id);
79
        $familyLogs = $etm->getRepository('App:Settings\Diverse\FamilyLog')->findAll();
80
81
        // First return data needed
82
        $familyLog['id'] = $supplier->getFamilyLog()->getId();
83
        $testFamily = '';
84
        foreach ($familyLogs as $family) {
85
            if ($family->getId() === $familyLog['id']) {
86
                $testFamily = $family;
87
            }
88
        }
89
        if ($testFamily->getLevel() === 2) {
90
            $familyLog['family']['id'] = $testFamily->getId();
91
            $familyLog['family']['name'] = $testFamily->getIndentedName();
92
            $key = 0;
93
            foreach ($familyLogs as $family) {
94
                if ($family->getParent() !== null && $family->getParent()->getId() === $familyLog['id']) {
95
                    $familyLog['family'][$key]['id'] = $family->getId();
96
                    $familyLog['family'][$key]['name'] = $family->getIndentedName();
97
                    $key++;
98
                }
99
            }
100
        }
101
        return $familyLog;
102
    }
103
}
104