SdAssets   C
last analyzed

Complexity

Total Complexity 62

Size/Duplication

Total Lines 292
Duplicated Lines 17.12 %

Coupling/Cohesion

Components 2
Dependencies 7

Importance

Changes 0
Metric Value
dl 50
loc 292
rs 5.9493
c 0
b 0
f 0
wmc 62
lcom 2
cbo 7

29 Methods

Rating   Name   Duplication   Size   Complexity  
A setExternalIdAttribute() 0 3 1
A departmentRelation() 0 3 1
A departments() 0 11 3
A impactRelation() 0 3 1
A impacts() 0 12 3
A locationRelation() 0 3 1
A locations() 0 11 3
A assetTypes() 0 11 3
A used() 14 14 4
A managed() 14 14 4
A products() 0 11 3
A assignedOn() 0 8 2
A usedBy() 0 3 1
A managedBy() 0 3 1
A assetType() 0 3 1
A relation() 0 3 1
A assetForm() 0 3 1
A additionalData() 0 3 1
A product() 0 3 1
B detachRelation() 0 15 5
A deleteAttachment() 0 5 1
A delete() 0 6 1
A requests() 0 9 2
A assetRelation() 0 13 2
A getRequesters() 0 15 3
B switchCase() 0 16 5
A getOrganizationRelation() 0 4 1
A getOrganization() 10 10 3
A getOrgWithLink() 12 12 3

