RemoteController   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 118
Duplicated Lines 0 %

Test Coverage

Coverage 55.28%

Importance

Changes 0
Metric Value
eloc 56
dl 0
loc 118
ccs 47
cts 85
cp 0.5528
rs 10
c 0
b 0
f 0
wmc 13

6 Methods

Rating   Name   Duplication   Size   Complexity  
A unsupportedMethodAction() 0 12 1
A getResponse() 0 11 2
A notFoundAction() 0 7 1
A create() 0 3 1
A initAction() 0 31 4
A readAction() 0 31 4
1
<?php
2
3
/*
4
 * This file is part of the doyo/code-coverage project.
5
 *
6
 * (c) Anthonius Munthi <https://itstoni.com>
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\Bridge\CodeCoverage\Controller;
15
16
use Doyo\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 1
    public static function create()
29
    {
30 1
        return new static();
31
    }
32
33
    /**
34
     * @return Response
35
     */
36 1
    public function getResponse()
37
    {
38 1
        $request  = Request::createFromGlobals();
39 1
        $action   = $request->get('action').'Action';
40 1
        $callable = [$this, $action];
41
42 1
        if (!method_exists($this, $action)) {
43 1
            $callable = [$this, 'notFoundAction'];
44
        }
45
46 1
        return \call_user_func_array($callable, [$request]);
47
    }
48
49
    /**
50
     * @return JsonResponse
51
     */
52 2
    public function notFoundAction()
53
    {
54
        $data = [
55 2
            'message' => 'The page you requested is not exists',
56
        ];
57
58 2
        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 2
    public function initAction(Request $request)
76
    {
77 2
        if (!$request->isMethod(Request::METHOD_POST)) {
78 1
            return $this->unsupportedMethodAction($request, 'POST');
79
        }
80 1
        $name   = $request->get('session');
81 1
        $config = $request->getContent();
82 1
        $config = json_decode($config, true);
83 1
        $error  = 'Failed to create session: <comment>'.$name.'</comment>';
84
85
        try {
86 1
            $session = new RemoteSession($name);
0 ignored issues
show
Bug introduced by
It seems like $name can also be of type null; however, parameter $name of Doyo\Bridge\CodeCoverage...eSession::__construct() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

86
            $session = new RemoteSession(/** @scrutinizer ignore-type */ $name);
Loading history...
87 1
            $session->init($config);
88 1
            $created = true;
89
        } catch (\Exception $e) {
90
            $error   = $e->getMessage();
91
            $created = false;
92
        }
93
94 1
        $status = Response::HTTP_ACCEPTED;
95 1
        if ($created) {
96
            $data = [
97 1
                'message' => 'coverage session: '.$name.' initialized.',
98
            ];
99
        } else {
100
            $data = [
101
                'message' => $error,
102
            ];
103
        }
104
105 1
        return new JsonResponse($data, $status);
106
    }
107
108 4
    public function readAction(Request $request)
109
    {
110 4
        if (!$request->isMethod(Request::METHOD_GET)) {
111 1
            return $this->unsupportedMethodAction($request, Request::METHOD_GET);
112
        }
113
114 3
        if (!$request->get('session')) {
115
            $data = [
116 1
                'message' => 'code coverage session not exists',
117
            ];
118
119 1
            return new JsonResponse($data, Response::HTTP_NOT_FOUND);
120
        }
121
122 2
        $session = $request->get('session');
123 2
        $session = new RemoteSession($session);
0 ignored issues
show
Bug introduced by
It seems like $session can also be of type null; however, parameter $name of Doyo\Bridge\CodeCoverage...eSession::__construct() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

123
        $session = new RemoteSession(/** @scrutinizer ignore-type */ $session);
Loading history...
124
125 2
        if (null === $session->getProcessor()) {
126
            $data = [
127 1
                'message' => 'Session '.$session->getName().' is not initialized.',
128
            ];
129
130 1
            return new JsonResponse($data, Response::HTTP_NOT_FOUND);
131
        }
132
133 1
        $data    = serialize($session);
134
135 1
        $response =  new Response($data, Response::HTTP_OK);
136 1
        $response->headers->set('Content-Type', static::SERIALIZED_OBJECT_CONTENT_TYPE);
137
138 1
        return $response;
139
    }
140
}
141