Completed
Push — master ( fb9ef3...273ac9 )
by claudio
13:46 queued 04:33
created

CalendarsController::destroy()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 0
loc 8
ccs 5
cts 5
cp 1
rs 9.4286
cc 1
eloc 5
nc 1
nop 1
crap 1
1
<?php
2
3
namespace plunner\Http\Controllers\Employees\Calendars;
4
5
use Illuminate\Http\Request;
6
use it\thecsea\caldav_client_adapter\simple_caldav_client\SimpleCaldavAdapter;
7
use plunner\Calendar;
8
use plunner\Http\Controllers\Controller;
9
use plunner\Http\Requests\Employees\CalendarRequest;
10
11
12
class CalendarsController extends Controller
13
{
14
    /**
15
     * ExampleController constructor.
16
     */
17 14
    public function __construct()
18
    {
19 14
        config(['auth.model' => \plunner\Employee::class]);
20 14
        config(['jwt.user' => \plunner\Employee::class]);
21 14
        $this->middleware('jwt.authandrefresh:mode-en');
22 14
    }
23
24
25
    /**
26
     * Display a listing of the resource.
27
     *
28
     * @return \Illuminate\Http\Response
29
     */
30 2
    public function index()
31
    {
32
        //
33
        /**
34
         * @var $employee Employee
35
         */
36 2
        $employee = \Auth::user();
37 2
        return $employee->calendars;
38
    }
39
40
    /**
41
     * Store a newly created resource in storage.
42
     *
43
     * @param  CalendarRequest  $request
44
     * @return \Illuminate\Http\Response
45
     */
46 2
    public function store(CalendarRequest $request)
47
    {
48
        //
49 2
        $employee = \Auth::user();
50 2
        $input = $request->all();
51 2
        $calendar = $employee->calendars()->create($input);
52 2
        return $calendar;
53
    }
54
55
    /**
56
     * Display the specified resource.
57
     *
58
     * @param  int  $id
59
     * @return \Illuminate\Http\Response
60
     */
61 6
    public function show($id)
62
    {
63
        //
64 6
        $calendar = Calendar::findOrFail($id);
65 6
        $this->authorize($calendar);
66 4
        return $calendar;
67
    }
68
69
    /**
70
     * Update the specified resource in storage.
71
     *
72
     * @param  CalendarRequest  $request
73
     * @param  int  $id
74
     * @return \Illuminate\Http\Response
75
     */
76 2
    public function update(CalendarRequest $request, $id)
77
    {
78
        //
79 2
        $calendar = Calendar::findOrFail($id);
80 2
        $this->authorize($calendar);
81 2
        $input = $request->all();
82 2
        $calendar->update($input);
83 2
        return $calendar;
84
    }
85
86
    /**
87
     * Remove the specified resource from storage.
88
     *
89
     * @param  int  $id
90
     * @return \Illuminate\Http\Response
91
     */
92 2
    public function destroy($id)
93
    {
94
        //
95 2
        $calendar = Calendar::findOrFail($id);
96 2
        $this->authorize($calendar);
97 2
        $calendar->delete();
98 2
        return $calendar;
99
    }
100
101
    /**
102
     * Return a list of calendars name of a specif caldav calendar
103
     * @param Request $request
104
     * @return \Illuminate\Http\Response
105
     */
106
    public function getCalendars(Request $request)
107
    {
108
        //TODO VALIDATE
109
        //TODO test this
110
        try {
111
            $caldavClient = new SimpleCaldavAdapter();
112
            $caldavClient->connect($request->get('url'), $request->get('username'), $request->get('password'));
113
            $calendars = $caldavClient->findCalendars();
114
            return array_keys($calendars);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return array_keys($calendars); (integer[]) is incompatible with the return type documented by plunner\Http\Controllers...ontroller::getCalendars of type Illuminate\Http\Response.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
115
        }catch (\it\thecsea\caldav_client_adapter\CaldavException $e)
116
        {
117
            return Response::json(['error' => $e->getMessage()],422);
118
        }
119
    }
120
}
121