ImportfileController   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 134
Duplicated Lines 0 %

Test Coverage

Coverage 12.77%

Importance

Changes 0
Metric Value
wmc 8
dl 0
loc 134
rs 10
c 0
b 0
f 0
ccs 6
cts 47
cp 0.1277

4 Methods

Rating   Name   Duplication   Size   Complexity  
B importAction() 0 28 3
A uploadImportfileAction() 0 22 2
A importfilesAction() 0 20 2
A __construct() 0 6 1
1
<?php
2
/* Copyright (C) 2017 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 AdminBundle\Service\TwineImport\ImportTwine;
22
use DembeloMain\Model\Repository\ImportfileRepositoryInterface;
23
use Doctrine\Bundle\MongoDBBundle\ManagerRegistry;
24
use InvalidArgumentException;
25
use Symfony\Component\HttpFoundation\Response;
26
use Symfony\Component\HttpFoundation\Request;
27
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
28
29
/**
30
 * Class ImportController
31
 * @Route(service="app.admin_controller_importfile")
32
 */
33
class ImportfileController
34
{
35
    /**
36
     * @var ImportfileRepositoryInterface
37
     */
38
    private $importfileRepository;
39
40
    /**
41
     * @var ImportTwine
42
     */
43
    private $importTwine;
44
45
    /**
46
     * @var ManagerRegistry
47
     */
48
    private $mongoDb;
49
50
    /**
51
     * @var
52
     */
53
    private $configTwineDirectory;
54
55
    /**
56
     * ImportController constructor.
57
     * @param ImportfileRepositoryInterface $importfileRepository
58
     * @param ImportTwine                   $importTwine
59
     * @param ManagerRegistry               $mongoDb
60
     * @param string                        $configTwineDirectory
61
     */
62 1
    public function __construct(ImportfileRepositoryInterface $importfileRepository, ImportTwine $importTwine, ManagerRegistry $mongoDb, string $configTwineDirectory)
63
    {
64 1
        $this->importfileRepository = $importfileRepository;
65 1
        $this->importTwine = $importTwine;
66 1
        $this->mongoDb = $mongoDb;
67 1
        $this->configTwineDirectory = $configTwineDirectory;
68 1
    }
69
70
    /**
71
     * @Route("/importfiles", name="admin_importfiles")
72
     *
73
     * @return Response
74
     *
75
     * @throws InvalidArgumentException
76
     */
77
    public function importfilesAction(): Response
78
    {
79
        $importfiles = $this->importfileRepository->findAll();
80
81
        $output = array();
82
        /* @var $importfile \DembeloMain\Document\Importfile */
83
        foreach ($importfiles as $importfile) {
84
            $importfileData = [];
85
            $importfileData['id'] = $importfile->getId();
86
            $importfileData['name'] = $importfile->getName();
87
            $importfileData['author'] = $importfile->getAuthor();
88
            $importfileData['publisher'] = $importfile->getPublisher();
89
            $importfileData['imported'] = $importfile->getImported();
90
            $importfileData['orgname'] = $importfile->getOriginalname();
91
            $importfileData['licenseeId'] = $importfile->getLicenseeId();
92
            $importfileData['topicId'] = $importfile->getTopicId();
93
            $output[] = $importfileData;
94
        }
95
96
        return new Response(\json_encode($output));
97
    }
98
99
    /**
100
     * @Route("/import", name="admin_import")
101
     *
102
     * @param Request $request
103
     *
104
     * @return Response
105
     *
106
     * @throws InvalidArgumentException
107
     */
108
    public function importAction(Request $request): Response
109
    {
110
        $importfileId = $request->get('importfileId');
111
112
        /* @var $dm \Doctrine\ODM\MongoDB\DocumentManager*/
113
        $dm = $this->mongoDb->getManager();
114
115
        /* @var $importfile \DembeloMain\Document\Importfile */
116
        $importfile = $this->importfileRepository->find($importfileId);
117
        try {
118
            if (null === $importfile) {
119
                throw new \RuntimeException(sprintf('file with id [%s] not found', $importfileId));
120
            }
121
            $returnValue = $this->importTwine->run($importfile);
122
123
            $dm->flush();
124
            $output = [
125
                'success' => true,
126
                'returnValue' => $returnValue,
127
            ];
128
        } catch (\Exception $e) {
129
            $output = [
130
                'success' => false,
131
                'message' => $e->getMessage().':<br/>'.$e->getTraceAsString(),
132
            ];
133
        }
134
135
        return new Response(json_encode($output));
136
    }
137
138
    /**
139
     * @Route("/uploadimportfile", name="admin_upload_file")
140
     *
141
     * @return Response
142
     *
143
     * @throws InvalidArgumentException
144
     */
145
    public function uploadImportfileAction(): Response
146
    {
147
        $output = array();
148
149
        $file = $_FILES['upload'];
150
151
        if ($file['error'] !== UPLOAD_ERR_OK) {
152
            $output['status'] = 'error';
153
154
            return new Response(\json_encode($output));
155
        }
156
157
        $filename = md5(uniqid('', true).$file['name']);
158
159
        move_uploaded_file($file['tmp_name'], $this->configTwineDirectory.$filename);
160
161
        $output['filename'] = $filename;
162
        $output['orgname'] = $file['name'];
163
164
        $output['status'] = 'server';
165
166
        return new Response(\json_encode($output));
167
    }
168
}
169