AccountController::enableAccount()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.7666
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace App\Http\Controllers;
4
5
use Illuminate\Http\Request;
6
use App\Account;
7
use App\Group;
8
use App\Category;
9
use Auth;
10
11
class AccountController extends Controller
12
{
13
    use SearchTrait;
14
15
    /**
16
     * Create a new controller instance.
17
     *
18
     * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
19
     */
20
    public function __construct()
21
    {
22
        $this->middleware('auth');
23
    }
24
25
    /**
26
     * List existing accounts
27
     *
28
     * @return Response
29
     */
30
    public function showAccounts(Request $request, $group_id = null, $category_id = null)
31
    {
32
        $accounts = Account::orderBy('netlogin', 'asc');
33
        $group = null;
34
35
        // because request routing is not smart
36
        if ($request->is('accounts/category/*')) {
37
            $category_id = $group_id;
38
            $group_id = null;
39
        }
40
41
        // case when admin can only manager its own group§
42
        if (Auth::user()->level == 1) {
43
            $group_id = Auth::user()->group->id;
44
        }
45
46
        // if a group filter is set
47
        if (false === is_null($group_id)) {
48
            $group = Group::find($group_id);
49
            $accounts = $accounts->where('group_id', $group_id);
50
        }
51
52
        // if a category filter is set
53
        if (false === is_null($category_id)) {
54
            $accounts = $accounts->where('category_id', $category_id);
55
        }
56
57
        $results = $this->search($accounts, $request->input('type'), $request->input('search'));
58
59
        // store search criteria
60
        if (false === is_null($results)) {
61
            $request->session()->flash(
62
                'results',
63
                trans_choice(
64
                    'accounts.message.search',
65
                    $results,
66
                    ['number' => $results]
67
                )
68
            );
69
            $request->session()->flash('search', $request->input('search'));
70
            $request->session()->flash('type', $request->input('type'));
71
        }
72
73
        return view('account/accounts',
74
            [
75
                'accounts' => $accounts->paginate(20),
76
                'group' => $group,
77
                'categories' => Category::orderBy('name', 'asc')->get()
78
            ]
79
        );
80
    }
81
82
    /**
83
     * Edit the given account.
84
     *
85
     * @param  int  $id
86
     * @return Response
87
     */
88
    public function editAccount($id = null)
89
    {
90
        // if no ID then new account
91
        if (empty($id)) {
92
            $account = new Account;
93
        } else {
94
            $account = Account::findOrFail($id);
95
        }
96
        return view('account/account',
97
            [
98
                'account' => $account,
99
                'groups' => Group::orderBy('name', 'asc')->get(),
100
                'categories' => Category::orderBy('name', 'asc')->get()
101
            ]
102
        );
103
    }
104
105
    /**
106
     * Add a new account.
107
     *
108
     * @return Response
109
     */
110
    public function addAccount(Request $request)
111
    {
112
        $this->validate($request, [
113
            'firstname' => 'required|alpha_num|max:100',
114
            'lastname' => 'required|alpha_num|max:100',
115
            'netlogin' => 'required|unique:accounts,netlogin',
116
            'netpass' => 'required|min:8|different:netlogin',
117
            'expirydate' => 'required_if:status,1|date|after:today',
118
            'category' => 'required|exists:categories,id',
119
            'status' => 'required|boolean',
120
            'group' => 'required|exists:groups,id'
121
        ]);
122
123
        $account = new Account;
124
        $account->netlogin = $request->netlogin;
0 ignored issues
show
Documentation introduced by
The property netlogin does not exist on object<App\Account>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write 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.");
        }
    }

}

Since the property has write access only, you can use the @property-write 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...
125
        $account->netpass = Account::generateHash($request->netpass);
0 ignored issues
show
Documentation introduced by
The property netpass does not exist on object<App\Account>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write 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.");
        }
    }

}

Since the property has write access only, you can use the @property-write 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...
126
        $account->firstname = ucwords($request->firstname);
0 ignored issues
show
Documentation introduced by
The property firstname does not exist on object<App\Account>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write 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.");
        }
    }

}

Since the property has write access only, you can use the @property-write 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...
127
        $account->lastname = ucwords($request->lastname);
0 ignored issues
show
Documentation introduced by
The property lastname does not exist on object<App\Account>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write 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.");
        }
    }

}

