DashboardController::update()   A
last analyzed

Complexity

Conditions 3
Paths 2

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 2
nop 1
1
<?php
2
3
namespace App\Http\Controllers;
4
5
use App\Repositories\UserRepository;
6
use App\Repositories\LotRepository;
7
use App\Repositories\ProfileRepository;
8
use App\Repositories\InvolvedRepository;
9
use Carbon\Carbon;
10
use Illuminate\Contracts\Auth\Guard;
11
use App\Http\Requests\UpdateUserSettings;
12
use App\Http\Requests\UpdateUserPassword;
13
use App\Services\ImageProcessor;
14
use App\Video;
15
use Illuminate\Http\UploadedFile;
16
17
18
class DashboardController extends Controller
19
{
20
    /**
21
     * @var UserRepository
22
     */
23
    protected $users;
24
25
    /**
26
     * @var  ProfileRepository
27
     */
28
    protected $profile;
29
30
    /**
31
     * @var Guard
32
     */
33
    private $auth;
34
35
    private $lots;
36
37
    private $involved;
38
39
    /**
40
     * DashboardController constructor.
41
     * @param UserRepository $userRepository
42
     * @param Guard $auth
43
     */
44
    public function __construct(UserRepository $userRepository,
45
                                Guard $auth,
46
                                ProfileRepository $profileRepository,
47
                                LotRepository $lotRepository,
48
                                InvolvedRepository $involvedRepository
49
    )
50
    {
51
        $this->users = $userRepository;
52
        $this->profile = $profileRepository;
53
        $this->auth = $auth;
54
        $this->lots = $lotRepository;
55
        $this->involved = $involvedRepository;
56
    }
57
58
    public function howWork()
59
    {
60
61
        $video = Video::orderBy('id', 'desc')->get();
62
63
        return view('dashboard.how-amma-work', compact('video'));
64
    }
65
66
    /**
67
     * My vendors.
68
     *
69
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
70
     */
71
    public function myVendors()
72
    {
73
        $vendors = $this->auth->user()->vendors;
0 ignored issues
show
Bug introduced by
Accessing vendors on the interface Illuminate\Contracts\Auth\Authenticatable 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...
74
75
        return view('dashboard.my-vendors', compact('vendors'));
76
    }
77
78
    /**
79
     * My products.
80
     *
81
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
82
     */
83
    public function myProducts()
84
    {
85
        $vendors = $this->auth->user()->vendors;
0 ignored issues
show
Bug introduced by
Accessing vendors on the interface Illuminate\Contracts\Auth\Authenticatable 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...
86
87
        return view('dashboard.my-products', compact('vendors'));
88
    }
89
90
    /**
91
     * My products.
92
     *
93
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
94
     */
95
    public function myInvolved()
96
    {
97
        $involved = $this->auth->user()->involved()->active()->get();
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Illuminate\Contracts\Auth\Authenticatable as the method involved() does only exist in the following implementations of said interface: App\User.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
98
99
        $product = $this->sortInvolvedProducts($involved);
100
101
        return view('dashboard.my-involved', compact('product'));
102
    }
103
104
    public function sortInvolvedProducts($involved) {
105
106
        if (count($involved)) {
107
            foreach ($involved as $item) {
108
                if ($item->lot->verify_status == 'verified') {
109
                    $product[] = ['date' =>$item->lot->public_date, 'product' => $item->product, 'involved' => $item];
0 ignored issues
show
Coding Style Comprehensibility introduced by
$product was never initialized. Although not strictly required by PHP, it is generally a good practice to add $product = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
110
                }else {
111
                    $product[] = ['date' =>date('dmy',strtotime('9999999')), 'product' => $item->product, 'involved' => $item];
0 ignored issues
show
Bug introduced by
The variable $product does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
112
                }
113
            }
114
            usort($product, function ($product, $b) {
115
                return date('dmy',strtotime($b['date'])) - date('dmy',strtotime($product['date']));
116
            });
117
118
            return $product;
119
        }
120
    }
121
122
    /**
123
     * Account and password settings.
124
     *
125
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
126
     */
127
    public function accountSettings()
128
    {
129
        return view('dashboard.account-settings');
130
    }
131
132
    public function userPassword()
133
    {
134
        return view('dashboard.user-password');
135
    }
136
137
    /**
138
     * @param UpdateUserSettings $request
139
     * @return mixed
140
     */
141
    public function update(UpdateUserSettings $request)
142
    {
143
        $this->users->update_user($request->all());
144
145
        $image = $request->file('photo');
146
        if ($image && $image instanceof UploadedFile) {
147
            (new ImageProcessor())->changeAvatar($image);
148
        }
149
150
        return back()->withStatus('Setarile au fost modificate!')->withColor('green')->with('activeclass', 'update_settings');
0 ignored issues
show
Bug introduced by
The method withStatus() does not exist on Illuminate\Http\RedirectResponse. Did you maybe mean status()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
151
    }
152
153
    /**
154
     * @param UpdateUserPassword $request
155
     * @return mixed
156
     */
157
    public function updatePassword(UpdateUserPassword $request)
158
    {
159
        $this->users->updatePassword($request->password);
0 ignored issues
show
Documentation introduced by
The property password does not exist on object<App\Http\Requests\UpdateUserPassword>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
160
161
        return back()->withStatus('Password Updated!')->with('activeclass', 'update_password');
0 ignored issues
show
Bug introduced by
The method withStatus() does not exist on Illuminate\Http\RedirectResponse. Did you maybe mean status()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
162
    }
163
}