User::hasSession()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace App\Helpers;
4
5
use App\Facades\Session;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, App\Helpers\Session.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
6
use App\Models\ChocolateyId;
7
use App\Models\User as UserModel;
8
use App\Singleton;
9
use Illuminate\Http\Request;
10
use Illuminate\Support\Facades\Config;
11
12
/**
13
 * Class User.
14
 */
15
final class User extends Singleton
16
{
17
    /**
18
     * Update User Data without overwriting Session.
19
     *
20
     * @param array $parameters
21
     *
22
     * @return UserModel
23
     */
24
    public function updateSession(array $parameters)
25
    {
26
        return $this->setSession($this->updateUser($this->getUser(), $parameters));
0 ignored issues
show
Bug introduced by
It seems like $this->getUser() can be null; however, updateUser() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
27
    }
28
29
    /**
30
     * Set User Data on Session.
31
     *
32
     * @param UserModel $user
33
     *
34
     * @return UserModel
35
     */
36
    public function setSession(UserModel $user)
37
    {
38
        return Session::set(Config::get('chocolatey.security.session'), $user);
39
    }
40
41
    /**
42
     * Update User Data by User Model.
43
     *
44
     * @param UserModel $user
45
     * @param array     $parameters
46
     *
47
     * @return UserModel
48
     */
49
    public function updateUser($user, array $parameters)
50
    {
51
        $user->update($parameters);
52
53
        return $user;
54
    }
55
56
    /**
57
     * Get User Data from Session
58
     * If User Session doesn't exists, return null.
59
     *
60
     * @return UserModel|null
61
     */
62
    public function getUser()
63
    {
64
        return Session::get(Config::get('chocolatey.security.session')) ?? null;
65
    }
66
67
    /**
68
     * Retrieve Non Banned Users (If all Users are Banned, return the Banned user Also).
69
     *
70
     * @param Request      $request
71
     * @param ChocolateyId $chocolateyId
72
     *
73
     * @return UserModel
74
     */
75
    private function checkForBanAlternative(Request $request, ChocolateyId $chocolateyId)
0 ignored issues
show
Unused Code introduced by
The parameter $chocolateyId is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
76
    {
77
        $temporaryUsers = UserModel::where('mail', $request->json()->get('email'))->get();
78
79
        foreach ($temporaryUsers as $forUser) {
80
            if (!$forUser->isBanned) {
81
                return $forUser;
82
            }
83
        }
84
85
        return $temporaryUsers->get(0);
86
    }
87
88
    /**
89
     * Get Users.
90
     *
91
     * @param Request      $request
92
     * @param ChocolateyId $chocolateyId
93
     *
94
     * @return UserModel
95
     */
96
    private function retrieveUser(Request $request, ChocolateyId $chocolateyId)
97
    {
98
        if ($chocolateyId->last_logged_id != 0) {
99
            $temporaryUser = UserModel::find($chocolateyId->last_logged_id);
100
101
            if ($temporaryUser->isBanned) {
102
                return $this->checkForBanAlternative($request, $chocolateyId);
103
            }
104
105
            return $temporaryUser;
106
        }
107
108
        $temporaryUser = UserModel::where('mail', $request->json()->get('email'))->first();
109
110
        if ($temporaryUser->isBanned) {
111
            return $this->checkForBanAlternative($request, $chocolateyId);
112
        }
113
114
        return $temporaryUser;
115
    }
116
117
    /**
118
     * Set Session From Login Credentials.
119
     *
120
     * @param Request $request
121
     *
122
     * @return UserModel
123
     */
124
    public function loginUser(Request $request)
125
    {
126
        $chocolateyId = ChocolateyId::find($request->json()->get('email'));
127
128
        if ($chocolateyId == null) {
129
            return;
130
        }
131
132
        $user = $this->retrieveUser($request, $chocolateyId);
133
134
        $chocolateyId->last_logged_id = $user->uniqueId;
135
136
        return $chocolateyId->password == hash(Config::get('chocolatey.security.hash'), $request->json()->get('password'))
137
            ? $this->setSession($user) : null;
138
    }
139
140
    /**
141
     * Return if USer Session Exists.
142
     *
143
     * @return bool
144
     */
145
    public function hasSession()
146
    {
147
        return (bool) Session::get(Config::get('chocolatey.security.session'));
148
    }
149
150
    /**
151
     * Erase User Session.
152
     */
153
    public function eraseSession()
154
    {
155
        Session::erase(Config::get('chocolatey.security.session'));
156
    }
157
}
158