EventsController::index()   A
last analyzed

Complexity

Conditions 5
Paths 8

Size

Total Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 26
rs 9.1928
c 0
b 0
f 0
cc 5
nc 8
nop 1
1
<?php
2
3
namespace Fabrica\Http\Api;
4
5
use Illuminate\Http\Request;
6
7
use Fabrica\Http\Requests;
8
use Fabrica\Http\Api\Controller;
9
use Fabrica\Customization\Eloquent\Events;
10
use Fabrica\Customization\Eloquent\EventNotifications;
11
use Fabrica\Project\Provider;
12
13
class EventsController extends Controller
14
{
15
    /**
16
     * Display a listing of the resource.
17
     *
18
     * @return \Illuminate\Http\Response
19
     */
20
    public function index($project_key)
21
    {
22
        $events = Provider::getEventList($project_key);
23
        foreach ($events as $key => $event)
24
        {
25
            $event->notifications = $this->getNotifications($project_key, $event['_id']);
26
        }
27
28
        $roles = Provider::getRoleList($project_key, ['name']);
29
        $users = Provider::getUserList($project_key);
30
31
        $single_user_fields = [];
32
        $multi_user_fields = [];
33
        $fields = Provider::getFieldList($project_key);
34
        foreach ($fields as $field)
35
        {
36
            if ($field->type === 'SingleUser') {
37
                $single_user_fields[] = [ 'id' => $field->key, 'name' => $field->name ];
38
            } 
39
            else if ($field->type === 'MultiUser') {
40
                $multi_user_fields[] = [ 'id' => $field->key, 'name' => $field->name ];
41
            } 
42
        }
43
44
        return response()->json(['ecode' => 0, 'data' => $events, 'options' => [ 'roles' => $roles, 'users' => $users, 'single_user_fields' => $single_user_fields, 'multi_user_fields' => $multi_user_fields ]]);
0 ignored issues
show
Bug introduced by
The method json does only exist in Illuminate\Contracts\Routing\ResponseFactory, but not in Illuminate\Http\Response.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
45
    }
46
47
    /**
48
     * Store a newly created resource in storage.
49
     *
50
     * @param  \Illuminate\Http\Request $request
51
     * @return \Illuminate\Http\Response
52
     */
53 View Code Duplication
    public function store(Request $request, $project_key)
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...
54
    {
55
        $name = $request->input('name');
56
        if (!$name) {
57
            throw new \UnexpectedValueException('the name can not be empty.', -12800);
58
        }
59
60
        if (Provider::isEventExisted($project_key, $name)) {
61
            throw new \UnexpectedValueException('event name cannot be repeated', -12801);
62
        }
63
64
        $event = Events::create([ 'project_key' => $project_key, 'apply' => 'workflow' ] + $request->all());
65
        return response()->json(['ecode' => 0, 'data' => $event]);
0 ignored issues
show
Bug introduced by
The method json does only exist in Illuminate\Contracts\Routing\ResponseFactory, but not in Illuminate\Http\Response.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
66
    }
67
68
    /**
69
     * Display the specified resource.
70
     *
71
     * @param  int $id
72
     * @return \Illuminate\Http\Response
73
     */
74
    public function show($project_key, $id)
75
    {
76
        $event = Events::find($id);
77
        return response()->json(['ecode' => 0, 'data' => $event]);
0 ignored issues
show
Bug introduced by
The method json does only exist in Illuminate\Contracts\Routing\ResponseFactory, but not in Illuminate\Http\Response.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
78
    }
79
80
    /**
81
     * Update the specified resource in storage.
82
     *
83
     * @param  \Illuminate\Http\Request $request
84
     * @param  int                      $id
85
     * @return \Illuminate\Http\Response
86
     */
87 View Code Duplication
    public function update(Request $request, $project_key, $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...
88
    {
89
        $event = Events::find($id);
90
        if (!$event || $project_key != $event->project_key) {
91
            throw new \UnexpectedValueException('the event does not exist or is not in the project.', -12802);
92
        }
93
94
        $name = $request->input('name');
95
        if (isset($name)) {
96
            if (!$name) {
97
                throw new \UnexpectedValueException('the name can not be empty.', -12800);
98
            }
99
            if ($event->name !== $name && Provider::isEventExisted($project_key, $name)) {
100
                throw new \UnexpectedValueException('event name cannot be repeated', -12801);
101
            }
102
        }
103
        $event->fill($request->except(['project_key']))->save();
104
105
        $event = Events::find($id);
106
        $event->notifications = $this->getNotifications($project_key, $id);
107
108
        return response()->json(['ecode' => 0, 'data' => $event]);
0 ignored issues
show
Bug introduced by
The method json does only exist in Illuminate\Contracts\Routing\ResponseFactory, but not in Illuminate\Http\Response.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
109
    }
110
111
    /**
112
     * Remove the specified resource from storage.
113
     *
114
     * @param  int $id
115
     * @return \Illuminate\Http\Response
116
     */
117
    public function destroy($project_key, $id)
118
    {
119
        $event = Events::find($id);
120
        if (!$event || $project_key != $event->project_key) {
121
            throw new \UnexpectedValueException('the event does not exist or is not in the project.', -12802);
122
        }
123
124
        $en = EventNotifications::where([ 'project_key' => $project_key, 'event_id' => $id ])->first();
125
        $en && $en->delete();
126
127
        Events::destroy($id);
128
        return response()->json(['ecode' => 0, 'data' => ['id' => $id]]);
0 ignored issues
show
Bug introduced by
The method json does only exist in Illuminate\Contracts\Routing\ResponseFactory, but not in Illuminate\Http\Response.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
129
    }
130
131
    /**
132
     * set notifications
133
     *
134
     * @param  string $project_key
135
     * @param  string $event_id
0 ignored issues
show
Bug introduced by
There is no parameter named $event_id. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
136
     * @return array
137
     */
138
    public function setNotify(Request $request, $project_key, $id)
139
    {
140
        $event = Events::find($id);
141 View Code Duplication
        if (!$event || ($event->project_key != '$_sys_$' && $project_key != $event->project_key)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
142
            throw new \UnexpectedValueException('the event does not exist or is not in the project.', -12802);
143
        }
144
145
        $notifications = $request->input('notifications');
146
        if (isset($notifications)) {
147
            $en = EventNotifications::where([ 'project_key' => $project_key, 'event_id' => $id ])->first();
148
            $en && $en->delete();
149
150
            EventNotifications::create([ 'project_key' => $project_key, 'event_id' => $id, 'notifications' => $notifications ]);
151
        }
152
        $event->notifications = $this->getNotifications($project_key, $id);
153
154
        return response()->json(['ecode' => 0, 'data' => $event]);
0 ignored issues
show
Bug introduced by
The method json does only exist in Illuminate\Contracts\Routing\ResponseFactory, but not in Illuminate\Http\Response.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
155
    }
156
157
    /**
158
     * get notifications by project_key and eventid. 
159
     *
160
     * @param  string $project_key
161
     * @param  string $event_id
162
     * @return array 
163
     */
164 View Code Duplication
    public function getNotifications($project_key, $event_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...
165
    {
166
        $en = EventNotifications::where([ 'project_key' => $project_key, 'event_id' => $event_id ])->first();
167
        if (!$en && $project_key !== '$_sys_$') {
168
            $en = EventNotifications::where([ 'project_key' => '$_sys_$', 'event_id' => $event_id ])->first();
169
        }
170
        return $en && isset($en->notifications) ? $en->notifications : [];
171
    }
172
173
    /**
174
     * reset the notification.
175
     *
176
     * @param  string $project_key
177
     * @param  string $event_id
178
     * @return \Illuminate\Http\Response
179
     */
180 View Code Duplication
    public function reset($project_key, $event_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...
181
    {
182
        $en = EventNotifications::where([ 'project_key' => $project_key, 'event_id' => $event_id ])->first();
183
        $en && $en->delete();
184
185
        $event = Events::find($event_id)->toArray();
186
187
        $en = EventNotifications::where([ 'project_key' => '$_sys_$', 'event_id' => $event_id ])->first();
188
        $event['notifications'] = $en && isset($en->notifications) ? $en->notifications : [];
189
190
        return response()->json(['ecode' => 0, 'data' => $event]);
0 ignored issues
show
Bug introduced by
The method json does only exist in Illuminate\Contracts\Routing\ResponseFactory, but not in Illuminate\Http\Response.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
191
    }
192
}
193