Completed
Push — master ( 1af8b6...90bc50 )
by Laurent
03:35
created

ArticleController::changeAction()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 29
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 29
rs 8.8571
cc 3
eloc 20
nc 3
nop 2
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 GIT: <git_id>
12
 *
13
 * @link      https://github.com/Dev-Int/glsr
14
 */
15
namespace AppBundle\Controller\Settings;
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\Settings\Article;
23
use AppBundle\Entity\Settings\Supplier;
24
use AppBundle\Form\Type\Settings\ArticleType;
25
use AppBundle\Form\Type\Settings\ArticleReassignType;
26
27
/**
28
 * Article controller.
29
 *
30
 * @category Controller
31
 *
32
 * @Route("/article")
33
 */
34
class ArticleController extends AbstractController
35
{
36
    /**
37
     * Lists all Article entities.
38
     *
39
     * @Route("/", name="article")
40
     * @Method("GET")
41
     * @Template()
42
     *
43
     * @param \Symfony\Component\HttpFoundation\Request $request Paginate request
44
     * @return array
45
     */
46
    public function indexAction(Request $request)
47
    {
48
        $return = $this->abstractIndexAction('Settings\Article', 'article', $request);
49
        
50
        return $return;
51
    }
52
53
    /**
54
     * Finds and displays a Article entity.
55
     *
56
     * @Route("/{slug}/show", name="article_show")
57
     * @Method("GET")
58
     * @Template()
59
     *
60
     * @param \AppBundle\Entity\Article $article Article item to display
61
     * @return array
62
     */
63
    public function showAction(Article $article)
64
    {
65
        $return = $this->abstractShowAction($article, 'article');
66
67
        return $return;
68
    }
69
70
    /**
71
     * Displays a form to create a new Article entity.
72
     *
73
     * @Route("/admin/new", name="article_new")
74
     * @Method("GET")
75
     * @Template()
76
     *
77
     * @return array
78
     */
79
    public function newAction()
80
    {
81
        $return = $this->abstractNewAction(
82
            'Settings\Article',
83
            'AppBundle\Entity\Settings\Article',
84
            ArticleType::class,
85
            'article'
86
        );
87
88
        return $return;
89
    }
90
91
    /**
92
     * Creates a new Article entity.
93
     *
94
     * @Route("/admin/create", name="article_create")
95
     * @Method("POST")
96
     * @Template("AppBundle:Article:new.html.twig")
97
     *
98
     * @param \Symfony\Component\HttpFoundation\Request $request Form request
99
     * @return array
100
     */
101
    public function createAction(Request $request)
102
    {
103
        $return = $this->abstractCreateAction(
104
            $request,
105
            'Settings\Article',
106
            'AppBundle\Entity\Settings\Article',
107
            ArticleType::class,
108
            'article'
109
        );
110
111
        return $return;
112
    }
113
114
    /**
115
     * Displays a form to edit an existing Article entity.
116
     *
117
     * @Route("/admin/{slug}/edit", name="article_edit")
118
     * @Method("GET")
119
     * @Template()
120
     *
121
     * @param \AppBundle\Entity\Article $article Article item to edit
122
     * @return array
123
     */
124
    public function editAction(Article $article)
125
    {
126
        $return = $this->abstractEditAction($article, 'article', ArticleType::class);
127
128
        return $return;
129
    }
130
131
    /**
132
     * Edits an existing Article entity.
133
     *
134
     * @Route("/admin/{slug}/update", name="article_update")
135
     * @Method("PUT")
136
     * @Template("AppBundle:Article:edit.html.twig")
137
     *
138
     * @param \AppBundle\Entity\Article                 $article Article item to update
139
     * @param \Symfony\Component\HttpFoundation\Request $request Form request
140
     * @return array
141
     */
142
    public function updateAction(Article $article, Request $request)
143
    {
144
        $return = $this->abstractUpdateAction(
145
            $article,
146
            $request,
147
            'article',
148
            ArticleType::class
149
        );
150
151
        return $return;
152
    }
153
154
    /**
155
     * Réassigner les articles d'un fournisseur.
156
     *
157
     * @param Supplier $supplier Fournisseur à réassigner
158
     * @Route("/admin/{slug}/reassign", name="article_reassign")
159
     * @Method("GET")
160
     * @Template()
161
     *
162
     * @param \AppBundle\Entity\Supplier $supplier Supplier articles to reassign
163
     * @return array
164
     */
165
    public function reassignAction(Supplier $supplier)
166
    {
167
        $etm = $this->getDoctrine()->getManager();
168
        $suppliers = $etm->getRepository('AppBundle:Settings\Supplier')->getSupplierForReassign($supplier);
169
        $articles = $etm->getRepository('AppBundle:Settings\Article')
170
            ->getArticleFromSupplier($supplier->getId());
171
172
        $reassignForm = $this->createForm(
173
            ArticleReassignType::class,
174
            $articles,
175
            array('action' => $this->generateUrl('article_change', array('slug' => $supplier->getSlug())),)
176
        );
177
178
        return array(
179
            'reassign_form' => $reassignForm->createView(),
180
            'suppliers' => $suppliers,
181
            'articles' => $articles
182
        );
183
    }
184
185
    /**
186
     * Creates a new Article entity.
187
     *
188
     * @Route("/admin/{slug}/change", name="article_change")
189
     * @Method("POST")
190
     * @Template("AppBundle:Article:reassign.html.twig")
191
     *
192
     * @param \Symfony\Component\HttpFoundation\Request $request  Form request
193
     * @param \AppBundle\Entity\Supplier                $supplier Supplier to desactivate
194
     * @return array
195
     */
196
    public function changeAction(Request $request, Supplier $supplier)
197
    {
198
        $etm = $this->getDoctrine()->getManager();
199
        $articles = $etm->getRepository('AppBundle:Settings\Article')->getArticleFromSupplier($supplier->getId());
200
201
        $reassignForm = $this->createForm(ArticleReassignType::class, $articles, array(
202
            'action' => $this->generateUrl('article_change', array('slug' => $supplier->getSlug())),
203
        ));
204
        $datas = $reassignForm->handleRequest($request);
205
206
        foreach ($datas as $data) {
207
            $input = explode('-', $data->getName());
208
            list($inputName, $articleId) = $input;
209
            $inputData = $data->getViewData();
210
            if ($inputName === 'supplier') {
211
                $newArticles = $etm->getRepository('AppBundle:Settings\Article')->find($articleId);
212
                $newSupplier = $etm->getRepository('AppBundle:Settings\Supplier')->find($inputData);
213
                //On modifie le fournisseur de l'article
214
                $newArticles->setSupplier($newSupplier);
215
                // On enregistre l'objet $article dans la base de données
216
                $etm->persist($newArticles);
1 ignored issue
show
Bug introduced by
It seems like $newArticles defined by $etm->getRepository('App...cle')->find($articleId) on line 211 can also be of type null; however, Doctrine\Common\Persiste...bjectManager::persist() does only seem to accept object, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
217
            }
218
        }
219
        $etm->flush();
220
        $message = $this->get('translator')
221
            ->trans('delete.reassign_ok', array('%supplier.name%' => $supplier->getName()), 'gs_suppliers');
222
        $this->addFlash('info', $message);
223
        return $this->redirectToRoute('supplier');
224
    }
225
226
    /**
227
     * Save order.
228
     *
229
     * @Route("/order/{entity}/{field}/{type}", name="article_sort")
230
     *
231
     * @param string $entity Entity of the field to sort
232
     * @param string $field  Field to sort
233
     * @param string $type   type of sort
234
     * @return \Symfony\Component\HttpFoundation\RedirectResponse
235
     */
236
    public function sortAction($entity, $field, $type)
237
    {
238
        $this->get('app.helper.controller')->setOrder('article', $entity, $field, $type);
239
240
        return $this->redirect($this->generateUrl('article'));
241
    }
242
    /**
243
     * Deletes a Article entity.
244
     *
245
     * @Route("/admin/{id}/delete", name="article_delete", requirements={"id"="\d+"})
246
     * @Method("DELETE")
247
     *
248
     * @param \AppBundle\Entity\Article                 $article Article item to delete
249
     * @param \Symfony\Component\HttpFoundation\Request $request Form request
250
     * @return \Symfony\Component\HttpFoundation\RedirectResponse
251
     */
252
    public function deleteAction(Article $article, Request $request)
253
    {
254
        $form = $this->createDeleteForm($article->getId(), 'article_delete');
255
        if ($form->handleRequest($request)->isValid()) {
256
            $etm = $this->getDoctrine()->getManager();
257
            $article->setActive(false);
258
            $etm->persist($article);
259
            $etm->flush();
260
        }
261
262
        return $this->redirectToRoute('article');
263
    }
264
}
265