Completed
Push — master ( 8776c1...f76cd1 )
by Guillaume
02:25
created

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

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
151
        $this->get("event_dispatcher")->dispatch($libelle, $event);
152
    }
153
154
}
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...
155