Completed
Push — master ( 7e9231...419bd7 )
by Guillaume
15:48
created

StructureController::getFields()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 1
1
<?php
2
3
namespace Starkerxp\StructureBundle\Controller;
4
5
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
6
use Symfony\Component\OptionsResolver\OptionsResolver;
7
8
9
class StructureController extends Controller
10
{
11
    /**
12
     * Permet de gérer les paramètres par défaut pour la gestion de l'api.
13
     *
14
     * @return OptionsResolver
15
     */
16
    protected function resolveParams()
17
    {
18
        $resolver = new OptionsResolver();
19
        $resolver->setDefaults(
20
            [
21
                'offset' => 0,
22
                'limit'  => 15,
23
                'fields' => "*",
24
                'sort'   => "",
25
                //'filter' => "",
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
26
            ]
27
        );
28
29
        return $resolver;
30
    }
31
32
    /**
33
     * Génère un tableau d'orderBy afin d'afficher les résultats comme voulu.
34
     * @param $sort
35
     *
36
     * @return array
37
     */
38
    protected function getOrderBy($sort)
39
    {
40
        if (empty($sort)) {
41
            return [];
42
        }
43
        $tableauSort = explode(',', $sort);
44
        $export = [];
45
        foreach ($tableauSort as $element) {
46
            $order = substr($element, 0, 1) == '-' ? 'DESC' : 'ASC';
47
            $export[$order == "DESC" ? substr($element, 1) : trim($element)] = $order;
48
        }
49
50
        return $export;
51
    }
52
53
54
    /**
55
     * Permet de gérer les champs quer l'api va retourner. Par défaut elle retournera tous les champs.
56
     *
57
     * @param $fields
58
     *
59
     * @return array
60
     */
61
    protected function getFields($fields)
62
    {
63
        if ($fields == "*") {
64
            return [];
65
        }
66
67
        return explode(",", $fields);
68
    }
69
70
    /**
71
     * Retourne l'entity manager de la connexion defaut.
72
     *
73
     * @return \Doctrine\Common\Persistence\ObjectManager|object
74
     */
75
    protected function getEntityManager()
76
    {
77
        return $this->getDoctrine()->getManager();
78
    }
79
80
    /**
81
     * Retourne les messages d'erreur issu d'un formulaire.
82
     *
83
     * @param $form
84
     *
85
     * @return array
86
     */
87
    protected function getFormErrors($form){
88
        $errors = $this->get("starkerxp_structure.services.form_errors")->getFormErrors($form);
89
        return $errors;
90
    }
91
}
92