1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the doyo/behat-coverage-extension project. |
5
|
|
|
* |
6
|
|
|
* (c) Anthonius Munthi <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
declare(strict_types=1); |
13
|
|
|
|
14
|
|
|
namespace Doyo\Behat\Coverage\Bridge\CodeCoverage\Controller; |
15
|
|
|
|
16
|
|
|
use Doyo\Behat\Coverage\Bridge\CodeCoverage\Session\RemoteSession; |
17
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse; |
18
|
|
|
use Symfony\Component\HttpFoundation\Request; |
19
|
|
|
use Symfony\Component\HttpFoundation\Response; |
20
|
|
|
|
21
|
|
|
class RemoteController |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @return static |
25
|
|
|
*/ |
26
|
2 |
|
public static function create() |
27
|
|
|
{ |
28
|
2 |
|
return new static(); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @return Response |
33
|
|
|
*/ |
34
|
2 |
|
public function getResponse() |
35
|
|
|
{ |
36
|
2 |
|
$request = Request::createFromGlobals(); |
37
|
2 |
|
$action = $request->get('action').'Action'; |
38
|
2 |
|
$callable = [$this, $action]; |
39
|
|
|
|
40
|
2 |
|
if (!method_exists($this, $action)) { |
41
|
1 |
|
$callable = [$this, 'notFoundAction']; |
42
|
|
|
} |
43
|
|
|
|
44
|
2 |
|
return \call_user_func_array($callable, [$request]); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @return JsonResponse |
49
|
|
|
*/ |
50
|
2 |
|
public function notFoundAction() |
51
|
|
|
{ |
52
|
|
|
$data = [ |
53
|
2 |
|
'message' => 'The page you requested is not exists', |
54
|
|
|
]; |
55
|
|
|
|
56
|
2 |
|
return new JsonResponse($data, 404); |
57
|
|
|
} |
58
|
|
|
|
59
|
3 |
|
public function unsupportedMethodAction(Request $request, $supportedMethod) |
60
|
|
|
{ |
61
|
|
|
$data = [ |
62
|
3 |
|
'message' => sprintf( |
63
|
3 |
|
'action: %s not support method: %s. Supported method: %s', |
64
|
3 |
|
$request->get('action'), |
65
|
3 |
|
$request->getMethod(), |
66
|
|
|
$supportedMethod |
67
|
|
|
), |
68
|
|
|
]; |
69
|
|
|
|
70
|
3 |
|
return new JsonResponse($data, Response::HTTP_METHOD_NOT_ALLOWED); |
71
|
|
|
} |
72
|
|
|
|
73
|
3 |
|
public function initAction(Request $request) |
74
|
|
|
{ |
75
|
3 |
|
if (!$request->isMethod(Request::METHOD_POST)) { |
76
|
1 |
|
return $this->unsupportedMethodAction($request, 'POST'); |
77
|
|
|
} |
78
|
2 |
|
$name = $request->get('session'); |
79
|
2 |
|
$config = $request->getContent(); |
80
|
2 |
|
$config = json_decode($config, true); |
81
|
|
|
|
82
|
2 |
|
$session = new RemoteSession($name); |
83
|
2 |
|
$session->init($config); |
84
|
|
|
|
85
|
|
|
$data = [ |
86
|
2 |
|
'message' => 'coverage session: '.$name.' initialized.', |
87
|
|
|
]; |
88
|
|
|
|
89
|
2 |
|
return new JsonResponse($data, Response::HTTP_ACCEPTED); |
90
|
|
|
} |
91
|
|
|
|
92
|
2 |
|
public function readAction(Request $request) |
93
|
|
|
{ |
94
|
2 |
|
if (!$request->isMethod(Request::METHOD_GET)) { |
95
|
1 |
|
return $this->unsupportedMethodAction($request, Request::METHOD_GET); |
96
|
|
|
} |
97
|
|
|
|
98
|
1 |
|
if (!$request->get('session')) { |
99
|
|
|
$data = [ |
100
|
|
|
'message' => 'code coverage session not exists', |
101
|
|
|
]; |
102
|
|
|
|
103
|
|
|
return new JsonResponse($data, Response::HTTP_NOT_FOUND); |
104
|
|
|
} |
105
|
1 |
|
$session = $request->get('session'); |
106
|
1 |
|
$session = new RemoteSession($session); |
107
|
1 |
|
$data = serialize($session->getProcessor()->getCodeCoverage()); |
108
|
|
|
|
109
|
1 |
|
return new Response($data, Response::HTTP_OK); |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|