Completed
Push — master ( 24a4dc...fe3ba7 )
by Ricardo
04:03
created

ModuleController::handle()   A

Complexity

Conditions 6
Paths 4

Size

Total Lines 19

Duplication

Lines 19
Ratio 100 %

Importance

Changes 0
Metric Value
dl 19
loc 19
rs 9.0111
c 0
b 0
f 0
cc 6
nc 4
nop 2
1
<?php
2
3
namespace Fabrica\Http\Api;
4
5
use Illuminate\Http\Request;
6
use Illuminate\Support\Facades\Event;
7
8
//use Fabrica\Events\ModuleEvent;
9
use Fabrica\Events\IssueEvent;
10
use Fabrica\Http\Requests;
11
use Fabrica\Http\Api\Controller;
12
13
use DB;
14
use Sentinel;
15
use Fabrica\Project\Provider;
16
use Fabrica\Project\Eloquent\Module;
17
18
class ModuleController extends Controller
19
{
20
    public function __construct()
21
    {
22
        $this->middleware('privilege:manage_project', [ 'except' => [ 'index' ] ]);
23
        parent::__construct();
24
    }
25
26
    /**
27
     * Display a listing of the resource.
28
     *
29
     * @return \Illuminate\Http\Response
30
     */
31
    public function index($project_key)
32
    {
33
        $modules = Module::whereRaw([ 'project_key' => $project_key ])->orderBy('sn', 'asc')->get();
34
        foreach ($modules as $module)
35
        {
36
            $module->is_used = $this->isFieldUsedByIssue($project_key, 'module', $module->toArray()); 
37
        }
38
        $users = Provider::getUserList($project_key);
39
        return response()->json([ 'ecode' => 0, 'data' => $modules, 'options' => [ 'users' => $users ] ]);
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...
40
    }
41
42
    /**
43
     * Store a newly created resource in storage.
44
     *
45
     * @param  \Illuminate\Http\Request $request
46
     * @return \Illuminate\Http\Response
47
     */
48
    public function store(Request $request, $project_key)
49
    {
50
        $name = $request->input('name');
51
        if (!$name) {
52
            throw new \UnexpectedValueException('the name can not be empty.', -11400);
53
        }
54
55
        if (Module::whereRaw([ 'name' => $name ])->exists()) {
56
            throw new \UnexpectedValueException('module name cannot be repeated', -11401);
57
        }
58
59
        $principal = [];
60
        $principal_id = $request->input('principal');
61 View Code Duplication
        if (isset($principal_id)) {
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...
62
            $user_info = Sentinel::findById($principal_id);
63
            $principal = [ 'id' => $principal_id, 'name' => $user_info->first_name ];
64
        }
65
66
        $creator = [ 'id' => $this->user->id, 'name' => $this->user->first_name, 'email' => $this->user->email ];
67
        $module = Module::create(
68
            [ 
69
            'project_key' => $project_key, 
70
            'principal' => $principal, 
71
            'sn' => time(), 
72
            'creator' => $creator ] + $request->all()
73
        );
74
75
        // trigger event of version added
76
        //Event::fire(new ModuleEvent($project_key, $creator, [ 'event_key' => 'create_module', 'data' => $module->name ]));
77
78
        return response()->json([ 'ecode' => 0, 'data' => $module ]);
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...
79
    }
80
81
    /**
82
     * Display the specified resource.
83
     *
84
     * @param  int $id
85
     * @return \Illuminate\Http\Response
86
     */
87 View Code Duplication
    public function show($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
        $module = Module::find($id);
90
        $module->is_used = $this->isFieldUsedByIssue($project_key, 'module', $module->toArray());
91
92
        return response()->json(['ecode' => 0, 'data' => $module]);
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...
93
    }
94
95
    /**
96
     * Update the specified resource in storage.
97
     *
98
     * @param  \Illuminate\Http\Request $request
99
     * @param  int                      $id
100
     * @return \Illuminate\Http\Response
101
     */
102
    public function update(Request $request, $project_key, $id)
103
    {
104
        $module = Module::find($id);
105
        if (!$module || $module->project_key != $project_key) {
106
            throw new \UnexpectedValueException('the module does not exist or is not in the project.', -11402);
107
        }
108
109
        $name = $request->input('name');
110 View Code Duplication
        if (isset($name)) {
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...
111
            if (!$name) {
112
                throw new \UnexpectedValueException('the name can not be empty.', -11400);
113
            }
114
            if ($module->name !== $name && Module::whereRaw([ 'name' => $name ])->exists()) {
115
                throw new \UnexpectedValueException('module name cannot be repeated', -11401);
116
            }
117
        }
118
119
        $principal_id = $request->input('principal');
120
        if (isset($principal_id)) {
121 View Code Duplication
            if ($principal_id) {
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...
122
                $user_info = Sentinel::findById($principal_id);
123
                $principal = [ 'id' => $principal_id, 'name' => $user_info->first_name ];
124
            }
125
            else
126
            {
127
                $principal = [];
128
            }
129
        }
130
        else
131
        {
132
            $principal = isset($module['principal']) ? $module['principal'] : [];
133
        }
134
135
        $module->fill([ 'principal' => $principal ] + array_except($request->all(), [ 'creator', 'project_key' ]))->save();
136
137
        // trigger event of module edited
138
        //$cur_user = [ 'id' => $this->user->id, 'name' => $this->user->first_name, 'email' => $this->user->email ];
139
        //Event::fire(new ModuleEvent($project_key, $cur_user, [ 'event_key' => 'edit_module', 'data' => $request->all() ]));
140
141
        return response()->json([ 'ecode' => 0, 'data' => Module::find($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...
142
    }
143
144
    /**
145
     * Remove the specified resource from storage.
146
     *
147
     * @param  \Illuminate\Http\Request $request
148
     * @param  int                      $id
149
     * @return \Illuminate\Http\Response
150
     */
151
    public function delete(Request $request, $project_key, $id)
152
    {
153
        $module = Module::find($id);
154
        if (!$module || $project_key != $module->project_key) {
155
            throw new \UnexpectedValueException('the module does not exist or is not in the project.', -11402);
156
        }
157
158
        $operate_flg = $request->input('operate_flg');
159
        if (!isset($operate_flg) || $operate_flg === '0') {
160
            $is_used = $this->isFieldUsedByIssue($project_key, 'module', $module->toArray());
161
            if ($is_used) {
162
                throw new \UnexpectedValueException('the module has been used by some issues.', -11403);
163
            }
164
        }
165
        else if ($operate_flg === '1') {
166
            $swap_module = $request->input('swap_module');
167
            if (!isset($swap_module) || !$swap_module) {
168
                throw new \UnexpectedValueException('the swap module cannot be empty.', -11405);
169
            }
170
171
            $smodule = Module::find($swap_module);
172
            if (!$smodule || $project_key != $smodule->project_key) {
173
                throw new \UnexpectedValueException('the swap module does not exist or is not in the project.', -11406);
174
            }
175
176
            $this->updIssueModule($project_key, $id, $swap_module);
177
        }
178
        else if ($operate_flg === '2') {
179
            $this->updIssueModule($project_key, $id, '');
180
        }
181
        else
182
        {
183
            throw new \UnexpectedValueException('the operation has error.', -11404);
184
        }
185
186
        Module::destroy($id);
187
188
        // trigger event of module deleted 
189
        //$cur_user = [ 'id' => $this->user->id, 'name' => $this->user->first_name, 'email' => $this->user->email ];
190
        //Event::fire(new ModuleEvent($project_key, $cur_user, [ 'event_key' => 'del_module', 'data' => $module->name ]));
191
192 View Code Duplication
        if ($operate_flg === '1') {
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...
193
            return $this->show($project_key, $request->input('swap_module'));
194
        }
195
        else
196
        {
197
            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...
198
        }
199
    }
200
201
    /**
202
     * update the issues module
203
     *
204
     * @param  array  $issues
0 ignored issues
show
Bug introduced by
There is no parameter named $issues. 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...
205
     * @param  string $source
206
     * @param  string $dest
207
     * @return \Illuminate\Http\Response
208
     */
209
    public function updIssueModule($project_key, $source, $dest)
210
    {
211
        $issues = DB::collection('issue_' . $project_key)
212
            ->where('module', $source)
213
            ->where('del_flg', '<>', 1)
214
            ->get();
215
216
        foreach ($issues as $issue)
217
        {
218
            $updValues = [];
219
220
            if (is_string($issue['module'])) {
221
                if ($issue['module'] === $source) {
222
                    $updValues['module'] = $dest;
223
                }
224
            } 
225
            else if (is_array($issue['module'])) {
226
                $updValues['module'] = array_values(array_filter(array_unique(str_replace($source, $dest, $issue['module']))));
227
            }
228
229
            if ($updValues) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $updValues of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
230
                $updValues['modifier'] = [ 'id' => $this->user->id, 'name' => $this->user->first_name, 'email' => $this->user->email ];
231
                $updValues['updated_at'] = time();
232
233
                $issue_id = $issue['_id']->__toString();
234
235
                DB::collection('issue_' . $project_key)->where('_id', $issue_id)->update($updValues);
236
                // add to histroy table
237
                $snap_id = Provider::snap2His($project_key, $issue_id, [], [ 'module' ]);
238
                // trigger event of issue edited
239
                Event::fire(new IssueEvent($project_key, $issue_id, $updValues['modifier'], [ 'event_key' => 'edit_issue', 'snap_id' => $snap_id ]));
240
            }
241
        }
242
    }
243
244
    /**
245
     * update sort etc..
246
     *
247
     * @param  \Illuminate\Http\Request $request
248
     * @return void
249
     */
250 View Code Duplication
    public function handle(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...
251
    {
252
        // set module sort.
253
        $sequence_modules = $request->input('sequence') ?: [];
254
        if ($sequence_modules) {
255
            $i = 1;
256
            foreach ($sequence_modules as $module_id)
257
            {
258
                $module = Module::find($module_id);
259
                if (!$module || $module->project_key != $project_key) {
260
                    continue;
261
                }
262
                $module->sn = $i++;
263
                $module->save();
264
            }
265
        }
266
267
        return response()->json(['ecode' => 0, 'data' => [ 'sequence' => $sequence_modules ] ]);
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...
268
    }
269
}
270