Passed
Push — master ( 805c58...4a8ad5 )
by Nikita
10:14 queued 04:50
created

ProfileController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 3
c 2
b 0
f 0
dl 0
loc 9
rs 10
cc 1
nc 1
nop 2
1
<?php
2
3
namespace Gameap\Http\Controllers\API;
4
5
use Gameap\Http\Controllers\AuthController;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Gameap\Http\Controllers\API\AuthController. Consider defining an alias.

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 Gameap\Http\Requests\API\UpdateProfileRequest;
7
use Gameap\Models\User;
8
use Illuminate\Contracts\Auth\Factory as AuthFactory;
9
use Illuminate\Contracts\Hashing\Hasher;
10
11
class ProfileController extends AuthController
12
{
13
    /** @var AuthFactory */
14
    protected $authFactory;
15
16
    protected $hasher;
17
18
    public function __construct(
19
        AuthFactory $auth,
20
        Hasher $hasher
21
    )
22
    {
23
        parent::__construct();
24
25
        $this->authFactory = $auth;
26
        $this->hasher = $hasher;
27
    }
28
29
    public function show()
30
    {
31
        /** @var User $user */
32
        $user = $this->authFactory->guard()->user();
33
34
        return response()->json([
35
            'id' => $user->id,
36
            'login' => $user->login,
37
            'name' => $user->name,
38
            'email' => $user->email,
39
            'roles' => $user->roles->pluck('name'),
0 ignored issues
show
Bug introduced by
The property roles does not seem to exist on Gameap\Models\User. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
40
        ]);
41
    }
42
43
    public function save(UpdateProfileRequest $request)
44
    {
45
        /** @var User $user */
46
        $user = $this->authFactory->guard()->user();
47
48
        if ($request->has('current_password')) {
49
            if (!($this->hasher->check($request->get('current_password'), $user->password))) {
50
                return response()->json(['error' => __('profile.password_not_match_msg')], 400);
51
            }
52
53
            $user->update($request->only('password'));
54
        }
55
56
        if ($request->get('name') !== $user->name) {
57
            $user->update($request->only('name'));
58
        }
59
60
        return response()->json(['message' => 'success']);
61
    }
62
}