1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SnifferReport\Controller; |
4
|
|
|
|
5
|
|
|
use Silex\Application; |
6
|
|
|
use SnifferReport\Exception\SnifferReportException; |
7
|
|
|
use SnifferReport\Model\Sniff; |
8
|
|
|
use SnifferReport\Service\Validator; |
9
|
|
|
use SnifferReport\Service\FilesHandler; |
10
|
|
|
use SnifferReport\Service\GitHandler; |
11
|
|
|
use SnifferReport\Service\Sniffer; |
12
|
|
|
use SnifferReport\Service\SniffParser; |
13
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
14
|
|
|
use Symfony\Component\HttpFoundation\Request; |
15
|
|
|
use \Exception; |
16
|
|
|
|
17
|
|
|
class MainController |
18
|
|
|
{ |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Processes the POSTed data. |
22
|
|
|
* |
23
|
|
|
* @param Application $app |
24
|
|
|
* @param Request $request |
25
|
|
|
* |
26
|
|
|
* @return Sniff |
27
|
|
|
* |
28
|
|
|
* @throws SnifferReportException |
29
|
|
|
*/ |
30
|
|
|
public static function processSniff(Application $app, Request $request) |
|
|
|
|
31
|
|
|
{ |
32
|
|
|
$file = $request->files->get('file'); |
33
|
|
|
$git_url = $request->get('git_url'); |
34
|
|
|
$options = $request->get('options'); |
35
|
|
|
|
36
|
|
|
$valid_git_url = (is_null($git_url) || !Validator::isGitUrl($git_url)); |
37
|
|
|
if ($valid_git_url && is_null($file)) { |
38
|
|
|
throw new SnifferReportException('File or Git URL required', 400); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
if (!is_null($file)) { |
42
|
|
|
$file_name = $file->getClientOriginalName(); |
43
|
|
|
$file->move(FILES_DIRECTORY_ROOT, $file_name); |
44
|
|
|
$files = FilesHandler::handle($file_name, $file->getClientMimeType()); |
45
|
|
|
} else { |
46
|
|
|
try { |
47
|
|
|
$files = GitHandler::handle($git_url); |
48
|
|
|
} catch (Exception $e) { |
49
|
|
|
throw new SnifferReportException("Error when trying to clone git repository: {$e->getMessage()}", 500); |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
// @fixme Temporary |
54
|
|
|
if (empty($options)) { |
55
|
|
|
throw new SnifferReportException( |
56
|
|
|
'Missing options', |
57
|
|
|
400 |
58
|
|
|
); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
$options = json_decode($options); |
62
|
|
|
|
63
|
|
|
if (!Validator::areStandardsValid($options->standards)) { |
64
|
|
|
throw new SnifferReportException( |
65
|
|
|
'One or more standards are not valid. Please try again with different values', |
66
|
|
|
400 |
67
|
|
|
); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
if (!Validator::areExtensionsValid($options->extensions)) { |
71
|
|
|
throw new SnifferReportException( |
72
|
|
|
'One or more extensions are not valid. Please try again with different values', |
73
|
|
|
400 |
74
|
|
|
); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
$standards = implode(',', $options->standards); |
78
|
|
|
$extensions = implode(',', $options->extensions); |
79
|
|
|
|
80
|
|
|
$sniff_result = Sniffer::sniffFiles($files, $standards, $extensions); |
81
|
|
|
|
82
|
|
|
// Delete all files that were generated in this request. |
83
|
|
|
$fs = new Filesystem(); |
84
|
|
|
$fs->remove(FILES_DIRECTORY_ROOT); |
85
|
|
|
|
86
|
|
|
try { |
87
|
|
|
// @todo: Make class abstract |
88
|
|
|
$sniffParser = new SniffParser(); |
89
|
|
|
$response = $sniffParser->parseSniff($sniff_result); |
90
|
|
|
} catch (\PDOException $e) { |
91
|
|
|
throw new SnifferReportException("Error when trying to save sniff: {$e->getMessage()}", 500); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
return $response; |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.