TopicController::topicSuggestAction()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 18
rs 9.4285
ccs 0
cts 10
cp 0
cc 2
eloc 9
nc 2
nop 1
crap 6
1
<?php
2
/* Copyright (C) 2016 Michael Giesler
3
 *
4
 * This file is part of Dembelo.
5
 *
6
 * Dembelo is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU Affero General Public License as published by
8
 * the Free Software Foundation, either version 3 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * Dembelo is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
 * GNU Affero General Public License 3 for more details.
15
 *
16
 * You should have received a copy of the GNU Affero General Public License 3
17
 * along with Dembelo. If not, see <http://www.gnu.org/licenses/>.
18
 */
19
namespace AdminBundle\Controller;
20
21
use DembeloMain\Model\Repository\TopicRepositoryInterface;
22
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
23
use Symfony\Component\HttpFoundation\Request;
24
use Symfony\Component\HttpFoundation\Response;
25
26
/**
27
 * Class TopicController
28
 *
29
 * @Route(service="app.controller_admin_topic")
30
 */
31
class TopicController
32
{
33
    /**
34
     * @var TopicRepositoryInterface
35
     */
36
    private $topicRepository;
37
38
    /**
39
     * TopicController constructor.
40
     * @param TopicRepositoryInterface $topicRepository
41
     */
42 5
    public function __construct(TopicRepositoryInterface $topicRepository)
43
    {
44 5
        $this->topicRepository = $topicRepository;
45 5
    }
46
47
    /**
48
     * @Route("/topic/list", name="admin_topics")
49
     *
50
     * @param Request $request
51
     *
52
     * @return Response
53
     */
54 3
    public function listAction(Request $request): Response
55
    {
56 3
        $filters = $request->query->get('filter');
57
58 3
        if (null === $filters) {
59 2
            $topics = $this->topicRepository->findBy([], ['sortKey' => 'ASC']);
60
        } else {
61 1
            $topics = $this->topicRepository->findFiltered($filters, ['sortKey', 'ASC']);
62
        }
63
64 3
        if (null === $topics) {
65 1
            return new Response(\json_encode([]));
66
        }
67
68 2
        $output = [] ;
69
        /* @var $topic \DembeloMain\Document\Topic */
70 2
        foreach ($topics as $topic) {
71 2
            $item = [];
72 2
            $item['id'] = $topic->getId();
73 2
            $item['name'] = $topic->getName();
74 2
            $item['status'] = (String) $topic->getStatus();
75 2
            $item['sortKey'] = $topic->getSortKey();
76 2
            $item['originalImageName'] = $topic->getOriginalImageName();
77 2
            $output[] = $item;
78
        }
79
80 2
        return new Response(\json_encode($output));
81
    }
82
83
    /**
84
     * @Route("/topicSuggest", name="admin_topic_suggest")
85
     *
86
     * @param Request $request
87
     *
88
     * @return Response
89
     *
90
     * @throws \InvalidArgumentException
91
     */
92
    public function topicSuggestAction(Request $request): Response
93
    {
94
        $filter = $request->query->get('filter');
95
96
        $searchString = $filter['value'];
97
98
        /* @var $topics \DembeloMain\Document\Topic[] */
99
        $topics = $this->topicRepository->findBy(array('name' => new \MongoRegex('/'.$searchString.'/')), null, 10);
100
101
        $output = [];
102
        foreach ($topics as $topic) {
103
            $output[] = array(
104
                'id' => $topic->getId(),
105
                'value' => $topic->getName(),
106
            );
107
        }
108
109
        return new Response(\json_encode($output));
110
    }
111
112
    /**
113
     * @Route("/topic/uploadimage", name="admin_topics_image")
114
     *
115
     * @param Request $request
116
     *
117
     * @return Response
118
     *
119
     * @throws \InvalidArgumentException
120
     */
121 2
    public function uploadImageAction(Request $request): Response
122
    {
123 2
        $output = [];
124 2
        $file = $_FILES['upload'];
125 2
        if ($file['error'] !== UPLOAD_ERR_OK) {
126 1
            $output['status'] = 'error';
127
128 1
            return new Response(\json_encode($output));
129
        }
130 1
        $directory = $request->query->get('topic_image_directory');
131 1
        $filename = md5(uniqid('', true).$file['name']);
132 1
        move_uploaded_file($file["tmp_name"], $directory.$filename);
133 1
        $output['imageFileName'] = $filename;
134 1
        $output['originalImageName'] = $file['name'];
135
136 1
        return new Response(\json_encode($output));
137
    }
138
}
139