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

RemoteController::unsupportedMethodAction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1.0938

Importance

Changes 0
Metric Value
cc 1
eloc 7
nc 1
nop 2
dl 0
loc 12
rs 10
c 0
b 0
f 0
ccs 6
cts 11
cp 0.5455
crap 1.0938
1
<?php
2
3
namespace Doyo\Behat\Coverage\Bridge\CodeCoverage\Controller;
4
5
use Doyo\Behat\Coverage\Bridge\CodeCoverage\Cache;
6
use Doyo\Behat\Coverage\Bridge\CodeCoverage\Processor;
7
use Symfony\Component\HttpFoundation\JsonResponse;
8
use Symfony\Component\HttpFoundation\Request;
9
use Symfony\Component\HttpFoundation\Response;
10
11
class RemoteController
12
{
13
    /**
14
     * @return static
15
     */
16 3
    public static function create()
17
    {
18 3
        return new static();
19
    }
20
21
    /**
22
     * @return Response
23
     */
24 3
    public function getResponse()
25
    {
26 3
        $request = Request::createFromGlobals();
27 3
        $action = $request->get('action').'Action';
28 3
        $callable = [$this,$action];
29
30 3
        if(!method_exists($this,$action)){
31 2
            $callable = [$this,'notFoundAction'];
32
        }
33
34 3
        return call_user_func_array($callable,[$request]);
35
    }
36
37
    /**
38
     * @param Request $request
39
     * @return JsonResponse
40
     */
41 3
    public function notFoundAction()
42
    {
43
        $data = [
44 3
            'message' => 'The page you requested is not exists',
45
        ];
46 3
        return new JsonResponse($data, 404);
47
    }
48
49 1
    public function unsupportedMethodAction(Request $request, $supportedMethod)
50
    {
51
        $data = [
52 1
            'message' => sprintf(
53 1
                'action: %s not support method: %s. Supported method: %s',
54 1
                $request->get('action'),
55 1
                $request->getMethod(),
56
                $supportedMethod
57
            ),
58
        ];
59
60 1
        return new JsonResponse($data,Response::HTTP_METHOD_NOT_ALLOWED);
61
    }
62
63 2
    public function initAction(Request $request)
64
    {
65 2
        if(!$request->isMethod(Request::METHOD_POST)){
66
            return $this->unsupportedMethodAction($request,'POST');
67
        }
68 2
        $session = $request->get('session');
69 2
        $config = $request->getContent();
70 2
        $config = json_decode($config, true);
71
72 2
        $cache = new Cache($session);
73 2
        $cache->setFilter($config['filter']);
74 2
        $cache->setCodeCoverageOptions($config['codeCoverageOptions']);
75 2
        $cache->save();
76
77
        $data = [
78 2
            'message' => 'coverage session: '.$session.' initialized.'
79
        ];
80 2
        return new JsonResponse($data,Response::HTTP_ACCEPTED);
81
    }
82
}
83