Completed
Push — master ( 465cbc...56a155 )
by
unknown
12:24
created

BatchController::createByBatchReferencesAction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace MediaMonks\SonataMediaBundle\Controller;
4
5
use MediaMonks\SonataMediaBundle\Admin\MediaAdmin;
6
use MediaMonks\SonataMediaBundle\Model\AbstractMedia;
7
use MediaMonks\SonataMediaBundle\Model\MediaInterface;
8
use Symfony\Component\HttpFoundation\JsonResponse;
9
use Symfony\Component\HttpFoundation\Request;
10
use Symfony\Component\HttpFoundation\Response;
11
use Symfony\Component\Routing\RouterInterface;
12
use Symfony\Component\Templating\EngineInterface;
13
14
class BatchController
15
{
16
    /**
17
     * @var MediaAdmin
18
     */
19
    private $mediaAdmin;
20
21
    /**
22
     * @var EngineInterface
23
     */
24
    private $templating;
25
26
    /**
27
     * @var RouterInterface
28
     */
29
    private $router;
30
31
    /**
32
     * @param MediaAdmin $mediaAdmin
33
     * @param EngineInterface $templating
34
     * @param RouterInterface $router
35
     */
36
    public function __construct(MediaAdmin $mediaAdmin, EngineInterface $templating, RouterInterface $router)
37
    {
38
        $this->mediaAdmin = $mediaAdmin;
39
        $this->templating = $templating;
40
        $this->router = $router;
41
    }
42
43
    /**
44
     * @param Request $request
45
     * @return JsonResponse
46
     */
47 View Code Duplication
    public function getAutocompleteItemsAction(Request $request)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
48
    {
49
        $this->mediaAdmin->checkAccess('list');
50
51
        $minimumInputLength = 3;
52
        $searchText = $request->get('q');
53
        if (mb_strlen($searchText, 'UTF-8') < $minimumInputLength) {
54
            return new JsonResponse(['status' => 'KO', 'message' => 'Too short search string'], Response::HTTP_FORBIDDEN);
55
        }
56
57
        $this->mediaAdmin->setPersistFilters(false);
58
        $datagrid = $this->mediaAdmin->getDatagrid();
59
        $datagrid->setValue('title', null, $searchText);
60
        $datagrid->setValue('_per_page', null, $request->query->get('_per_page', 1));
61
        $datagrid->setValue('_page', null, $request->query->get('_page', 10));
62
        $datagrid->buildPager();
63
64
        $pager = $datagrid->getPager();
65
        $results = $pager->getResults();
66
67
        /**
68
         * @var MediaInterface $media
69
         */
70
        $items = [];
71
        foreach($results as $media) {
72
            $items[] = [
73
                'id' => $media->getId(),
74
                'label' => $this->templating->render('@MediaMonksSonataMedia/CRUD/autocomplete.html.twig', [
75
                    'media' => $media
76
                ])
77
            ];
78
        }
79
80
        return new JsonResponse([
81
            'status' => 'OK',
82
            'more' => false,
83
            'items' => $items
84
        ]);
85
    }
86
87
    /**
88
     * @param Request $request
89
     * @return Response
90
     */
91
    public function listAction(Request $request)
0 ignored issues
show
Unused Code introduced by
The parameter $request 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...
92
    {
93
        return new Response($this->templating->render('@MediaMonksSonataMedia/Helper/batch.html.twig'));
94
    }
95
96
    /**
97
     * @param Request $request
98
     * @return JsonResponse
99
     */
100
    public function uploadAction(Request $request)
101
    {
102
        try {
103
            /**
104
             * @var $media AbstractMedia
105
             */
106
            $media = $this->mediaAdmin->getNewInstance();
107
            $media->setProvider('image'); // @todo add support for files
108
            $media->setBinaryContent(current($request->files->all()));
109
            // @todo validate
110
            $this->mediaAdmin->create($media);
111
112
            return new JsonResponse([
113
                'success' => true,
114
                'id' => $media->getId(),
115
                'title' => $media->getTitle(),
116
                'type' => $media->getType(),
117
                'provider' => $media->getProvider(),
118
                'editUrl' => $this->router->generate('admin_mediamonks_sonatamedia_media_edit', [
119
                    'id' => $media->getId()
120
                ])
121
            ]);
122
        }
123
        catch (\Exception $e) {
124
            return new JsonResponse([
125
                'success' => false,
126
                'message' => $e->getMessage()
127
            ]);
128
        }
129
    }
130
131
    /**
132
     * @param Request $request
133
     * @return JsonResponse
134
     */
135
    public function createByBatchReferencesAction(Request $request)
0 ignored issues
show
Unused Code introduced by
The parameter $request 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...
136
    {
137
        return new JsonResponse([]);
138
    }
139
}
140