Completed
Push — dev ( e5a73e...d4e169 )
by Arnaud
03:54 queued 03:48
created

EntityLabelTrait::getEntityLabel()   B

Complexity

Conditions 7
Paths 7

Size

Total Lines 20
Code Lines 16

Duplication

Lines 20
Ratio 100 %

Code Coverage

Tests 15
CRAP Score 7.0119

Importance

Changes 2
Bugs 2 Features 0
Metric Value
c 2
b 2
f 0
dl 20
loc 20
ccs 15
cts 16
cp 0.9375
rs 8.2222
cc 7
eloc 16
nc 7
nop 1
crap 7.0119
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 View Code Duplication
    public function getEntityLabel($entity)
17
    {
18 1
        $label = '';
19 1
        $accessor = PropertyAccess::createPropertyAccessor();
20
21 1
        if ($accessor->isReadable($entity, 'label')) {
22 1
            $label = $accessor->getValue($entity, 'label');
23 1
        } else if ($accessor->isReadable($entity, 'title')) {
24 1
            $label = $accessor->getValue($entity, 'title');
25 1
        } else if ($accessor->isReadable($entity, 'name')) {
26
            $label = $accessor->getValue($entity, 'name');
27 1
        } else if ($accessor->isReadable($entity, '__toString')) {
28 1
            $label = $accessor->getValue($entity, '__toString');
29 1
        } else if ($accessor->isReadable($entity, 'content')) {
30 1
            $label = strip_tags(substr($label = $accessor->getValue($entity, 'content'), 0, 100));
31 1
        } else if ($accessor->isReadable($entity, 'id')) {
32 1
            $label = $accessor->getValue($entity, 'id');
33
        }
34 1
        return $label;
35
    }
36
}
37