Since the property has write access only, you can use the @property-write 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...
128
        $account->category_id = $request->category;
0 ignored issues
show
Documentation introduced by
The property category_id does not exist on object<App\Account>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write 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.");
        }
    }

}

Since the property has write access only, you can use the @property-write 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...
129
        if (empty($request->expirydate)) {
130
            $account->expire = date_create('+90 day')->format('Y-m-d');
0 ignored issues
show
Documentation introduced by
The property expire does not exist on object<App\Account>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write 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.");
        }
    }

}

Since the property has write access only, you can use the @property-write 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...
131
        } else {
132
            $account->expire = $request->expirydate;
0 ignored issues
show
Documentation introduced by
The property expire does not exist on object<App\Account>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write 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.");
        }
    }

}

Since the property has write access only, you can use the @property-write 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...
133
        }
134
        $account->status = $request->status;
0 ignored issues
show
Documentation introduced by
The property status does not exist on object<App\Account>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write 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.");
        }
    }

}

Since the property has write access only, you can use the @property-write 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...
135
        $account->group_id = $request->group;
0 ignored issues
show
Documentation introduced by
The property group_id does not exist on object<App\Account>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write 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.");
        }
    }

}

Since the property has write access only, you can use the @property-write 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...
136
        $account->created_by = $request->user()->id;
0 ignored issues
show
Documentation introduced by
The property created_by does not exist on object<App\Account>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write 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.");
        }
    }

}

Since the property has write access only, you can use the @property-write 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...
137
        $account->save();
138
        return redirect('accounts')->with(
139
            'status',
140
            trans('accounts.message.add', ['account' => $account->netlogin])
0 ignored issues
show
Documentation introduced by
The property netlogin does not exist on object<App\Account>. 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...
141
        );
142
    }
143
144
    /**
145
     * Update an account.
146
     *
147
     * @param  Request  $request
148
     * @param  int  $id
149
     * @return Response
150
     */
151
    public function updateAccount(Request $request, $id)
152
    {
153
        $this->validate($request, [
154
            'netlogin' => 'required|exists:accounts,netlogin',
155
            'expirydate' => 'required_if:status,1|date|after:today',
156
            'category' => 'required|exists:categories,id',
157
            'status' => 'required|boolean',
158
            'group' => 'required|exists:groups,id'
159
        ]);
160
161
        $account = Account::findOrFail($id);
162
        if (false === empty($request->netpass)) {
163
            $account->netpass = Account::generateHash($request->netpass);
164
        }
165
        if (false === empty($request->expirydate)) {
166
            $account->expire = $request->expirydate;
167
        }
168
        $account->category_id = $request->category;
169
        $account->status = $request->status;
170
        $account->group_id = $request->group;
171
        $account->update();
172
        return redirect('accounts')->with(
173
            'status',
174
            trans('accounts.message.update', ['account' => $account->netlogin])
175
        );
176
    }
177
178
    /**
179
     * Enable an account.
180
     *
181
     * @param  int  $id
182
     * @return Response
183
     */
184
    public function enableAccount($id)
185
    {
186
        $account = Account::findOrFail($id);
187
        $account->enable();
188
        return redirect()->back()->with(
189
            'status',
190
            trans(
191
                'accounts.message.enable',
192
                [
193
                    'account' => $account->netlogin,
194
                    'date'    => $account->expire
195
                ]
196
            )
197
        );
198
    }
199
200
    /**
201
     * Disable an account.
202
     *
203
     * @param  int  $id
204
     * @return Response
205
     */
206 View Code Duplication
    public function disableAccount($id)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
207
    {
208
        $account = Account::findOrFail($id);
209
        $account->disable();
210
        return redirect()->back()->with(
211
            'status',
212
            trans('accounts.message.disable', ['account' => $account->netlogin])
213
        );
214
    }
215
216
    /**
217
     * Remove an account.
218
     *
219
     * @param  int  $id
220
     * @return Response
221
     */
222 View Code Duplication
    public function removeAccount($id)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
223
    {
224
        $account = Account::findOrFail($id);
225
        $netlogin = $account->netlogin;
226
        $account->delete();
227
        return redirect()->back()->with(
228
            'status',
229
            trans('accounts.message.delete', ['account' => $netlogin])
230
        );
231
    }
232
}
233