Completed
Branch master (7a36f1)
by Mohamed
04:08
created

FormAbstract   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 139
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 87.1%

Importance

Changes 4
Bugs 4 Features 1
Metric Value
wmc 11
lcom 1
cbo 3
dl 0
loc 139
ccs 27
cts 31
cp 0.871
rs 10
c 4
b 4
f 1

10 Methods

Rating   Name   Duplication   Size   Complexity  
A isEditing() 0 4 1
A openType() 0 4 1
A editingModel() 0 6 1
A setup() 0 11 2
A getModel() 0 4 1
A actions() 0 4 1
A fields() 0 4 1
A rules() 0 4 1
A getRedirectUrl() 0 4 1
A projectUploadFields() 0 15 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 Illuminate\Database\Eloquent\Model;
15
use Tinyissue\Extensions\Auth\LoggedUser;
16
use Tinyissue\Model\Project as ProjectModel;
17
use Tinyissue\Model\User as UserModel;
18
19
/**
20
 * FormAbstract is an abstract class for Form classes.
21
 *
22
 * @author Mohamed Alsharaf <[email protected]>
23
 */
24
abstract class FormAbstract implements FormInterface
25
{
26
    use LoggedUser;
27
28
    /**
29
     * An instance of Model.
30
     *
31
     * @var Model
32
     */
33
    protected $model;
34
35
    /**
36
     * Set an instance of model currently being edited.
37
     *
38
     * @param Model $model
39
     *
40
     * @return void|FormInterface
41
     */
42 13
    public function editingModel(Model $model)
43
    {
44 13
        $this->model = $model;
45
46 13
        return $this;
47
    }
48
49
    /**
50
     * Setup the object from the route parameters.
51
     *
52
     * @param array $params
53
     *
54
     * @return FormInterface
55
     */
56
    public function setup(array $params)
57
    {
58 26
        $model = array_first($params, function ($key, $value) {
59 4
            return $value instanceof Model;
60 26
        });
61 26
        if ($model) {
62 4
            $this->editingModel($model);
63
        }
64
65 26
        return $this;
66
    }
67
68
    /**
69
     * Whether or not the form is in editing of a model.
70
     *
71
     * @return bool
72
     */
73 32
    public function isEditing()
74
    {
75 32
        return $this->model instanceof Model;
76
    }
77
78
    /**
79
     * Return an instance of the model being edited.
80
     *
81
     * @return Model
82
     */
83 50
    public function getModel()
84
    {
85 50
        return $this->model;
86
    }
87
88
    /**
89
     * Returns form type.
90
     *
91
     * @return string
92
     */
93 41
    public function openType()
94
    {
95 41
        return 'open';
96
    }
97
98
    /**
99
     * Returns an array of form actions.
100
     *
101
     * @return array
102
     */
103 14
    public function actions()
104
    {
105 14
        return [];
106
    }
107
108
    /**
109
     * Returns an array of form fields.
110
     *
111
     * @return array
112
     */
113
    public function fields()
114
    {
115
        return [];
116
    }
117
118
    /**
119
     * Returns an array form rules.
120
     *
121
     * @return array
122
     */
123 31
    public function rules()
124
    {
125 31
        return [];
126
    }
127
128
    /**
129
     * Returns the form redirect url on error.
130
     *
131
     * @return string
132
     */
133
    public function getRedirectUrl()
134
    {
135
        return '';
136
    }
137
138
    /**
139
     * Returns project upload fields.
140
     *
141
     * @param string       $name
142
     * @param ProjectModel $project
143
     * @param UserModel    $user
144
     *
145
     * @return array
146
     */
147 13
    protected function projectUploadFields($name, ProjectModel $project, UserModel $user)
148
    {
149
        return [
150
            $name => [
151 13
                'type'                 => 'FileUpload',
152 13
                'data_message_success' => trans('tinyissue.success_upload'),
153 13
                'data_message_failed'  => trans('tinyissue.error_uploadfailed'),
154
                'multiple'             => null,
155 13
            ],
156 13
            $name . '_token' => [
157 13
                'type'  => 'hidden',
158 13
                'value' => md5($project->id . time() . $user->id . rand(1, 100)),
159
            ],
160
        ];
161
    }
162
}
163