Completed
Push — develop ( 935d2f...42d29e )
by Mohamed
06:41
created

GlobalIssue::fields()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 25
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 1

Importance

Changes 5
Bugs 0 Features 1
Metric Value
c 5
b 0
f 1
dl 0
loc 25
ccs 15
cts 15
cp 1
rs 8.8571
cc 1
eloc 15
nc 1
nop 0
crap 1
1
<?php
2
3
/*
4
 * This file is part of the Tinyissue package.
5
 *
6
 * (c) Mohamed Alsharaf <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Tinyissue\Form;
13
14
use Tinyissue\Model;
15
16
/**
17
 * GlobalIssue is a class to defines fields & rules for adding an issue form.
18
 *
19
 * @author Mohamed Alsharaf <[email protected]>
20
 */
21
class GlobalIssue extends Issue
22
{
23
    /**
24
     * List of projects.
25
     *
26
     * @var array
27
     */
28
    protected $projects = [];
29
30
    /**
31
     * Returns list of logged in user projects.
32
     *
33
     * @return array
34
     */
35 1
    protected function getProjects()
36
    {
37 1
        if (!$this->projects) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->projects 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...
38 1
            $this->projects = \Auth::user()->projects()->get()->lists('name', 'id');
39
        }
40
41 1
        return $this->projects;
42
    }
43
44
    /**
45
     * @param array $params
46
     *
47
     * @return void
48
     */
49 1
    public function setup(array $params)
50
    {
51 1
        $this->project = new Model\Project();
52 1
    }
53
54
    /**
55
     * @return array
56
     */
57 1
    public function actions()
58
    {
59
        return [
60 1
            'submit' => 'create_issue',
61
        ];
62
    }
63
64
    /**
65
     * @return array
66
     */
67 1
    public function fields()
68
    {
69 1
        $fields = $this->fieldTitle();
70
71 1
        $fields['project'] = [
72 1
            'type'    => 'select',
73 1
            'label'   => 'project',
74 1
            'options' => $this->getProjects()->all(),
0 ignored issues
show
Bug introduced by
The method all cannot be called on $this->getProjects() (of type array).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
75
        ];
76
77 1
        $fields += $this->fieldBody();
78
79 1
        $fields['tag'] = [
80 1
            'type'    => 'select',
81 1
            'label'   => 'type',
82 1
            'options' => ['' => ''] + (new Model\Tag())->getTypeTags()->lists('name', 'id')->map(function ($name) {
83 1
                return ucwords($name);
84 1
            })->all(),
85
        ];
86
87
        // Only on creating new issue
88 1
        $fields += $this->fieldUpload();
89
90 1
        return $fields;
91
    }
92
93
    /**
94
     * @return array
95
     */
96 1
    public function rules()
97
    {
98 1
        $rules            = parent::rules();
99 1
        $rules['project'] = 'required|in:' . $this->getProjects()->keys()->implode(',');
0 ignored issues
show
Bug introduced by
The method keys cannot be called on $this->getProjects() (of type array).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
100
101 1
        return $rules;
102
    }
103
104
    /**
105
     * @return string
106
     */
107
    public function getRedirectUrl()
108
    {
109
        return 'projects/new-issue';
110
    }
111
}
112