Completed
Push — master ( 87ae16...8f16eb )
by Guillaume
02:59
created

StructureController::getContainer()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace Starkerxp\StructureBundle\Controller;
4
5
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
6
use Symfony\Component\DependencyInjection\ContainerInterface;
7
use Symfony\Component\HttpFoundation\Request;
8
use Symfony\Component\OptionsResolver\OptionsResolver;
9
10
11
class StructureController extends Controller
12
{
13
    /**
14
     * Permet de gérer les paramètres par défaut pour la gestion de l'api.
15
     *
16
     * @return OptionsResolver
17
     */
18
    protected function resolveParams()
19
    {
20
        $resolver = new OptionsResolver();
21
        $resolver->setDefaults(
22
            [
23
                'offset' => 0,
24
                'limit'  => 15,
25
                'fields' => "*",
26
                'sort'   => "",
27
                //'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...
28
            ]
29
        );
30
31
        return $resolver;
32
    }
33
34
    /**
35
     * Génère un tableau d'orderBy afin d'afficher les résultats comme voulu.
36
     *
37
     * @param $sort
38
     *
39
     * @return array
40
     */
41
    protected function getOrderBy($sort)
42
    {
43
        if (empty($sort)) {
44
            return [];
45
        }
46
        $tableauSort = explode(',', $sort);
47
        $export = [];
48
        foreach ($tableauSort as $element) {
49
            $order = substr($element, 0, 1) == '-' ? 'DESC' : 'ASC';
50
            $export[$order == "DESC" ? substr($element, 1) : trim($element)] = $order;
51
        }
52
53
        return $export;
54
    }
55
56
57
    /**
58
     * Permet de gérer les champs quer l'api va retourner. Par défaut elle retournera tous les champs.
59
     *
60
     * @param $fields
61
     *
62
     * @return array
63
     */
64
    protected function getFields($fields)
65
    {
66
        if ($fields == "*") {
67
            return [];
68
        }
69
70
        return explode(",", $fields);
71
    }
72
73
    /**
74
     * Retourne l'entity manager de la connexion defaut.
75
     *
76
     * @return \Doctrine\Common\Persistence\ObjectManager|object
77
     */
78
    protected function getEntityManager()
79
    {
80
        return $this->getDoctrine()->getManager();
81
    }
82
83
    /**
84
     * Retourne les messages d'erreur issu d'un formulaire.
85
     *
86
     * @param $form
87
     *
88
     * @return array
89
     */
90
    protected function getFormErrors($form)
91
    {
92
        $errors = $this->get("starkerxp_structure.services.form_errors")->getFormErrors($form);
93
94
        return $errors;
95
    }
96
97
    /**
98
     * Retourne un uuid.
99
     *
100
     * @return string
101
     */
102
    protected function getUuid()
103
    {
104
        return (\Ramsey\Uuid\Uuid::uuid4())->toString();
105
    }
106
107
    /**
108
     * Permet de traduire un message.
109
     *
110
     * @param $id
111
     * @param null $domain
112
     * @param array $parameters
113
     *
114
     * @return string
115
     */
116
    protected function translate($id, $domain = null, array $parameters = [])
117
    {
118
        return $this->get('translator')->trans($id, $parameters, $domain);
119
    }
120
121
    /**
122
     * Retourne des données envoyer en json ou POST/PUT.
123
     *
124
     * @param Request $request
125
     *
126
     * @return array|mixed
127
     */
128
    protected function getRequestData(Request $request)
129
    {
130
        $data = json_decode($request->getContent(), true);
131
        if (empty($data)) {
132
            $data = $request->request->all();
133
        }
134
135
        return $data;
136
    }
137
138
    /**
139
     * @return ContainerInterface|null
140
     */
141
    protected function getContainer()
142
    {
143
        return $this->getContainer();
144
    }
145
146
147
}
0 ignored issues
show
Coding Style introduced by
As per coding style, files should not end with a newline character.

This check marks files that end in a newline character, i.e. an empy line.

Loading history...
148