CommentsController::deleteCommentAction()   A
last analyzed

Complexity

Conditions 2
Paths 1

Size

Total Lines 7

Duplication

Lines 7
Ratio 100 %

Importance

Changes 0
Metric Value
dl 7
loc 7
rs 10
c 0
b 0
f 0
cc 2
nc 1
nop 1
1
<?php
2
3
namespace KI\CoreBundle\Controller;
4
5
use Nelmio\ApiDocBundle\Annotation\ApiDoc;
6
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
7
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
8
use Symfony\Component\DependencyInjection\ContainerInterface;
9
10
class CommentsController extends ResourceController
11
{
12
    public function setContainer(ContainerInterface $container = null)
13
    {
14
        parent::setContainer($container);
15
        $this->initialize('Comment', 'Core');
16
    }
17
18
    /**
19
     * @ApiDoc(
20
     *  description="Retourne les commentaires",
21
     *  statusCodes={
22
     *   200="Requête traitée avec succès mais pas d’information à renvoyer",
23
     *   401="Une authentification est nécessaire pour effectuer cette View",
24
     *   403="Pas les droits suffisants pour effectuer cette View",
25
     *   404="Ressource non trouvée",
26
     *   503="Service temporairement indisponible ou en maintenance",
27
     *  },
28
     *  section="Likeable"
29
     * )
30
     * @Route("/comments/{slug}")
31
     * @Method("GET")
32
     */
33
    public function getCommentAction($slug)
34
    {
35
        $comment = $this->getOne($slug);
36
37
        return $this->json($comment);
38
    }
39
40
    /**
41
     * @ApiDoc(
42
     *  description="Retourne les commentaires",
43
     *  statusCodes={
44
     *   200="Requête traitée avec succès mais pas d’information à renvoyer",
45
     *   401="Une authentification est nécessaire pour effectuer cette View",
46
     *   403="Pas les droits suffisants pour effectuer cette View",
47
     *   404="Ressource non trouvée",
48
     *   503="Service temporairement indisponible ou en maintenance",
49
     *  },
50
     *  section="Likeable"
51
     * )
52
     * @Route("/{object}/{slug}/comments")
53
     * @Method("GET")
54
     */
55 View Code Duplication
    public function getCommentsAction($object, $slug)
56
    {
57
        $this->trust($this->is('USER'));
58
        $this->autoInitialize($object);
59
        $item = $this->findBySlug($slug);
60
        return $this->json($item->getComments());
61
    }
62
63
    /**
64
     * @ApiDoc(
65
     *  description="Retourne les commentaires d'une sous ressource",
66
     *  statusCodes={
67
     *   200="Requête traitée avec succès mais pas d’information à renvoyer",
68
     *   401="Une authentification est nécessaire pour effectuer cette View",
69
     *   403="Pas les droits suffisants pour effectuer cette View",
70
     *   404="Ressource non trouvée",
71
     *   503="Service temporairement indisponible ou en maintenance",
72
     *  },
73
     *  section="Likeable"
74
     * )
75
     * @Route("/{object}/{slug}/{subobject}/{subslug}/comments")
76
     * @Method("GET")
77
     */
78 View Code Duplication
    public function getCommentsSubAction($object, $slug, $subobject, $subslug)
0 ignored issues
show
Unused Code introduced by
The parameter $object is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $slug is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
79
    {
80
        $this->trust($this->is('USER'));
81
        $this->autoInitialize($subobject);
82
        $item = $this->findBySlug($subslug);
83
        return $this->json($item->getComments());
84
    }
85
86
    /**
87
     * @ApiDoc(
88
     *  description="Ajoute un commentaire",
89
     *  requirements={
90
     *   {
91
     *    "name"="text",
92
     *    "dataType"="string",
93
     *    "description"="Le commentaire"
94
     *   }
95
     *  },
96
     *  statusCodes={
97
     *   201="Requête traitée avec succès avec création d’un document",
98
     *   400="La syntaxe de la requête est erronée",
99
     *   401="Une authentification est nécessaire pour effectuer cette View",
100
     *   403="Pas les droits suffisants pour effectuer cette View",
101
     *   404="Ressource non trouvée",
102
     *   503="Service temporairement indisponible ou en maintenance",
103
     *  },
104
     *  section="Likeable"
105
     * )
106
     * @Route("/{object}/{slug}/comments")
107
     * @Method("POST")
108
     */
109
    public function postCommentAction($object, $slug)
110
    {
111
        $data = $this->post($this->is('USER') && !$this->is('EXTERIEUR'));
112
113
        if ($data['code'] == 201) {
114
            $this->autoInitialize($object);
115
            $item = $this->findBySlug($slug);
116
            $item->addComment($data['item']);
117
        }
118
        $this->initialize('Comment', 'Core');
119
        return $this->formJson($data);
120
    }
121
122
    /**
123
     * @ApiDoc(
124
     *  description="Ajoute un commentaire à une sous ressource",
125
     *  statusCodes={
126
     *   201="Requête traitée avec succès avec création d’un document",
127
     *   400="La syntaxe de la requête est erronée",
128
     *   401="Une authentification est nécessaire pour effectuer cette View",
129
     *   403="Pas les droits suffisants pour effectuer cette View",
130
     *   404="Ressource non trouvée",
131
     *   503="Service temporairement indisponible ou en maintenance",
132
     *  },
133
     *  section="Likeable"
134
     * )
135
     * @Route("/{object}/{slug}/{subobject}/{subslug}/comments")
136
     * @Method("POST")
137
     */
138
    public function postCommentSubAction($object, $slug, $subobject, $subslug)
0 ignored issues
show
Unused Code introduced by
The parameter $object is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $slug is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
139
    {
140
        $return = $this->partialPost();
0 ignored issues
show
Bug introduced by
The method partialPost() does not seem to exist on object<KI\CoreBundle\Con...ler\CommentsController>.

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...
141
142
        if ($return['code'] == 201) {
143
            $this->autoInitialize($subobject);
144
            $item = $this->findBySlug($subslug);
145
            $item->addComment($return['item']);
146
        }
147
        $this->initialize('Comment', 'Core');
148
        return $this->postView($return);
0 ignored issues
show
Bug introduced by
The method postView() does not seem to exist on object<KI\CoreBundle\Con...ler\CommentsController>.

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...
149
    }
150
151
    /**
152
     * @ApiDoc(
153
     *  description="Modifie un commentaire",
154
     *  statusCodes={
155
     *   201="Requête traitée avec succès avec création d’un document",
156
     *   400="La syntaxe de la requête est erronée",
157
     *   401="Une authentification est nécessaire pour effectuer cette View",
158
     *   403="Pas les droits suffisants pour effectuer cette View",
159
     *   404="Ressource non trouvée",
160
     *   503="Service temporairement indisponible ou en maintenance",
161
     *  },
162
     *  section="Likeable"
163
     * )
164
     * @Route("/comments/{id}")
165
     * @Method("PATCH")
166
     */
167 View Code Duplication
    public function patchCommentAction($id)
168
    {
169
        $comment = $this->findBySlug($id);
170
171
        $data = $this->patch($id, !$this->is('ADMIN') && $this->user != $comment->getAuthor());
172
173
        return $this->formJson($data);
174
    }
175
176
    /**
177
     * @ApiDoc(
178
     *  description="Supprime un commentaire",
179
     *  statusCodes={
180
     *   201="Requête traitée avec succès avec création d’un document",
181
     *   400="La syntaxe de la requête est erronée",
182
     *   401="Une authentification est nécessaire pour effectuer cette View",
183
     *   403="Pas les droits suffisants pour effectuer cette View",
184
     *   404="Ressource non trouvée",
185
     *   503="Service temporairement indisponible ou en maintenance",
186
     *  },
187
     *  section="Likeable"
188
     * )
189
     * @Route("/comments/{id}")
190
     * @Method("DELETE")
191
     */
192 View Code Duplication
    public function deleteCommentAction($id)
193
    {
194
        $comment = $this->findBySlug($id);
195
        $this->delete($id, !$this->is('ADMIN') && $this->user != $comment->getAuthor());
196
197
        return $this->json(null, 204);
198
    }
199
}
200