EventController   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 83
Duplicated Lines 9.64 %

Coupling/Cohesion

Components 0
Dependencies 8

Importance

Changes 8
Bugs 1 Features 3
Metric Value
wmc 8
c 8
b 1
f 3
lcom 0
cbo 8
dl 8
loc 83
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A index() 0 5 1
A new_event() 0 4 1
A create() 0 12 1
A edit() 0 6 1
A update() 0 18 2
A delete() 8 8 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php namespace WITR\Http\Controllers\Admin;
2
3
use WITR\Http\Requests\Admin\Event as Requests;
4
use WITR\Http\Controllers\Controller;
5
use WITR\Event;
6
use Carbon\Carbon;
7
use Input;
8
use File;
9
10
use Illuminate\Http\Request;
11
12
class EventController extends Controller {
13
14
	public function __construct()
15
	{
16
		$this->middleware('auth');
17
		$this->middleware('editor');
18
	}
19
	
20
	/**
21
	 * Display a listing of the Events.
22
	 *
23
	 * @return Response
24
	 */
25
	public function index()
26
	{
27
		$events = Event::where('type', 'SLIDER')->orderBy('date', 'desc')->get();
28
		return view('admin.events.index', ['events' => $events]);
29
	}
30
31
	/**
32
	 * Display a listing of the resource.
33
	 *
34
	 * @return Response
35
	 */
36
	public function new_event()
37
	{
38
		return view('admin.events.create');
39
	}
40
41
	/**
42
	* Save the new Event
43
	*
44
	* @return Response
45
	*/
46
	public function create(Requests\CreateRequest $request)
47
	{
48
		$input = $request->all();
49
		$event = new Event($input);
50
		$event->date = Carbon::createFromFormat('m/d/Y', $input['date']);
0 ignored issues
show
Documentation introduced by
The property date does not exist on object<WITR\Event>. 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...
51
		$event->type = 'SLIDER';
0 ignored issues
show
Documentation introduced by
The property type does not exist on object<WITR\Event>. 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...
52
		$file = $request->file('picture');
53
		$event->uploadFile('picture', $file);
54
		$event->save();
55
		return redirect()->route('admin.events.index')
56
			->with('success', 'Event Saved!');
57
	}
58
59
	public function edit($id)
60
	{
61
		$event = Event::findOrFail($id);
62
63
		return view('admin.events.edit', ['event' => $event]);
64
	}
65
66
	public function update(Requests\UpdateRequest $request, $id)
67
	{
68
		$event = Event::findOrFail($id);
69
		$event->fill($request->except(['date', 'picture']));
70
71
		if ($request->hasFile('picture'))
72
		{
73
			$file = $request->file('picture');
74
			$event->uploadFile('picture', $file);
75
		}
76
77
		$date = Carbon::createFromFormat('m/d/Y', $request->input('date'));
0 ignored issues
show
Bug introduced by
It seems like $request->input('date') targeting Illuminate\Http\Request::input() can also be of type array; however, Carbon\Carbon::createFromFormat() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
78
		$event->date = $date;
79
80
		$event->save();
81
		return redirect()->route('admin.events.index')
82
			->with('success', 'Event Saved!');
83
	}
84
85 View Code Duplication
	public function delete($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...
86
	{
87
		$event = Event::findOrFail($id);
88
		File::delete(public_path().'/img/events/'.$event->picture);
89
		Event::destroy($id);
90
		return redirect()->route('admin.events.index')
91
			->with('success', 'Event Deleted!');
92
	}
93
94
}
95