Completed
Push — master ( 620f01...b7a431 )
by Antonio Carlos
02:08 queued 11s
created

Response::getUser()

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 1
ccs 0
cts 0
cp 0
nc 1
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 7
    protected function makeStatusCode()
34
    {
35 7
        if ($this->getRequest()->isMethod('get') || ($this->checkOTP() === Constants::OTP_VALID)) {
36 4
            return SymfonyResponse::HTTP_OK;
37
        }
38 3
        if ($this->checkOTP() === Constants::OTP_EMPTY) {
39 2
            return SymfonyResponse::HTTP_BAD_REQUEST;
40
        }
41
42 1
        return SymfonyResponse::HTTP_UNPROCESSABLE_ENTITY;
43
    }
44
45
    /**
46
     * Make a web response.
47
     *
48
     * @param $statusCode
49
     *
50
     * @return \Illuminate\Http\Response
51
     */
52 7
    protected function makeHtmlResponse($statusCode)
53
    {
54 7
        $view = $this->getView();
55
56 7
        if ($statusCode !== SymfonyResponse::HTTP_OK) {
57 3
            $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...
58
        }
59
60 7
        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...
61
    }
62
63
    /**
64
     * Create a response to request the OTP.
65
     *
66
     * @return \Illuminate\Http\Response|\Illuminate\Http\JsonResponse
67
     */
68 7
    public function makeRequestOneTimePasswordResponse()
69
    {
70 7
        event(
71 7
            app()->version() < '5.4'
72
                ? new OneTimePasswordRequested53($this->getUser())
73 7
                : new OneTimePasswordRequested($this->getUser())
74
        );
75
76 7
        $expectJson = app()->version() < '5.4'
77
            ? $this->getRequest()->wantsJson()
78 7
            : $this->getRequest()->expectsJson();
79
80 7
        return $expectJson
81
            ? $this->makeJsonResponse($this->makeStatusCode())
82 7
            : $this->makeHtmlResponse($this->makeStatusCode());
83
    }
84
85
    /**
86
     * Get the OTP view.
87
     *
88
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
89
     */
90 7
    private function getView()
91
    {
92 7
        return view($this->config('view'));
93
    }
94
95
    abstract protected function getErrorBagForStatusCode($statusCode);
96
97
    abstract protected function inputHasOneTimePassword();
98
99
    abstract public function checkOTP();
100
101
    abstract protected function getUser();
102
103
    abstract public function getRequest();
104
105
    abstract protected function config($string, $children = []);
106
}
107