Passed
Pull Request — develop (#8)
by ANTHONIUS
04:34
created

RemoteController::create()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1.037

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
ccs 2
cts 3
cp 0.6667
crap 1.037
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 1
    public static function create()
27
    {
28 1
        return new static();
29
    }
30
31
    /**
32
     * @return Response
33
     */
34 1
    public function getResponse()
35
    {
36 1
        $request  = Request::createFromGlobals();
37 1
        $action   = $request->get('action').'Action';
38 1
        $callable = [$this, $action];
39
40 1
        if (!method_exists($this, $action)) {
41 1
            $callable = [$this, 'notFoundAction'];
42
        }
43
44 1
        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 2
    public function initAction(Request $request)
74
    {
75 2
        if (!$request->isMethod(Request::METHOD_POST)) {
76 1
            return $this->unsupportedMethodAction($request, 'POST');
77
        }
78 1
        $name   = $request->get('session');
79 1
        $config = $request->getContent();
80 1
        $config = json_decode($config, true);
81
82 1
        $session = new RemoteSession($name);
83 1
        $session->init($config);
84 1
        $session->save();
85
86
        $data = [
87 1
            'message' => 'coverage session: '.$name.' initialized.',
88
        ];
89
90 1
        return new JsonResponse($data, Response::HTTP_ACCEPTED);
91
    }
92
93 2
    public function readAction(Request $request)
94
    {
95 2
        if (!$request->isMethod(Request::METHOD_GET)) {
96 1
            return $this->unsupportedMethodAction($request, Request::METHOD_GET);
97
        }
98
99 1
        if(!$request->get('session')){
100
            $data = [
101
                'message' => 'code coverage session not exists'
102
            ];
103
            return new JsonResponse($data, Response::HTTP_NOT_FOUND);
104
        }
105 1
        $session = $request->get('session');
106 1
        $session = new RemoteSession($session);
107
108 1
        return new JsonResponse($session->getData(), Response::HTTP_OK);
109
    }
110
}
111