How to fix   Duplicated Code    Complexity   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like SdAssets often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use SdAssets, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
namespace App\Plugins\ServiceDesk\Model\Assets;
4
5
use Illuminate\Database\Eloquent\Model;
6
use App\Plugins\ServiceDesk\Model\Common\AssetRelation;
7
8
class SdAssets extends Model {
9
10
    protected $table = 'sd_assets';
11
    protected $fillable = [
12
        'id',
13
        'name',
14
        'description',
15
        'department_id',
16
        'asset_type_id',
17
        'impact_type_id',
18
        'managed_by',
19
        'used_by',
20
        'attachment',
21
        'location_id',
22
        'assigned_on',
23
        'product_id',
24
        'external_id',
25
        'organization',
26
    ];
27
28
    public function setExternalIdAttribute($value) {
29
        $this->attributes['external_id'] = str_slug($value);
30
    }
31
32
    public function departmentRelation() {
33
        return $this->belongsTo('App\Model\helpdesk\Agent\Department', 'department_id');
34
    }
35
36
    /**
37
     * get the department name
38
     * @return string
39
     */
40
    public function departments() {
41
        $value = "--";
42
        $attr = $this->attributes['department_id'];
43
        if ($attr) {
44
            $attrs = $this->departmentRelation()->first();
45
            if ($attrs) {
46
                $value = $attrs->name;
47
            }
48
        }
49
        return ucfirst($value);
50
    }
51
52
    public function impactRelation() {
53
        return $this->belongsTo('App\Plugins\ServiceDesk\Model\Assets\SdImpactypes', 'impact_type_id');
54
    }
55
56
    /**
57
     * get the impact name
58
     * @return string
59
     */
60
    public function impacts() {
61
        $value = "--";
62
        $attr = $this->attributes['impact_type_id'];
63
        //dd()
64
        if ($attr) {
65
            $attrs = $this->impactRelation()->first();
66
            if ($attrs) {
67
                $value = $attrs->name;
68
            }
69
        }
70
        return ucfirst($value);
71
    }
72
73
    public function locationRelation() {
74
        return $this->belongsTo('App\Plugins\ServiceDesk\Model\Assets\SdLocations', 'location_id');
75
    }
76
77
    /**
78
     * get the location name
79
     * @return string
80
     */
81
    public function locations() {
82
        $value = "--";
83
        $attr = $this->attributes['location_id'];
84
        if ($attr) {
85
            $attrs = $this->locationRelation()->first();
86
            if ($attrs) {
87
                $value = "<a href=" . url('service-desk/location-types/' . $attr . '/show') . ">$attrs->title</a>";
88
            }
89
        }
90
        return ucfirst($value);
91
    }
92
93
    public function assetTypes() {
94
        $value = "--";
95
        $attr = $this->attributes['asset_type_id'];
96
        if ($attr) {
97
            $attrs = $this->assetType()->first();
98
            if ($attrs) {
99
                $value = $attrs->name;
100
            }
101
        }
102
        return ucfirst($value);
103
    }
104
105 View Code Duplication
    public function used() {
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...
106
        $value = "--";
107
        $attr = $this->attributes['used_by'];
108
        if ($attr) {
109
            $attrs = $this->usedBy()->first();
110
            if ($attrs) {
111
                $value = ucfirst($attrs->first_name) . " " . ucfirst($attrs->last_name);
112
                if ($value == " ") {
113
                    $value = $attrs->user_name;
114
                }
115
            }
116
        }
117
        return $value;
118
    }
119
120 View Code Duplication
    public function managed() {
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...
121
        $value = "--";
122
        $attr = $this->attributes['managed_by'];
123
        if ($attr) {
124
            $attrs = $this->managedBy()->first();
125
            if ($attrs) {
126
                $value = ucfirst($attrs->first_name) . " " . ucfirst($attrs->last_name);
127
                if ($value == " ") {
128
                    $value = $attrs->user_name;
129
                }
130
            }
131
        }
132
        return $value;
133
    }
134
135
    public function products() {
136
        $value = "--";
137
        $attr = $this->attributes['product_id'];
138
        if ($attr) {
139
            $attrs = $this->product()->first();
140
            if ($attrs) {
141
                $value = "<a href=" . url('service-desk/products/' . $attr . '/show') . ">$attrs->name</a>";
142
            }
143
        }
144
        return $value;
145
    }
146
147
    public function assignedOn() {
148
        $value = "--";
149
        $attr = $this->attributes['assigned_on'];
150
        if ($attr !== NULL) {
151
            $value = $this->attributes['assigned_on'];
152
        }
153
        return $value;
154
    }
155
156
    public function usedBy() {
157
        return $this->belongsTo('App\User', 'used_by');
158
    }
159
160
    public function managedBy() {
161
        return $this->belongsTo('App\User', 'managed_by');
162
    }
163
164
    public function assetType() {
165
        return $this->belongsTo('App\Plugins\ServiceDesk\Model\Assets\SdAssettypes');
166
    }
167
168
    public function relation() {
169
        return $this->hasMany('App\Plugins\ServiceDesk\Model\Common\TicketAssetProblem', 'asset_id');
170
    }
171
172
    public function assetForm() {
173
        return $this->hasMany('App\Plugins\ServiceDesk\Model\Assets\AssetForm', 'asset_id');
174
    }
175
176
    public function additionalData() {
177
        return $this->assetForm()->lists('value', 'key')->toArray();
178
    }
179
180
    public function product() {
181
        return $this->belongsTo('App\Plugins\ServiceDesk\Model\Products\SdProducts', 'product_id');
182
    }
183
184
    public function detachRelation() {
185
        $relations = $this->relation()->get();
186
        if ($relations->count() > 0) {
187
            foreach ($relations as $relation) {
188
                $relation->asset_id = NULL;
189
                $relation->save();
190
            }
191
        }
192
        $forms = $this->assetForm()->get();
193
        if ($forms->count() > 0) {
194
            foreach ($forms as $form) {
195
                $form->delete();
196
            }
197
        }
198
    }
199
200
    public function deleteAttachment($id) {
201
        \App\Plugins\ServiceDesk\Controllers\Library\UtilityController::deleteAssetRelation($id);
202
        $table = $this->table;
203
        \App\Plugins\ServiceDesk\Controllers\Library\UtilityController::deleteAttachments($id, $table);
204
    }
205
206
    public function delete() {
207
        $id = $this->id;
0 ignored issues
show
Documentation introduced by
The property id does not exist on object<App\Plugins\Servi...\Model\Assets\SdAssets>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
208
        $this->deleteAttachment($id);
209
        $this->detachRelation();
210
        parent::delete();
211
    }
212
213
    public function requests() {
214
        $assets = $this->assetRelation();
215
        $ids = [];
216
        foreach ($assets as $key => $value) {
217
            $explode = explode('.', $key);
218
            $ids[] = array_first($explode);
219
        }
220
        return $this->getRequesters($ids);
221
    }
222
223
    public function assetRelation() {
224
        $relation = new AssetRelation();
225
        $relations = $relation->lists('asset_ids', 'id')->toArray();
0 ignored issues
show
Documentation Bug introduced by
The method lists does not exist on object<App\Plugins\Servi...l\Common\AssetRelation>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
226
        $array = array_dot($relations);
227
        $id = $this->attributes['id'];
228
        $array1 = array_where($array, function ($key, $value) use($id) {
229
            if ($value == $id) {
230
                return $value;
231
            }
232
        });
233
234
        return $array1;
235
    }
236
237
    public function getRequesters($ids) {
238
        $relation = new AssetRelation();
239
        $relations = $relation->whereIn('id', $ids)->lists('owner')->toArray();
0 ignored issues
show
Documentation Bug introduced by
The method whereIn does not exist on object<App\Plugins\Servi...l\Common\AssetRelation>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
240
        $schema = [];
241
        if (count($relations) > 0) {
242
            foreach ($relations as $relation) {
243
                $explode = explode(':', $relation);
244
                $table = array_first($explode);
245
                $id = array_last($explode);
246
                //dd($id);
247
                $schema[] = $this->switchCase($table, $id);
248
            }
249
        }
250
        return $schema;
251
    }
252
253
    public function switchCase($table, $id) {
254
        $change = new \App\Plugins\ServiceDesk\Model\Changes\SdChanges();
255
        $release = new \App\Plugins\ServiceDesk\Model\Releases\SdReleases();
256
        $ticket = new \App\Plugins\ServiceDesk\Model\Common\Ticket();
257
        $problem = new \App\Plugins\ServiceDesk\Model\Problem\SdProblem();
258
        switch ($table) {
259
            case "sd_changes":
260
                return $change->find($id);
0 ignored issues
show
Documentation Bug introduced by
The method find does not exist on object<App\Plugins\Servi...odel\Changes\SdChanges>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
261
            case "sd_releases":
262
                return $release->find($id);
0 ignored issues
show
Documentation Bug introduced by
The method find does not exist on object<App\Plugins\Servi...el\Releases\SdReleases>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
263
            case "tickets":
264
                return $ticket->find($id);
0 ignored issues
show
Documentation Bug introduced by
The method find does not exist on object<App\Plugins\Servi...sk\Model\Common\Ticket>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
265
            case "sd_problem":
266
                return $problem->find($id);
0 ignored issues
show
Documentation Bug introduced by
The method find does not exist on object<App\Plugins\Servi...odel\Problem\SdProblem>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
267
        }
268
    }
269
270
    public function getOrganizationRelation() {
271
        $related = "App\Model\helpdesk\Agent_panel\Organization";
272
        return $this->belongsTo($related, 'organization');
273
    }
274
275 View Code Duplication
    public function getOrganization() {
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...
276
        $name = "";
277
        if ($this->getOrganizationRelation()) {
278
            $org = $this->getOrganizationRelation()->first();
279
            if ($org) {
280
                $name = $org->name;
281
            }
282
        }
283
        return $name;
284
    }
285
286 View Code Duplication
    public function getOrgWithLink() {
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...
287
        $name = "--";
288
        $org = $this->getOrganization();
289
        if ($org !== "") {
290
            $orgs = $this->getOrganizationRelation()->first();
291
            if ($orgs) {
292
                $id = $orgs->id;
293
                $name = "<a href=" . url('organizations/' . $id) . ">" . ucfirst($org) . "</a>";
294
            }
295
        }
296
        return $name;
297
    }
298
299
}
300