Completed
Push — Recipes ( 630f49...c8afb0 )
by Laurent
12:15 queued 03:48
created

ArticleController::reassignAction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 19
rs 9.6333
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
/**
3
 * ArticleController Controller of articles.
4
 *
5
 * PHP Version 7
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 App\Controller\Settings;
16
17
use App\Controller\AbstractAppController;
18
use Symfony\Component\HttpFoundation\Request;
19
use Symfony\Component\Routing\Annotation\Route;
20
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
21
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
22
use App\Entity\Settings\Article;
23
use App\Entity\Settings\Supplier;
24
use App\Form\Type\Settings\ArticleType;
25
use App\Form\Type\Settings\ArticleReassignType;
26
27
/**
28
 * Article controller.
29
 *
30
 * @category Controller
31
 *
32
 * @Route("/article")
33
 */
34
class ArticleController extends AbstractAppController
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 \App\Entity\Settings\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
            'App\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("settings\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
            'App\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 \App\Entity\Settings\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("settings\article/edit.html.twig")
137
     *
138
     * @param \App\Entity\Settings\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
     * Reassign articles from a provider.
156
     *
157
     * @Route("/admin/{slug}/reassign", name="article_reassign")
158
     * @Method("GET")
159
     * @Template()
160
     *
161
     * @param \App\Entity\Supplier $supplier Supplier articles to reassign
162
     * @return array
163
     */
164
    public function reassignAction(Supplier $supplier)
165
    {
166
        $etm = $this->getDoctrine()->getManager();
167
        $suppliers = $etm->getRepository('App:Settings\Supplier')->getSupplierForReassign($supplier);
168
        $articles = $etm->getRepository('App:Settings\Article')
169
            ->getArticleFromSupplier($supplier->getId());
170
171
        $reassignForm = $this->createForm(
172
            ArticleReassignType::class,
173
            $articles,
174
            array('action' => $this->generateUrl('article_change', array('slug' => $supplier->getSlug())),)
175
        );
176
177
        return array(
178
            'reassign_form' => $reassignForm->createView(),
179
            'suppliers' => $suppliers,
180
            'articles' => $articles
181
        );
182
    }
183
184
    /**
185
     * Creates a new Article entity.
186
     *
187
     * @Route("/admin/{slug}/change", name="article_change")
188
     * @Method("POST")
189
     * @Template("settings\article/reassign.html.twig")
190
     *
191
     * @param \Symfony\Component\HttpFoundation\Request $request  Form request
192
     * @param \App\Entity\Supplier                      $supplier Supplier to desactivate
193
     * @return array
194
     */
195
    public function changeAction(Request $request, Supplier $supplier)
196
    {
197
        $etm = $this->getDoctrine()->getManager();
198
        $articles = $etm->getRepository('App:Settings\Article')->getArticleFromSupplier($supplier->getId());
199
200
        $reassignForm = $this->createForm(ArticleReassignType::class, $articles, array(
201
            'action' => $this->generateUrl('article_change', array('slug' => $supplier->getSlug())),
202
        ));
203
        $datas = $reassignForm->handleRequest($request);
204
205
        foreach ($datas as $data) {
206
            $input = explode('-', $data->getName());
207
            list($inputName, $articleId) = $input;
208
            $inputData = $data->getViewData();
209
            if ($inputName === 'supplier') {
210
                $newArticles = $etm->getRepository('App:Settings\Article')->find($articleId);
211
                $newSupplier = $etm->getRepository('App:Settings\Supplier')->find($inputData);
212
                // We modify the supplier of the article
213
                $newArticles->setSupplier($newSupplier);
214
                // We record the object $article in the database
215
                $etm->persist($newArticles);
0 ignored issues
show
Bug introduced by
It seems like $newArticles defined by $etm->getRepository('App...cle')->find($articleId) on line 210 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...
216
            }
217
        }
218
        $etm->flush();
219
        $message = $this->get('translator')
220
            ->trans('delete.reassign_ok', array('%supplier.name%' => $supplier->getName()), 'gs_suppliers');
221
        $this->addFlash('info', $message);
222
        return $this->redirectToRoute('supplier');
223
    }
224
225
    /**
226
     * Save order.
227
     *
228
     * @Route("/order/{entity}/{field}/{type}", name="article_sort")
229
     *
230
     * @param string $entity Entity of the field to sort
231
     * @param string $field  Field to sort
232
     * @param string $type   type of sort
233
     * @return \Symfony\Component\HttpFoundation\RedirectResponse
234
     */
235
    public function sortAction($entity, $field, $type)
236
    {
237
        $this->get('app.helper.controller')->setOrder('article', $entity, $field, $type);
238
239
        return $this->redirect($this->generateUrl('article'));
240
    }
241
    /**
242
     * Deletes a Article entity.
243
     *
244
     * @Route("/admin/{id}/delete", name="article_delete", requirements={"id"="\d+"})
245
     * @Method("DELETE")
246
     *
247
     * @param \App\Entity\Settings\Article              $article Article item to delete
248
     * @param \Symfony\Component\HttpFoundation\Request $request Form request
249
     * @return \Symfony\Component\HttpFoundation\RedirectResponse
250
     */
251
    public function deleteAction(Article $article, Request $request)
252
    {
253
        $form = $this->createDeleteForm($article->getId(), 'article_delete');
254
        if ($form->handleRequest($request)->isValid()) {
255
            $etm = $this->getDoctrine()->getManager();
256
            $article->setActive(false);
257
            $etm->persist($article);
258
            $etm->flush();
259
        }
260
261
        return $this->redirectToRoute('article');
262
    }
263
}
264