Completed
Push — develop ( c86111...2faf16 )
by ANTHONIUS
21s
created

RemoteController   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Test Coverage

Coverage 57.14%

Importance

Changes 0
Metric Value
eloc 38
dl 0
loc 89
ccs 36
cts 63
cp 0.5714
rs 10
c 0
b 0
f 0
wmc 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getResponse() 0 11 2
A notFoundAction() 0 7 1
A create() 0 3 1
A unsupportedMethodAction() 0 12 1
A initAction() 0 17 2
A readAction() 0 18 3
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