Completed
Push — development ( f57bd7...a7d33c )
by Claudio
04:40
created

User   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 115
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 2
dl 0
loc 115
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A getInstance() 0 10 2
A updateSession() 0 4 1
A setSession() 0 4 1
A updateData() 0 6 1
A getUser() 0 4 1
A loginUser() 0 7 2
A hasSession() 0 4 1
A eraseSession() 0 4 1
A filterName() 0 6 1
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\User as UserModel;
7
use Illuminate\Http\Request;
8
use Illuminate\Support\Facades\Config;
9
10
/**
11
 * Class User.
12
 */
13
class User
14
{
15
    /**
16
     * Create and return a User instance.
17
     *
18
     * @return User
19
     */
20
    public static function getInstance()
21
    {
22
        static $instance = null;
23
24
        if ($instance === null) {
25
            $instance = new static();
26
        }
27
28
        return $instance;
29
    }
30
31
    /**
32
     * Update User Data without overwriting Session.
33
     *
34
     * @param array $parameters
35
     *
36
     * @return UserModel
37
     */
38
    public function updateSession(array $parameters)
39
    {
40
        return $this->setSession($this->updateData($this->getUser(), $parameters));
0 ignored issues
show
Bug introduced by
It seems like $this->getUser() can be null; however, updateData() 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...
41
    }
42
43
    /**
44
     * Set User Data on Session.
45
     *
46
     * @param UserModel $user
47
     *
48
     * @return UserModel
49
     */
50
    public function setSession(UserModel $user)
51
    {
52
        return Session::set(Config::get('chocolatey.security.session'), $user);
53
    }
54
55
    /**
56
     * Update User Data by User Model.
57
     *
58
     * @param UserModel $user
59
     * @param array $parameters
60
     *
61
     * @return UserModel
62
     */
63
    public function updateData(UserModel $user, array $parameters)
64
    {
65
        $user->update($parameters);
66
67
        return $user;
68
    }
69
70
    /**
71
     * Get User Data from Session
72
     * If User Session doesn't exists, return null.
73
     *
74
     * @return UserModel|null
75
     */
76
    public function getUser()
77
    {
78
        return Session::get(Config::get('chocolatey.security.session')) ?? null;
79
    }
80
81
    /**
82
     * Set Session From Login Credentials.
83
     *
84
     * @param Request $request
85
     *
86
     * @return UserModel
87
     */
88
    public function loginUser(Request $request)
89
    {
90
        $user = UserModel::where('mail', $request->json()->get('email'))->where('password',
91
            hash(Config::get('chocolatey.security.hash'), $request->json()->get('password')))->first();
92
93
        return $user != null ? $this->setSession($user) : null;
94
    }
95
96
    /**
97
     * Return if USer Session Exists.
98
     *
99
     * @return bool
100
     */
101
    public function hasSession()
102
    {
103
        return (bool)Session::get(Config::get('chocolatey.security.session'));
104
    }
105
106
    /**
107
     * Erase User Session.
108
     */
109
    public function eraseSession()
110
    {
111
        Session::erase(Config::get('chocolatey.security.session'));
112
    }
113
114
    /**
115
     * Filter an Username from the Invalid Names Base.
116
     *
117
     * @param string $userName
118
     *
119
     * @return bool
120
     */
121
    public function filterName(string $userName): bool
122
    {
123
        return count(array_filter(Config::get('chocolatey.invalid'), function ($username) use ($userName) {
124
                return stripos($userName, $username) !== false;
125
            })) == 0;
126
    }
127
}
128