Completed
Push — development ( b7987d...8c0e57 )
by Claudio
05:33
created

User::updateData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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