Completed
Push — dev ( e5bbcf...4306de )
by Arnaud
05:15
created

EntityLabelTrait   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 2 Features 0
Metric Value
wmc 3
c 3
b 2
f 0
lcom 0
cbo 2
dl 0
loc 33
ccs 16
cts 16
cp 1
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A getEntityLabel() 0 23 3
1
<?php
2
3
namespace LAG\AdminBundle\Admin\Behaviors;
4
5
use Symfony\Component\PropertyAccess\PropertyAccess;
6
7
trait EntityLabelTrait
8
{
9
    /**
10
     * Try to find a property to get a label from an entity. If found, it returns the property value through the
11
     * property accessor.
12
     *
13
     * @param $entity
14
     * @return string
15
     */
16 1
    public function getEntityLabel($entity)
17
    {
18 1
        $label = '';
19 1
        $accessor = PropertyAccess::createPropertyAccessor();
20
        $properties = [
21 1
            'label',
22 1
            'title',
23 1
            'name',
24 1
            '__toString',
25 1
            'content',
26 1
            'id',
27 1
        ];
28
29 1
        foreach ($properties as $property) {
30
31 1
            if ($accessor->isReadable($entity, $property)) {
32 1
                $label = $accessor->getValue($entity, $property);
33 1
                break;
34
            }               
35 1
        }
36
        
37 1
        return $label;
38
    }
39
}
40