ProfileController::transNotification()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 0
cts 8
cp 0
rs 9.9332
c 0
b 0
f 0
cc 1
nc 1
nop 3
crap 2
1
<?php namespace Arcanesoft\Auth\Http\Controllers\Admin;
2
3
use Arcanesoft\Auth\Http\Requests\Admin\Profile\UpdatePasswordRequest;
4
use Arcanesoft\Contracts\Auth\Models\User;
5
use Arcanesoft\Core\Http\Controllers\AdminController;
6
use Arcanesoft\Core\Traits\Notifyable;
7
use Log;
8
9
/**
10
 * Class     ProfileController
11
 *
12
 * @package  Arcanesoft\Auth\Http\Controllers\Admin
13
 * @author   ARCANEDEV <[email protected]>
14
 *
15
 * @todo: Adding the authorization checks
16
 */
17
class ProfileController extends AdminController
18
{
19
    /* -----------------------------------------------------------------
20
     |  Traits
21
     | -----------------------------------------------------------------
22
     */
23
24
    use Notifyable;
25
26
    /* -----------------------------------------------------------------
27
     |  Properties
28
     | -----------------------------------------------------------------
29
     */
30
31
    /**
32
     * The authenticated user.
33
     *
34
     * @var \Arcanesoft\Contracts\Auth\Models\User
35
     */
36
    protected $user;
37
38
    /**
39
     * The view namespace.
40
     *
41
     * @var string
42
     */
43
    protected $viewNamespace = 'auth';
44
45
    /* -----------------------------------------------------------------
46
     |  Constructor
47
     | -----------------------------------------------------------------
48
     */
49
50
    /**
51
     * ProfileController constructor.
52
     */
53
    public function __construct()
54
    {
55
        parent::__construct();
56
57
        $this->addBreadcrumbRoute('Profile', 'admin::auth.profile.index');
58
        $this->setCurrentPage('auth-profile');
59
    }
60
61
    /* -----------------------------------------------------------------
62
     |  Main Methods
63
     | -----------------------------------------------------------------
64
     */
65
66
    public function index()
67
    {
68
        $user = $this->getAuthenticatedUser();
69
70
        $this->setTitle("Profile - {$user->full_name}");
0 ignored issues
show
Bug introduced by
Accessing full_name on the interface Arcanesoft\Contracts\Auth\Models\User suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
71
        $this->addBreadcrumbRoute($user->full_name, 'admin::auth.profile.index');
0 ignored issues
show
Bug introduced by
Accessing full_name on the interface Arcanesoft\Contracts\Auth\Models\User suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
72
73
        return $this->view('admin.profile.index', compact('user'));
74
    }
75
76
    public function edit()
77
    {
78
        // TODO: complete the implementation
79
    }
80
81
    public function update()
82
    {
83
        // TODO: complete the implementation
84
    }
85
86
    public function updatePassword(UpdatePasswordRequest $request, User $user)
87
    {
88
        $user->update($request->getValidatedData());
89
90
        $this->transNotification('password-updated', [], $user->toArray());
91
92
        return redirect()->route('admin::auth.profile.index');
93
    }
94
95
    /* -----------------------------------------------------------------
96
     |  Other methods
97
     | -----------------------------------------------------------------
98
     */
99
100
    /**
101
     * Get the authenticated user.
102
     *
103
     * @return \Arcanesoft\Contracts\Auth\Models\User
104
     */
105
    private function getAuthenticatedUser()
106
    {
107
        return auth()->user();
0 ignored issues
show
Bug introduced by
The method user does only exist in Illuminate\Contracts\Auth\Guard, but not in Illuminate\Contracts\Auth\Factory.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
108
    }
109
110
    /**
111
     * Notify with translation.
112
     *
113
     * @param  string  $action
114
     * @param  array   $replace
115
     * @param  array   $context
116
     *
117
     * @return string
118
     */
119
    protected function transNotification($action, array $replace = [], array $context = [])
120
    {
121
        $title   = trans("auth::profile.messages.{$action}.title");
122
        $message = trans("auth::profile.messages.{$action}.message", $replace);
123
124
        Log::info($message, $context);
125
        $this->notifySuccess($message, $title);
126
127
        return $message;
128
    }
129
}
130