1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SnifferReport\Controller; |
4
|
|
|
|
5
|
|
|
use SnifferReport\Exception\InvalidStandardsException; |
6
|
|
|
use SnifferReport\Exception\GitCloneException; |
7
|
|
|
use SnifferReport\Exception\MissingFileOrGitParameterException; |
8
|
|
|
use SnifferReport\Model\Sniff; |
9
|
|
|
use SnifferReport\Service\Validator; |
10
|
|
|
use SnifferReport\Service\FilesHandler; |
11
|
|
|
use SnifferReport\Service\GitHandler; |
12
|
|
|
use SnifferReport\Service\Sniffer; |
13
|
|
|
use SnifferReport\Service\SniffParser; |
14
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
15
|
|
|
use Symfony\Component\HttpFoundation\Request; |
16
|
|
|
use \Exception; |
17
|
|
|
|
18
|
|
|
class MainController |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* Processes the POSTed data. |
22
|
|
|
* |
23
|
|
|
* @param Request $request |
24
|
|
|
* |
25
|
|
|
* @return Sniff |
26
|
|
|
* |
27
|
|
|
* @throws Exception |
28
|
|
|
* @throws GitCloneException |
29
|
|
|
* @throws InvalidStandardsException |
30
|
|
|
* @throws MissingFileOrGitParameterException |
31
|
|
|
*/ |
32
|
|
|
public static function processSniff(Request $request) |
33
|
|
|
{ |
34
|
|
|
$file = $request->files->get('file'); |
35
|
|
|
$git_url = $request->get('git_url'); |
36
|
|
|
$options = $request->get('options'); |
37
|
|
|
|
38
|
|
|
$valid_git_url = (is_null($git_url) || !Validator::isGitUrl($git_url)); |
39
|
|
|
if ($valid_git_url && is_null($file)) { |
40
|
|
|
throw new MissingFileOrGitParameterException(); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
if (!is_null($file)) { |
44
|
|
|
$file_name = $file->getClientOriginalName(); |
45
|
|
|
$file->move(FILES_DIRECTORY_ROOT, $file_name); |
46
|
|
|
$files = FilesHandler::handle($file_name, $file->getClientMimeType()); |
47
|
|
|
} else { |
48
|
|
|
try { |
49
|
|
|
$files = GitHandler::handle($git_url); |
50
|
|
|
} catch (Exception $e) { |
51
|
|
|
throw new GitCloneException($e); |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
// @fixme Temporary |
56
|
|
|
if (empty($options)) { |
57
|
|
|
throw new \Exception( |
58
|
|
|
'Missing options', |
59
|
|
|
400 |
60
|
|
|
); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
$options = json_decode($options); |
64
|
|
|
|
65
|
|
|
if (!Validator::areStandardsValid($options->standards)) { |
66
|
|
|
throw new InvalidStandardsException(); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
// @fixme: handle extensions and ignoring specific files/folders |
70
|
|
|
|
71
|
|
|
$standards = implode(',', $options->standards); |
72
|
|
|
$extensions = implode(',', $options->extensions); |
73
|
|
|
|
74
|
|
|
$sniff_result = Sniffer::sniffFiles($files, $standards, $extensions); |
75
|
|
|
|
76
|
|
|
// Delete all files that were generated in this request. |
77
|
|
|
$fs = new Filesystem(); |
78
|
|
|
$fs->remove(FILES_DIRECTORY_ROOT); |
79
|
|
|
|
80
|
|
|
return SniffParser::parseSniff($sniff_result); |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|