Completed
Push — develop ( 335c93...e4583a )
by Mohamed
12:58
created

FormAbstract::setLoggedUser()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 1 Features 1
Metric Value
c 1
b 1
f 1
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
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 Illuminate\Database\Eloquent\Model;
15
use Tinyissue\Model\Project;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Tinyissue\Form\Project.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
16
use Tinyissue\Model\User;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Tinyissue\Form\User.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
17
use Illuminate\Contracts\Auth\Guard;
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
    /**
27
     * An instance of Model.
28
     *
29
     * @var Model
30
     */
31
    protected $model;
32
33
    /**
34
     * An instance of Current logged user.
35
     *
36
     * @var User
37
     */
38
    protected $user;
39
40
    /**
41
     * Set an instance of current logged user.
42
     *
43
     * @param User $user
44
     *
45
     * @return $this
46
     */
47 14
    public function setLoggedUser(User $user)
48
    {
49 14
        $this->user = $user;
50
51 14
        return $this;
52
    }
53
54
    /**
55
     * Set an instance of model currently being edited.
56
     *
57
     * @param Model $model
58
     *
59
     * @return void|FormInterface
60
     */
61 9
    public function editingModel(Model $model)
62
    {
63 9
        $this->model = $model;
64
65 9
        return $this;
66
    }
67
68
    /**
69
     * Setup the object from the route parameters.
70
     *
71
     * @param array $params
72
     *
73
     * @return FormInterface
74
     */
75
    public function setup(array $params)
76
    {
77 24
        $model = array_first($params, function ($key, $value) {
78 3
            return $value instanceof Model;
79 24
        });
80 24
        if ($model) {
81 3
            $this->editingModel($model);
82
        }
83
84 24
        return $this;
85
    }
86
87
    /**
88
     * Whether or not the form is in editing of a model.
89
     *
90
     * @return bool
91
     */
92 26
    public function isEditing()
93
    {
94 26
        return $this->model instanceof Model;
95
    }
96
97
    /**
98
     * Return an instance of the model being edited.
99
     *
100
     * @return Model
101
     */
102 48
    public function getModel()
103
    {
104 48
        return $this->model;
105
    }
106
107
    /**
108
     * Returns form type.
109
     *
110
     * @return string
111
     */
112 39
    public function openType()
113
    {
114 39
        return 'open';
115
    }
116
117
    /**
118
     * Returns an array of form actions.
119
     *
120
     * @return array
121
     */
122 14
    public function actions()
123
    {
124 14
        return [];
125
    }
126
127
    /**
128
     * Returns an array of form fields.
129
     *
130
     * @return array
131
     */
132
    public function fields()
133
    {
134
        return [];
135
    }
136
137
    /**
138
     * Returns an array form rules.
139
     *
140
     * @return array
141
     */
142 15
    public function rules()
143
    {
144 15
        return [];
145
    }
146
147
    /**
148
     * Returns the form redirect url on error.
149
     *
150
     * @return string
151
     */
152
    public function getRedirectUrl()
153
    {
154
        return '';
155
    }
156
157
    /**
158
     * Returns project upload fields.
159
     *
160
     * @param string  $name
161
     * @param Project $project
162
     * @param User    $user
163
     *
164
     * @return array
165
     */
166
    protected function projectUploadFields($name, Project $project, User $user)
167
    {
168
        return [
169
            $name => [
170
                'type'                 => 'FileUpload',
171
                'data_message_success' => trans('tinyissue.success_upload'),
172
                'data_message_failed'  => trans('tinyissue.error_uploadfailed'),
173
                'multiple'             => null,
174
            ],
175
            $name . '_token' => [
176
                'type'  => 'hidden',
177
                'value' => md5($project->id . time() . $user->id . rand(1, 100)),
178
            ],
179
        ];
180
    }
181
}
182