DJController::index()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
rs 9.4286
cc 1
eloc 3
nc 1
nop 0
1
<?php 
2
3
namespace WITR\Http\Controllers\Admin;
4
5
use WITR\Http\Requests\Admin\DJ as Requests;
6
use WITR\Http\Controllers\Controller;
7
use WITR\DJ;
8
use WITR\TimeSlot;
9
use Input;
10
use File;
11
use Hash;
12
13
use Illuminate\Http\Request;
14
use Illuminate\Contracts\Auth\PasswordBroker;
15
16
class DJController extends Controller {
17
18
	public function __construct()
19
	{
20
		$this->middleware('auth');
21
		$this->middleware('admin');
22
	}
23
24
	/**
25
	 * Display a listing of the djs.
26
	 *
27
	 * @return Response
28
	 */
29
	public function index()
30
	{
31
		$djs = DJ::orderBy('name', 'asc')->get();
32
		return view('admin.djs.index', ['djs' => $djs]);
33
	}
34
35
	public function create()
36
	{
37
		return view('admin.djs.create'); 
38
	}
39
40
	/**
41
	 * Show the form for creating a new resource.
42
	 *
43
	 * @return Response
44
	 */
45 View Code Duplication
	public function store(Requests\CreateRequest $request)
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...
46
	{
47
		$dj = new DJ($request->all());
48
49
		if($request->hasFile('picture'))
50
		{
51
			$file = $request->file('picture');
52
			$dj->uploadFile('picture', $file);
53
		}
54
		else
55
		{
56
			$dj->picture = 'default.jpg';
0 ignored issues
show
Documentation introduced by
The property picture does not exist on object<WITR\DJ>. 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...
57
		}
58
59
		$dj->save();
60
61
		return redirect()->route('admin.djs.index')
62
			->with('success', 'DJ Created!');
63
	}
64
65
	/**
66
	 * Show the form for editing the specified resource.
67
	 *
68
	 * @param  int  $id
69
	 * @return Response
70
	 */
71
	public function edit($id)
72
	{
73
		$dj = DJ::findOrFail($id);
74
75
		return view('admin.djs.edit', ['dj' => $dj]);
76
	}
77
78
	/**
79
	 * Update the specified resource in storage.
80
	 *
81
	 * @param  int  $id
82
	 * @return Response
83
	 */
84 View Code Duplication
	public function update(Requests\UpdateRequest $request, $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...
85
	{
86
		$dj = DJ::findOrFail($id);
87
		$dj->fill($request->except(['picture']));
88
89
		if($request->hasFile('picture'))
90
		{
91
			$file = $request->file('picture');
92
			$dj->uploadFile('picture', $file);
93
		}
94
95
		$dj->save();
96
		return redirect()->route('admin.djs.index')
97
			->with('success', 'DJ Saved!');
98
	}
99
100
	/**
101
	 * Remove the specified resource from storage.
102
	 *
103
	 * @param  int  $id
104
	 * @return Response
105
	 */
106
	public function delete($id)
107
	{
108
		$timeslots = TimeSlot::where('dj', $id)->count();
109
		if ($timeslots > 0) {
110
			return redirect()->back()->with('error', 'Remove DJ from schedule before deleting.');
111
		}
112
113
		$dj = DJ::findOrFail($id);
114
		File::delete(public_path().'/img/djs/'.$dj->picture);
115
		DJ::destroy($id);
116
		return redirect()->route('admin.djs.index')
117
			->with('success', 'DJ Deleted!');
118
	}
119
120
}
121