Passed
Pull Request — master (#15)
by ANTHONIUS
03:07
created

RemoteController   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 114
Duplicated Lines 0 %

Test Coverage

Coverage 50.6%

Importance

Changes 0
Metric Value
eloc 54
dl 0
loc 114
ccs 42
cts 83
cp 0.506
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 initAction() 0 29 4
A readAction() 0 29 4
A create() 0 3 1
1
<?php
2
3
/*
4
 * This file is part of the doyo/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\Bridge\CodeCoverage\Controller;
15
16
use Doyo\Bridge\CodeCoverage\Session\RemoteSession;
17
use Spec\Doyo\Bridge\CodeCoverage\ResponseTrait;
18
use Symfony\Component\HttpFoundation\JsonResponse;
19
use Symfony\Component\HttpFoundation\Request;
20
use Symfony\Component\HttpFoundation\Response;
21
use Symfony\Component\HttpFoundation\Test\Constraint\ResponseHasCookie;
22
23
class RemoteController
24
{
25
    const SERIALIZED_OBJECT_CONTENT_TYPE = 'application/php-serialized-object';
26
27
    /**
28
     * @return static
29
     */
30 1
    public static function create()
31
    {
32 1
        return new static();
33
    }
34
35
    /**
36
     * @return Response
37
     */
38 1
    public function getResponse()
39
    {
40 1
        $request  = Request::createFromGlobals();
41 1
        $action   = $request->get('action').'Action';
42 1
        $callable = [$this, $action];
43
44 1
        if (!method_exists($this, $action)) {
45 1
            $callable = [$this, 'notFoundAction'];
46
        }
47
48 1
        return \call_user_func_array($callable, [$request]);
49
    }
50
51
    /**
52
     * @return JsonResponse
53
     */
54 2
    public function notFoundAction()
55
    {
56
        $data = [
57 2
            'message' => 'The page you requested is not exists',
58
        ];
59
60 2
        return new JsonResponse($data, 404);
61
    }
62
63 3
    public function unsupportedMethodAction(Request $request, $supportedMethod)
64
    {
65
        $data = [
66 3
            'message' => sprintf(
67 3
                'action: %s not support method: %s. Supported method: %s',
68 3
                $request->get('action'),
69 3
                $request->getMethod(),
70
                $supportedMethod
71
            ),
72
        ];
73
74 3
        return new JsonResponse($data, Response::HTTP_METHOD_NOT_ALLOWED);
75
    }
76
77 2
    public function initAction(Request $request)
78
    {
79 2
        if (!$request->isMethod(Request::METHOD_POST)) {
80 1
            return $this->unsupportedMethodAction($request, 'POST');
81
        }
82 1
        $name   = $request->get('session');
83 1
        $config = $request->getContent();
84 1
        $config = json_decode($config, true);
85 1
        $error = 'Failed to create session: <comment>'.$name.'</comment>';
86
87
        try{
88 1
            $created = RemoteSession::init($name, $config);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $created is correct as Doyo\Bridge\CodeCoverage...n::init($name, $config) targeting Doyo\Bridge\CodeCoverage...AbstractSession::init() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
Bug introduced by
It seems like $name can also be of type null; however, parameter $config of Doyo\Bridge\CodeCoverage...AbstractSession::init() does only seem to accept array, 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

88
            $created = RemoteSession::init(/** @scrutinizer ignore-type */ $name, $config);
Loading history...
Bug Best Practice introduced by
The method Doyo\Bridge\CodeCoverage...AbstractSession::init() is not static, but was called statically. ( Ignorable by Annotation )

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

88
            /** @scrutinizer ignore-call */ 
89
            $created = RemoteSession::init($name, $config);
Loading history...
Unused Code introduced by
The call to Doyo\Bridge\CodeCoverage...AbstractSession::init() has too many arguments starting with $config. ( Ignorable by Annotation )

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

88
            /** @scrutinizer ignore-call */ 
89
            $created = RemoteSession::init($name, $config);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
89 1
        }catch (\Exception $e){
90 1
            $error = $e->getMessage();
91 1
            $created = false;
92
        }
93
94 1
        $status = Response::HTTP_ACCEPTED;
95 1
        if($created){
0 ignored issues
show
introduced by
$created is of type false|null, thus it always evaluated to false.
Loading history...
96
            $data = [
97
                'message' => 'coverage session: '.$name.' initialized.',
98
            ];
99
        }else{
100
            $data = [
101 1
                '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 2
        $session = $request->get('session');
122 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

122
        $session = new RemoteSession(/** @scrutinizer ignore-type */ $session);
Loading history...
123 2
        $data    = serialize($session);
124
125
        if (null === $session->getProcessor()) {
0 ignored issues
show
Bug introduced by
Are you sure the usage of $session->getProcessor() targeting Doyo\Bridge\CodeCoverage...Session::getProcessor() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
introduced by
The condition null === $session->getProcessor() is always true.
Loading history...
126
            $data = [
127
                'message' => 'Session '.$session->getName().' is not initialized.',
128
            ];
129
130
            return new JsonResponse($data, Response::HTTP_NOT_FOUND);
131
        }
132
133
        $response =  new Response($data, Response::HTTP_OK);
134
        $response->headers->set('Content-Type', static::SERIALIZED_OBJECT_CONTENT_TYPE);
135
136
        return $response;
137
    }
138
}
139