Completed
Push — master ( 0cf701...e9a102 )
by claudio
06:37 queued 03:46
created

CalendarsController   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 130
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 69.23%

Importance

Changes 9
Bugs 3 Features 1
Metric Value
wmc 9
c 9
b 3
f 1
lcom 1
cbo 7
dl 0
loc 130
ccs 27
cts 39
cp 0.6923
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A index() 0 9 1
A store() 0 7 1
A storeCaldav() 0 12 1
A show() 0 7 1
A update() 0 12 1
A destroy() 0 8 1
A getCalendars() 0 14 2
1
<?php
2
3
namespace plunner\Http\Controllers\Employees\Calendars;
4
5
use Illuminate\Support\Facades\Response;
6
use Illuminate\Http\Request; //TODO fix this
7
use it\thecsea\caldav_client_adapter\simple_caldav_client\SimpleCaldavAdapter;
8
use plunner\Calendar;
9
use plunner\Http\Controllers\Controller;
10
use plunner\Http\Requests\Employees\CalendarRequest;
11
12
13
class CalendarsController extends Controller
14
{
15
    /**
16
     * ExampleController constructor.
17
     */
18 14
    public function __construct()
19
    {
20 14
        config(['auth.model' => \plunner\Employee::class]);
21 14
        config(['jwt.user' => \plunner\Employee::class]);
22 14
        $this->middleware('jwt.authandrefresh:mode-en');
23 14
    }
24
25
26
    /**
27
     * Display a listing of the resource.
28
     *
29
     * @return \Illuminate\Http\Response
30
     */
31 2
    public function index()
32
    {
33
        //
34
        /**
35
         * @var $employee Employee
36
         */
37 2
        $employee = \Auth::user();
38 2
        return $employee->calendars;
39
    }
40
41
    /**
42
     * Store a newly created resource in storage.
43
     *
44
     * @param  CalendarRequest  $request
45
     * @return \Illuminate\Http\Response
46
     */
47 2
    public function store(CalendarRequest $request)
48
    {
49
        //
50 2
        $employee = \Auth::user();
51 2
        $calendar = $employee->calendars()->create($request->only('name', 'enabled'));
52 2
        return $calendar;
53
    }
54
55
    /**
56
     * Store a newly created resource in storage with caldav.
57
     *
58
     * @param  CalendarRequest  $request
59
     * @return \Illuminate\Http\Response
60
     */
61
    public function storeCaldav(CalendarRequest $request)
62
    {
63
        //
64
        $employee = \Auth::user();
65
        $calendar = $employee->calendars()->create($request->only('name', 'enabled'));
66
        $calendar->caldav()->create($request->only('url','username', 'password', 'calendar_name'));
67
        //TODO test
68
        //TODO validator
69
        //TODO return caldav info
70
        //TODO supprot function to create simple calendar
71
        return $calendar;
72
    }
73
74
    /**
75
     * Display the specified resource.
76
     *
77
     * @param  int  $id
78
     * @return \Illuminate\Http\Response
79
     */
80 6
    public function show($id)
81
    {
82
        //
83 6
        $calendar = Calendar::with('caldav')->findOrFail($id);
84 6
        $this->authorize($calendar);
85 4
        return $calendar;
86
    }
87
88
    /**
89
     * Update the specified resource in storage.
90
     *
91
     * @param  CalendarRequest  $request
92
     * @param  int  $id
93
     * @return \Illuminate\Http\Response
94
     */
95 2
    public function update(CalendarRequest $request, $id)
96
    {
97
        //
98 2
        $calendar = Calendar::findOrFail($id);
99 2
        $this->authorize($calendar);
100 2
        $calendar->update($request->only('name', 'enabled'));
101
        //TODO test
102
        //TODO validator
103
        //TODO check if caldav exists?
104 2
        $calendar->caldav()->update($request->only('url','username', 'password', 'calendar_name'));
105 2
        return $calendar;
106
    }
107
108
    /**
109
     * Remove the specified resource from storage.
110
     *
111
     * @param  int  $id
112
     * @return \Illuminate\Http\Response
113
     */
114 2
    public function destroy($id)
115
    {
116
        //
117 2
        $calendar = Calendar::findOrFail($id);
118 2
        $this->authorize($calendar);
119 2
        $calendar->delete();
120 2
        return $calendar;
121
    }
122
123
    /**
124
     * Return a list of calendars name of a specif caldav calendar
125
     * @param Request $request
126
     * @return \Illuminate\Http\Response
127
     */
128
    public function getCalendars(Request $request)
129
    {
130
        //TODO VALIDATE
131
        //TODO test this
132
        try {
133
            $caldavClient = new SimpleCaldavAdapter();
134
            $caldavClient->connect($request->input('url'), $request->input('username'), $request->input('password'));
0 ignored issues
show
Bug introduced by
It seems like $request->input('url') targeting Illuminate\Http\Request::input() can also be of type array; however, it\thecsea\caldav_client...aldavAdapter::connect() 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...
Bug introduced by
It seems like $request->input('username') targeting Illuminate\Http\Request::input() can also be of type array; however, it\thecsea\caldav_client...aldavAdapter::connect() 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...
Bug introduced by
It seems like $request->input('password') targeting Illuminate\Http\Request::input() can also be of type array; however, it\thecsea\caldav_client...aldavAdapter::connect() 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...
135
            $calendars = $caldavClient->findCalendars();
136
            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...
137
        }catch (\it\thecsea\caldav_client_adapter\CaldavException $e)
138
        {
139
            return Response::json(['error' => $e->getMessage()],422);
140
        }
141
    }
142
}
143