Completed
Push — master ( 671383...5c8721 )
by Antonio Carlos
04:32 queued 02:15
created

Response::sessionPut()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 9
rs 9.6666
cc 1
eloc 5
nc 1
nop 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 Symfony\Component\HttpFoundation\Response as SymfonyResponse;
9
10
trait Response
11
{
12
    /**
13
     * Make a JSON response.
14
     *
15
     * @param $statusCode
16
     *
17
     * @return JsonResponse
18
     */
19
    protected function makeJsonResponse($statusCode)
20
    {
21
        return new IlluminateJsonResponse(
22
            $this->getErrorBagForStatusCode($statusCode),
0 ignored issues
show
Bug introduced by
It seems like getErrorBagForStatusCode() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
23
            $statusCode
24
        );
25
    }
26
27
    /**
28
     * Make the status code, to respond accordingly.
29
     *
30
     * @return int
31
     */
32
    protected function makeStatusCode()
33
    {
34
        return
35
            $this->inputHasOneTimePassword() && !$this->checkOTP()
0 ignored issues
show
Bug introduced by
It seems like inputHasOneTimePassword() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
Bug introduced by
It seems like checkOTP() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
36
                ? SymfonyResponse::HTTP_UNPROCESSABLE_ENTITY
37
                : SymfonyResponse::HTTP_OK;
38
    }
39
40
    /**
41
     * Make a web response.
42
     *
43
     * @param $statusCode
44
     *
45
     * @return \Illuminate\Http\Response
46
     */
47
    protected function makeHtmlResponse($statusCode)
48
    {
49
        if ($statusCode !== SymfonyResponse::HTTP_OK) {
50
            $this->getView()->withErrors($this->getErrorBagForStatusCode($statusCode));
0 ignored issues
show
Bug introduced by
It seems like getView() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
Bug introduced by
It seems like getErrorBagForStatusCode() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
51
        }
52
53
        return new IlluminateHtmlResponse($this->getView(), $statusCode);
0 ignored issues
show
Bug introduced by
It seems like getView() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
54
    }
55
56
    /**
57
     * Put a var value to the current session.
58
     *
59
     * @param $var
60
     * @param $value
61
     *
62
     * @return mixed
63
     */
64
    protected function sessionPut($var, $value)
65
    {
66
        $this->request->session()->put(
0 ignored issues
show
Bug introduced by
The property request does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
67
            $this->makeSessionVarName($var),
0 ignored issues
show
Bug introduced by
It seems like makeSessionVarName() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
68
            $value
69
        );
70
71
        return $value;
72
    }
73
74
    /**
75
     * Forget a session var.
76
     *
77
     * @param null $var
78
     */
79
    protected function sessionForget($var = null)
80
    {
81
        $this->request->session()->forget(
82
            $this->makeSessionVarName($var)
0 ignored issues
show
Bug introduced by
It seems like makeSessionVarName() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
83
        );
84
    }
85
86
    /**
87
     * Create a response to request the OTP.
88
     *
89
     * @return \Illuminate\Http\Response|\Illuminate\Http\JsonResponse
90
     */
91
    public function makeRequestOneTimePasswordResponse()
92
    {
93
        event(new OneTimePasswordRequested($this->getUser()));
0 ignored issues
show
Bug introduced by
It seems like getUser() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
94
95
        return
96
            $this->request->expectsJson()
97
                ? $this->makeJsonResponse($this->makeStatusCode())
98
                : $this->makeHtmlResponse($this->makeStatusCode());
99
    }
100
}
101