EpicController::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
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\IssueEvent;
9
use Fabrica\Http\Requests;
10
use Fabrica\Http\Api\Controller;
11
use Fabrica\Customization\Eloquent\State;
12
use Fabrica\Project\Eloquent\Epic;
13
use Fabrica\Project\Eloquent\Board;
14
use Fabrica\Project\Provider;
15
use DB;
16
17
class EpicController extends Controller
18
{
19
    public function __construct()
20
    {
21
        $this->middleware('privilege:manage_project', [ 'except' => [ 'index' ] ]);
22
        parent::__construct();
23
    }
24
25
    /**
26
     * Display a listing of the resource.
27
     *
28
     * @return \Illuminate\Http\Response
29
     */
30
    public function index(Request $request, $project_key)
31
    {
32
        $kanban_id = $request->input('kanban_id');
33
        $board = Board::find($kanban_id);
34
35
        $columns = isset($board->columns) ? $board->columns : [];
36
37
        $all_states = [];
38
        foreach ($columns as $column)
39
        {
40
            $all_states = array_merge($all_states, isset($column['states']) ? $column['states'] : []);
41
        }
42
        $last_column = array_pop($columns);
43
        $completed_states = isset($last_column['states']) ? $last_column['states'] : [];
44
45
        $epics = Epic::where([ 'project_key' => $project_key ])->orderBy('sn', 'asc')->get();
46
        foreach ($epics as $epic)
47
        {
48
            $epic->is_used = $this->isFieldUsedByIssue($project_key, 'epic', $epic->toArray());
49
50
            $completed_issue_cnt = $incompleted_issue_cnt = $inestimable_issue_cnt = 0;
51
52
            $issues = DB::collection('issue_' . $project_key)
53
                ->where('epic', $epic->id)
54
                ->where('del_flg', '<>', 1)
55
                ->get(['state']);
56
            foreach ($issues as $issue)
57
            {
58
                if (in_array($issue['state'], $completed_states)) {
59
                    $completed_issue_cnt++;
60
                }
61
                else if (in_array($issue['state'], $all_states)) {
62
                    $incompleted_issue_cnt++;
63
                }
64
                else
65
                {
66
                    $inestimable_issue_cnt++;
67
                }
68
            }
69
            $epic->completed = $completed_issue_cnt;
70
            $epic->incompleted = $incompleted_issue_cnt;
71
            $epic->inestimable = $inestimable_issue_cnt;
72
        }
73
            
74
        return response()->json([ 'ecode' => 0, 'data' => $epics, 'options' => [ 'completed_states' => $completed_states, 'incompleted_states' => array_diff($all_states, $completed_states) ] ]);
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...
75
    }
76
77
    /**
78
     * Store a newly created resource in storage.
79
     *
80
     * @param  \Illuminate\Http\Request $request
81
     * @return \Illuminate\Http\Response
82
     */
83 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...
84
    {
85
        $name = $request->input('name');
86
        if (!$name) {
87
            throw new \UnexpectedValueException('the name can not be empty.', -11800);
88
        }
89
90
        $bgColor = $request->input('bgColor');
91
        if (!$bgColor) {
92
            throw new \UnexpectedValueException('the bgColor can not be empty.', -11801);
93
        }
94
95
        if (Provider::isEpicExisted($project_key, $name)) {
96
            throw new \UnexpectedValueException('epic name cannot be repeated', -11802);
97
        }
98
99
        $epic = Epic::create([ 'project_key' => $project_key, 'sn' => time() ] + $request->all());
100
        return response()->json(['ecode' => 0, 'data' => $epic]);
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...
101
    }
102
103
    /**
104
     * Display the specified resource.
105
     *
106
     * @param  int $id
107
     * @return \Illuminate\Http\Response
108
     */
109 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...
110
    {
111
        $epic = Epic::find($id);
112
        $epic->is_used = $this->isFieldUsedByIssue($project_key, 'epic', $epic->toArray());
113
114
        return response()->json(['ecode' => 0, 'data' => $epic]);
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...
115
    }
116
117
    /**
118
     * Update the specified resource in storage.
119
     *
120
     * @param  \Illuminate\Http\Request $request
121
     * @param  int                      $id
122
     * @return \Illuminate\Http\Response
123
     */
124
    public function update(Request $request, $project_key, $id)
125
    {
126
        $name = $request->input('name');
127
        if (isset($name)) {
128
            if (!$name) {
129
                throw new \UnexpectedValueException('the name can not be empty.', -11800);
130
            }
131
        }
132
133
        $bgColor = $request->input('bgColor');
134
        if (isset($bgColor)) {
135
            if (!$bgColor) {
136
                throw new \UnexpectedValueException('the bgColor can not be empty.', -11801);
137
            }
138
        }
139
140
        $epic = Epic::find($id);
141
        if (!$epic || $project_key != $epic->project_key) {
142
            throw new \UnexpectedValueException('the epic does not exist or is not in the project.', -11803);
143
        }
144
145
        if ($epic->name !== $name && Provider::isEpicExisted($project_key, $name)) {
146
            throw new \UnexpectedValueException('epic name cannot be repeated', -11802);
147
        }
148
149
        $epic->fill($request->except(['project_key']))->save();
150
151
        return response()->json(['ecode' => 0, 'data' => Epic::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...
152
    }
153
154
    /**
155
     * update sort or defaultValue etc..
156
     *
157
     * @param  \Illuminate\Http\Request $request
158
     * @return void
159
     */
160 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...
161
    {
162
        // set epic sort.
163
        $sequence_epics = $request->input('sequence');
164
        if (isset($sequence_epics)) {
165
            $i = 1;
166
            foreach ($sequence_epics as $epic_id)
167
            {
168
                $epic = Epic::find($epic_id);
169
                if (!$epic || $epic->project_key != $project_key) {
170
                    continue;
171
                }
172
                $epic->sn = $i++;
173
                $epic->save();
174
            }
175
        }
176
177
        return response()->json(['ecode' => 0, 'data' => [ 'sequence' => $sequence_epics ]]);
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...
178
    }
179
180
    /**
181
     * Remove the specified resource from storage.
182
     *
183
     * @param  \Illuminate\Http\Request $request
184
     * @param  int                      $id
185
     * @return \Illuminate\Http\Response
186
     */
187 View Code Duplication
    public function delete(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...
188
    {
189
        $epic = Epic::find($id);
190
        if (!$epic || $project_key != $epic->project_key) {
191
            throw new \UnexpectedValueException('the epic does not exist or is not in the project.', -11803);
192
        }
193
194
        $operate_flg = $request->input('operate_flg');
195
        if (!isset($operate_flg) || $operate_flg === '0') {
196
            $is_used = $this->isFieldUsedByIssue($project_key, 'epic', $epic->toArray());
197
            if ($is_used) {
198
                throw new \UnexpectedValueException('the epic has been used by some issues.', -11804);
199
            }
200
        }
201
        else if ($operate_flg === '1') {
202
            $swap_epic = $request->input('swap_epic');
203
            if (!isset($swap_epic) || !$swap_epic) {
204
                throw new \UnexpectedValueException('the swap epic cannot be empty.', -11806);
205
            }
206
207
            $sepic = Epic::find($swap_epic);
208
            if (!$sepic || $project_key != $sepic->project_key) {
209
                throw new \UnexpectedValueException('the swap epic does not exist or is not in the project.', -11807);
210
            }
211
212
            $this->updIssueEpic($project_key, $id, $swap_epic);
213
        }
214
        else if ($operate_flg === '2') {
215
            $this->updIssueEpic($project_key, $id, '');
216
        }
217
        else
218
        {
219
            throw new \UnexpectedValueException('the operation has error.', -11805);
220
        }
221
222
        Epic::destroy($id);
223
224
        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...
225
226
        //if ($operate_flg === '1')
227
        //{
228
        //    return $this->show($project_key, $request->input('swap_epic'));
229
        //}
230
        //else
231
        //{
232
        //    return response()->json(['ecode' => 0, 'data' => [ 'id' => $id ]]);
233
        //}
234
    }
235
236
    /**
237
     * update the issues epic
238
     *
239
     * @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...
240
     * @param  string $source
241
     * @param  string $dest
242
     * @return \Illuminate\Http\Response
243
     */
244 View Code Duplication
    public function updIssueEpic($project_key, $source, $dest)
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...
245
    {
246
        $issues = DB::collection('issue_' . $project_key)
247
            ->where('epic', $source)
248
            ->where('del_flg', '<>', 1)
249
            ->get();
250
251
        foreach ($issues as $issue)
252
        {
253
            $updValues = [];
254
            $updValues['epic'] = $dest;
255
256
            $updValues['modifier'] = [ 'id' => $this->user->id, 'name' => $this->user->first_name, 'email' => $this->user->email ];
257
            $updValues['updated_at'] = time();
258
259
            $issue_id = $issue['_id']->__toString();
260
261
            DB::collection('issue_' . $project_key)->where('_id', $issue_id)->update($updValues);
262
            // add to histroy table
263
            $snap_id = Provider::snap2His($project_key, $issue_id, [], [ 'epic' ]);
264
            // trigger event of issue edited
265
            Event::fire(new IssueEvent($project_key, $issue_id, $updValues['modifier'], [ 'event_key' => 'edit_issue', 'snap_id' => $snap_id ]));
266
        }
267
    }
268
}
269