Completed
Push — master ( 2e7bd3...0e8e48 )
by Laurent
19:12
created

ArticleController::reassignAction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 22
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 1
Metric Value
c 1
b 1
f 1
dl 0
loc 22
rs 9.2
cc 1
eloc 15
nc 1
nop 1
1
<?php
2
/**
3
 * ArticleController controller des articles.
4
 *
5
 * PHP Version 5
6
 *
7
 * @author    Quétier Laurent <[email protected]>
8
 * @copyright 2014 Dev-Int GLSR
9
 * @license   http://opensource.org/licenses/gpl-license.php GNU Public License
10
 *
11
 * @version since 1.0.0
12
 *
13
 * @link      https://github.com/Dev-Int/glsr
14
 */
15
namespace AppBundle\Controller;
16
17
use Symfony\Component\HttpFoundation\Request;
18
use AppBundle\Controller\AbstractController;
19
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
20
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
21
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
22
use AppBundle\Entity\Article;
23
use AppBundle\Entity\Supplier;
24
use AppBundle\Form\Type\ArticleType;
25
use AppBundle\Form\Type\ArticleReassignType;
26
27
/**
28
 * Article controller.
29
 *
30
 * @category Controller
31
 *
32
 * @Route("/articles")
33
 */
34
class ArticleController extends AbstractController
35
{
36
    /**
37
     * Lists all Article entities.
38
     *
39
     * @Route("/", name="articles")
40
     * @Method("GET")
41
     * @Template()
42
     */
43
    public function indexAction(Request $request)
44
    {
45
        $em = $this->getDoctrine()->getManager();
46
        $qb = $em->getRepository('AppBundle:Article')->getArticles();
47
        $this->addQueryBuilderSort($qb, 'article');
48
        $paginator = $this->get('knp_paginator')->paginate($qb, $request->query->get('page', 1), 5);
49
        
50
        return array(
51
            'paginator' => $paginator,
52
        );
53
    }
54
55
    /**
56
     * Finds and displays a Article entity.
57
     *
58
     * @Route("/{slug}/show", name="articles_show")
59
     * @Method("GET")
60
     * @Template()
61
     */
62
    public function showAction(Article $article)
63
    {
64
        $deleteForm = $this->createDeleteForm($article->getId(), 'articles_delete');
65
66
        return array(
67
            'article' => $article,
68
            'delete_form' => $deleteForm->createView(),
69
        );
70
    }
71
72
    /**
73
     * Displays a form to create a new Article entity.
74
     *
75
     * @Route("/admin/new", name="articles_new")
76
     * @Method("GET")
77
     * @Template()
78
     */
79
    public function newAction()
80
    {
81
        $article = new Article();
82
        $form = $this->createForm(new ArticleType(), $article);
83
84
        return array(
85
            'article' => $article,
86
            'form'   => $form->createView(),
87
        );
88
    }
89
90
    /**
91
     * Creates a new Article entity.
92
     *
93
     * @Route("/create", name="articles_create")
94
     * @Method("POST")
95
     * @Template("AppBundle:Article:new.html.twig")
96
     */
97
    public function createAction(Request $request)
98
    {
99
        $article = new Article();
100
        $form = $this->createForm(new ArticleType(), $article);
101
        if ($form->handleRequest($request)->isValid()) {
102
            $em = $this->getDoctrine()->getManager();
103
            $em->persist($article);
104
            $em->flush();
105
106
            return $this->redirectToRoute('articles_show', array('slug' => $article->getSlug()));
107
        }
108
109
        return array(
110
            'article' => $article,
111
            'form'   => $form->createView(),
112
        );
113
    }
114
115
    /**
116
     * Displays a form to edit an existing Article entity.
117
     *
118
     * @Route("/admin/{slug}/edit", name="articles_edit")
119
     * @Method("GET")
120
     * @Template()
121
     */
122
    public function editAction(Article $article)
123
    {
124
        $editForm = $this->createForm(new ArticleType(), $article, array(
125
            'action' => $this->generateUrl('articles_update', array('slug' => $article->getSlug())),
126
            'method' => 'PUT',
127
        ));
128
        $deleteForm = $this->createDeleteForm($article->getId(), 'articles_delete');
129
130
        return array(
131
            'article' => $article,
132
            'edit_form'   => $editForm->createView(),
133
            'delete_form' => $deleteForm->createView(),
134
        );
135
    }
136
137
    /**
138
     * Edits an existing Article entity.
139
     *
140
     * @Route("/{slug}/update", name="articles_update")
141
     * @Method("PUT")
142
     * @Template("AppBundle:Article:edit.html.twig")
143
     */
144
    public function updateAction(Article $article, Request $request)
145
    {
146
        $editForm = $this->createForm(new ArticleType(), $article, array(
147
            'action' => $this->generateUrl('articles_update', array('slug' => $article->getSlug())),
148
            'method' => 'PUT',
149
        ));
150
        if ($editForm->handleRequest($request)->isValid()) {
151
            $this->getDoctrine()->getManager()->flush();
152
153
            return $this->redirectToRoute('articles_edit', array('slug' => $article->getSlug()));
154
        }
155
        $deleteForm = $this->createDeleteForm($article->getId(), 'articles_delete');
156
157
        return array(
158
            'article' => $article,
159
            'edit_form'   => $editForm->createView(),
160
            'delete_form' => $deleteForm->createView(),
161
        );
162
    }
163
164
    /**
165
     * Réassigner les articles d'un fournisseur.
166
     *
167
     * @param Supplier $supplier Fournisseur à réassigner
168
     * @Route("/{slug}/reassign", name="articles_reassign")
169
     * @Method("GET")
170
     * @Template()
171
     */
172
    public function reassignAction(Supplier $supplier)
173
    {
174
        $em = $this->getDoctrine()->getManager();
175
        $suppliers = $em->getRepository('AppBundle:Supplier')->getSupplierForReassign($supplier);
176
        $articles = $em->getRepository('AppBundle:Article')
177
            ->getArticleFromSupplier($supplier->getId());
178
179
        $reassign_form = $this->createForm(
0 ignored issues
show
Coding Style introduced by
$reassign_form does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
180
            new ArticleReassignType(),
181
            $articles,
182
            array(
183
                'action' => $this->generateUrl('articles_change',array('slug' => $supplier->getSlug())),
184
                'method' => 'PUT',
185
            )
186
        );
187
188
        return array(
189
            'reassign_form' => $reassign_form->createView(),
0 ignored issues
show
Coding Style introduced by
$reassign_form does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
190
            'suppliers' => $suppliers,
191
            'articles' => $articles
192
        );
193
    }
194
195
    /**
196
     * Creates a new Article entity.
197
     *
198
     * @Route("/{slug}/change", name="articles_change")
199
     * @Method("PUT")
200
     * @Template("AppBundle:Article:reassign.html.twig")
201
     */
202
    public function changeAction(Request $request, Supplier $supplier)
203
    {
204
        $em = $this->getDoctrine()->getManager();
205
        $suppliers = $em->getRepository('AppBundle:Supplier')->getSupplierForReassign($supplier);
206
        $articles = $em->getRepository('AppBundle:Article')->getArticleFromSupplier($supplier->getId());
207
        $newArticles = new Article();
0 ignored issues
show
Unused Code introduced by
$newArticles is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
208
        $newSupplier = new Supplier();
0 ignored issues
show
Unused Code introduced by
$newSupplier is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
209
210
        $reassign_form = $this->createForm(
0 ignored issues
show
Coding Style introduced by
$reassign_form does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
211
            new ArticleReassignType(),
212
            $articles,
213
            array(
214
                'action' => $this->generateUrl('articles_change',array('slug' => $supplier->getSlug())),
215
                'method' => 'PUT',
216
            )
217
        );
218
        $datas = $reassign_form->handleRequest($request);
0 ignored issues
show
Coding Style introduced by
$reassign_form does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
219
220
        foreach ($datas as $data) {
221
            $input = explode('-', $data->getName());
222
            list($inputName, $articleId) = $input;
223
            $inputData = $data->getViewData();
224
            if ($inputName === 'supplier') {
225
                $newArticles = $em->getRepository('AppBundle:Article')->find($articleId);
226
                $newSupplier = $em->getRepository('AppBundle:Supplier')->find($inputData);
227
                //On modifie le fournisseur de l'article
228
                $newArticles->setSupplier($newSupplier);
229
                // On enregistre l'objet $article dans la base de données
230
                $em->persist($newArticles);
231
                $em->flush();
232
            }
233
            return $this->redirectToRoute('articles');
234
        }
235
236
        return array(
237
            'reassign_form' => $reassign_form->createView(),
0 ignored issues
show
Coding Style introduced by
$reassign_form does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
238
            'suppliers' => $suppliers,
239
            'articles' => $articles
240
        );
241
    }
242
243
    /**
244
     * Save order.
245
     *
246
     * @Route("/order/{field}/{type}", name="articles_sort")
247
     */
248
    public function sortAction($field, $type)
249
    {
250
        $this->setOrder('article', $field, $type);
251
252
        return $this->redirect($this->generateUrl('articles'));
253
    }
254
    /**
255
     * Deletes a Article entity.
256
     *
257
     * @Route("/admin/{id}/delete", name="articles_delete", requirements={"id"="\d+"})
258
     * @Method("DELETE")
259
     */
260
    public function deleteAction(Article $article, Request $request)
261
    {
262
        $form = $this->createDeleteForm($article->getId(), 'articles_delete');
263
        if ($form->handleRequest($request)->isValid()) {
264
            $em = $this->getDoctrine()->getManager();
265
            $article->setActive(false);
266
            $em->persist($article);
267
            $em->flush();
268
        }
269
270
        return $this->redirectToRoute('articles');
271
    }
272
}
273