Passed
Push — develop ( 734f70...83757e )
by ANTHONIUS
03:38
created

RemoteController   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 102
Duplicated Lines 0 %

Test Coverage

Coverage 60.56%

Importance

Changes 0
Metric Value
eloc 45
dl 0
loc 102
ccs 43
cts 71
cp 0.6056
rs 10
c 0
b 0
f 0
wmc 11

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getResponse() 0 11 2
A notFoundAction() 0 7 1
A create() 0 3 1
A initAction() 0 17 2
A unsupportedMethodAction() 0 12 1
A readAction() 0 29 4
1
<?php
2
3
/*
4
 * This file is part of the doyo/behat-code-coverage 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
    const SERIALIZED_OBJECT_CONTENT_TYPE = 'application/php-serialized-object';
24
25
    /**
26
     * @return static
27
     */
28 8
    public static function create()
29
    {
30 8
        return new static();
31
    }
32
33
    /**
34
     * @return Response
35
     */
36 8
    public function getResponse()
37
    {
38 8
        $request  = Request::createFromGlobals();
39 8
        $action   = $request->get('action').'Action';
40 8
        $callable = [$this, $action];
41
42 8
        if (!method_exists($this, $action)) {
43 2
            $callable = [$this, 'notFoundAction'];
44
        }
45
46 8
        return \call_user_func_array($callable, [$request]);
47
    }
48
49
    /**
50
     * @return JsonResponse
51
     */
52 3
    public function notFoundAction()
53
    {
54
        $data = [
55 3
            'message' => 'The page you requested is not exists',
56
        ];
57
58 3
        return new JsonResponse($data, 404);
59
    }
60
61 3
    public function unsupportedMethodAction(Request $request, $supportedMethod)
62
    {
63
        $data = [
64 3
            'message' => sprintf(
65 3
                'action: %s not support method: %s. Supported method: %s',
66 3
                $request->get('action'),
67 3
                $request->getMethod(),
68
                $supportedMethod
69
            ),
70
        ];
71
72 3
        return new JsonResponse($data, Response::HTTP_METHOD_NOT_ALLOWED);
73
    }
74
75 7
    public function initAction(Request $request)
76
    {
77 7
        if (!$request->isMethod(Request::METHOD_POST)) {
78 1
            return $this->unsupportedMethodAction($request, 'POST');
79
        }
80 6
        $name   = $request->get('session');
81 6
        $config = $request->getContent();
82 6
        $config = json_decode($config, true);
83
84 6
        $session = new RemoteSession($name);
85 6
        $session->init($config);
86
87
        $data = [
88 6
            'message' => 'coverage session: '.$name.' initialized.',
89
        ];
90
91 6
        return new JsonResponse($data, Response::HTTP_ACCEPTED);
92
    }
93
94 9
    public function readAction(Request $request)
95
    {
96 9
        if (!$request->isMethod(Request::METHOD_GET)) {
97 1
            return $this->unsupportedMethodAction($request, Request::METHOD_GET);
98
        }
99
100 8
        if (!$request->get('session')) {
101
            $data = [
102 1
                'message' => 'code coverage session not exists',
103
            ];
104
105 1
            return new JsonResponse($data, Response::HTTP_NOT_FOUND);
106
        }
107 7
        $session = $request->get('session');
108 7
        $session = new RemoteSession($session);
109 7
        $data    = serialize($session);
110
111 7
        if (null === $session->getProcessor()) {
112
            $data = [
113 2
                'message' => 'Session '.$session->getName().' is not initialized.',
114
            ];
115
116 2
            return new JsonResponse($data, Response::HTTP_NOT_FOUND);
117
        }
118
119 5
        $response =  new Response($data, Response::HTTP_OK);
120 5
        $response->headers->set('Content-Type', static::SERIALIZED_OBJECT_CONTENT_TYPE);
121
122 5
        return $response;
123
    }
124
}
125