Completed
Push — development ( 3ace6a )
by Claudio
10:45 queued 08:01
created

User::checkForBanAlternative()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

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