Completed
Push — master ( 080b9b...8f9441 )
by Freek
01:54
created

TranslatableAttributeCollection   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3
Metric Value
wmc 8
lcom 1
cbo 3
dl 0
loc 40
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A createForModel() 0 10 2
A find() 0 10 3
A isTranslatable() 0 4 1
A getCast() 0 10 2
1
<?php
2
3
namespace Spatie\Translatable;
4
5
use Illuminate\Support\Collection;
6
7
class TranslatableAttributeCollection extends Collection
8
{
9
    public static function createForModel(Translatable $model)
10
    {
11
        $translatableFieldCollection = new static();
12
13
        foreach ($model->getTranslatableAttributes() as $key => $value) {
14
            $translatableFieldCollection->push(new TranslatableAttribute($key, $value));
15
        }
16
17
        return $translatableFieldCollection;
18
    }
19
20
    protected function find(string $attributeName)
21
    {
22
        foreach ($this->items as $translatableAttribute) {
23
            if ($translatableAttribute->name == $attributeName) {
24
                return $translatableAttribute;
25
            };
26
        }
27
28
        return;
29
    }
30
31
    public function isTranslatable(string $attributeName) : bool
32
    {
33
        return !is_null($this->find($attributeName));
34
    }
35
36
    public function getCast(string $attributeName) : string
37
    {
38
        $translatableAttribute = $this->find($attributeName);
39
40
        if (!$translatableAttribute) {
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
41
            // throw exception
42
        }
43
44
        return $this->find($attributeName)->cast;
45
    }
46
}
47