Completed
Push — master ( cbcc67...0cf701 )
by claudio
05:06
created

CalendarsController   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 132
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 69.05%

Importance

Changes 8
Bugs 3 Features 1
Metric Value
wmc 9
c 8
b 3
f 1
lcom 1
cbo 7
dl 0
loc 132
ccs 29
cts 42
cp 0.6905
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A index() 0 9 1
A store() 0 8 1
A storeCaldav() 0 13 1
A show() 0 7 1
A destroy() 0 8 1
A getCalendars() 0 14 2
A update() 0 12 1
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
        $input = $request->all();
52 2
        $calendar = $employee->calendars()->create($input);
53 2
        return $calendar;
54
    }
55
56
    /**
57
     * Store a newly created resource in storage with caldav.
58
     *
59
     * @param  CalendarRequest  $request
60
     * @return \Illuminate\Http\Response
61
     */
62
    public function storeCaldav(CalendarRequest $request)
63
    {
64
        //
65
        $employee = \Auth::user();
66
        $input = $request->all();
67
        $calendar = $employee->calendars()->create($input);
68
        $calendar->caldav()->create($input);
69
        //TODO test
70
        //TODO validator
71
        //TODO return caldav info
72
        //TODO supprot function to create simple calendar
73
        return $calendar;
74
    }
75
76
    /**
77
     * Display the specified resource.
78
     *
79
     * @param  int  $id
80
     * @return \Illuminate\Http\Response
81
     */
82 6
    public function show($id)
83
    {
84
        //
85 6
        $calendar = Calendar::with('caldav')->findOrFail($id);
86 6
        $this->authorize($calendar);
87 4
        return $calendar;
88
    }
89
90
    /**
91
     * Update the specified resource in storage.
92
     *
93
     * @param  CalendarRequest  $request
94
     * @param  int  $id
95
     * @return \Illuminate\Http\Response
96
     */
97 2
    public function update(CalendarRequest $request, $id)
98
    {
99
        //
100 2
        $calendar = Calendar::findOrFail($id);
101 2
        $this->authorize($calendar);
102 2
        $input = $request->all();
0 ignored issues
show
Unused Code introduced by
$input is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
103 2
        $calendar->update($request->only('name', 'enabled'));
104
        //TODO test
105
        //TODO validator
106 2
        $calendar->caldav()->update($request->only('url','username', 'password', 'calendar_name'));
107 2
        return $calendar;
108
    }
109
110
    /**
111
     * Remove the specified resource from storage.
112
     *
113
     * @param  int  $id
114
     * @return \Illuminate\Http\Response
115
     */
116 2
    public function destroy($id)
117
    {
118
        //
119 2
        $calendar = Calendar::findOrFail($id);
120 2
        $this->authorize($calendar);
121 2
        $calendar->delete();
122 2
        return $calendar;
123
    }
124
125
    /**
126
     * Return a list of calendars name of a specif caldav calendar
127
     * @param Request $request
128
     * @return \Illuminate\Http\Response
129
     */
130
    public function getCalendars(Request $request)
131
    {
132
        //TODO VALIDATE
133
        //TODO test this
134
        try {
135
            $caldavClient = new SimpleCaldavAdapter();
136
            $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...
137
            $calendars = $caldavClient->findCalendars();
138
            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...
139
        }catch (\it\thecsea\caldav_client_adapter\CaldavException $e)
140
        {
141
            return Response::json(['error' => $e->getMessage()],422);
142
        }
143
    }
144
}
145