Completed
Branch develop-3.0 (55e83a)
by Mohamed
02:53
created

UserMessagesSettings::getDefaultMessage()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 0
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 Illuminate\Support\Collection;
15
use Tinyissue\Model\Message;
16
use Tinyissue\Model\Project as ProjectModel;
17
18
/**
19
 * UserMessagesSettings is a class to defines fields & rules for edit user message settings form.
20
 *
21
 * @author Mohamed Alsharaf <[email protected]>
22
 */
23
class UserMessagesSettings extends FormAbstract
24
{
25
    /**
26
     * @var Collection
27
     */
28
    protected $projects;
29
30
    /**
31
     * @var Collection
32
     */
33
    protected $messages;
34
35
    /**
36
     * @var Message
37
     */
38
    protected $defaultMessage;
39
40
    /**
41
     * @param Collection $projects
42
     *
43
     * @return $this
44
     */
45
    public function setProjects(Collection $projects)
46
    {
47
        $this->projects = $projects;
48
49
        return $this;
50
    }
51
52
    /**
53
     * @return array
54
     */
55
    public function actions()
56
    {
57
        return [
58
            'submit' => 'update',
59
        ];
60
    }
61
62
    /**
63
     * @return array
64
     */
65
    public function fields()
66
    {
67
        $fields = [];
68
69
        // Change label widths
70
        \Former::setOption('TwitterBootstrap3.labelWidths', ['large' => 4, 'small' => 4]);
71
72
        /** @var Collection $messages Available message options */
73
        $messages = $this->getMessages()->dropdown();
74
75
        // Create field for each project
76
        $this->projects->each(function (ProjectModel $project) use (&$fields, $messages) {
77
            $fields['projects[' . $project->id . ']'] = $this->getSelectField($project, $messages);
0 ignored issues
show
Documentation introduced by
$messages is of type object<Illuminate\Support\Collection>, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
78
        });
79
80
        return $fields;
81
    }
82
83
    /**
84
     * @param ProjectModel $project
85
     * @param array        $messages
86
     *
87
     * @return array
88
     */
89
    protected function getSelectField(ProjectModel $project, array $messages)
90
    {
91
        $messageId = $this->getSelectedMessage($project);
92
93
        return [
94
            'type'    => 'select',
95
            'label'   => $project->name,
96
            'options' => $messages,
97
            'value'   => $messageId,
98
            'help'    => trans('tinyissue.messages_' . strtolower($messages[$messageId] . '_help')),
99
        ];
100
    }
101
102
    /**
103
     * Returns collection of all messages options.
104
     *
105
     * @return Collection
106
     */
107
    protected function getMessages()
108
    {
109
        if (null === $this->messages) {
110
            $this->messages = $this->app->make(Message::class)->all();
111
        }
112
113
        return $this->messages;
114
    }
115
116
    /**
117
     * Return default message for the current logged user based on the role.
118
     *
119
     * @return Message
120
     */
121
    protected function getDefaultMessage()
122
    {
123
        if (null === $this->defaultMessage) {
124
            $name = Message::$defaultMessageToRole[$this->getLoggedUser()->getRoleName()];
125
            $this->defaultMessage = $this->getMessages()->getByName($name);
126
        }
127
128
        return $this->defaultMessage;
129
    }
130
131
    /**
132
     * Return value of selected message for a project.
133
     *
134
     * @param ProjectModel $project
135
     *
136
     * @return int
137
     */
138
    protected function getSelectedMessage(ProjectModel $project)
139
    {
140
        $selected = $project->getPreferredMessageIdForUser($this->getLoggedUser()->id);
141
142
        if ($selected <= 0) {
143
            $selected = $this->getDefaultMessage()->id;
144
        }
145
146
        return $selected;
147
    }
148
}
149