Completed
Push — master ( b80d48...49aef3 )
by Guillaume
02:39
created

StructureController::translate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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