Completed
Push — master ( 56bf17...6fe368 )
by Antonio Carlos
06:48
created

Response::makeJsonResponse()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 0
cts 3
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 2
1
<?php
2
3
namespace PragmaRX\Google2FALaravel\Support;
4
5
use Illuminate\Http\JsonResponse as IlluminateJsonResponse;
6
use Illuminate\Http\Response as IlluminateHtmlResponse;
7
use PragmaRX\Google2FALaravel\Events\OneTimePasswordRequested;
8
use PragmaRX\Google2FALaravel\Events\OneTimePasswordRequested53;
9
use Symfony\Component\HttpFoundation\Response as SymfonyResponse;
10
11
trait Response
12
{
13
    /**
14
     * Make a JSON response.
15
     *
16
     * @param $statusCode
17
     *
18
     * @return IlluminateJsonResponse
19
     */
20
    protected function makeJsonResponse($statusCode)
21
    {
22
        return new IlluminateJsonResponse(
23
            $this->getErrorBagForStatusCode($statusCode),
24
            $statusCode
25
        );
26
    }
27
28
    /**
29
     * Make the status code, to respond accordingly.
30
     *
31
     * @return int
32
     */
33 5
    protected function makeStatusCode()
34
    {
35
        return
36 5
            $this->inputHasOneTimePassword() && !$this->checkOTP()
37 1
                ? SymfonyResponse::HTTP_UNPROCESSABLE_ENTITY
38 5
                : SymfonyResponse::HTTP_OK;
39
    }
40
41
    /**
42
     * Make a web response.
43
     *
44
     * @param $statusCode
45
     *
46
     * @return \Illuminate\Http\Response
47
     */
48 5
    protected function makeHtmlResponse($statusCode)
49
    {
50 5
        $view = $this->getView();
51
52 5
        if ($statusCode !== SymfonyResponse::HTTP_OK) {
53 1
            $view->withErrors($this->getErrorBagForStatusCode($statusCode));
0 ignored issues
show
Bug introduced by
The method withErrors does only exist in Illuminate\View\View, but not in Illuminate\Contracts\View\Factory.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
54
        }
55
56 5
        return new IlluminateHtmlResponse($view, $statusCode);
0 ignored issues
show
Documentation introduced by
$view is of type object<Illuminate\View\V...Contracts\View\Factory>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
57
    }
58
59
    /**
60
     * Create a response to request the OTP.
61
     *
62
     * @return \Illuminate\Http\Response|\Illuminate\Http\JsonResponse
63
     */
64 5
    public function makeRequestOneTimePasswordResponse()
65
    {
66 5
        event(
67 5
            app()->version() < '5.4'
68
                ? new OneTimePasswordRequested53($this->getUser())
69 5
                : new OneTimePasswordRequested($this->getUser())
70
        );
71
72 5
        $expectJson = app()->version() < '5.4'
73
            ? $this->getRequest()->wantsJson()
74 5
            : $this->getRequest()->expectsJson();
75
76 5
        return $expectJson
77
            ? $this->makeJsonResponse($this->makeStatusCode())
78 5
            : $this->makeHtmlResponse($this->makeStatusCode());
79
    }
80
81
    /**
82
     * Get the OTP view.
83
     *
84
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
85
     */
86 5
    private function getView()
87
    {
88 5
        return view($this->config('view'));
89
    }
90
91
    abstract protected function getErrorBagForStatusCode($statusCode);
92
93
    abstract protected function inputHasOneTimePassword();
94
95
    abstract public function checkOTP();
96
97
    abstract protected function getUser();
98
99
    abstract public function getRequest();
100
101
    abstract protected function config($string, $children = []);
102
}
103