DataTransformerLoaderTrait   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 6
c 0
b 0
f 0
lcom 1
cbo 1
dl 0
loc 44
ccs 0
cts 28
cp 0
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A transform() 0 15 3
A reverseTransform() 0 15 3
1
<?php
2
3
namespace Majora\Framework\Loader\Bridge\Form;
4
5
use Symfony\Component\Form\Exception\TransformationFailedException;
6
7
/**
8
 * Trait which provides a bridge between majora loader and symfony forms
9
 *
10
 * @see DataTransformerInterface
11
 *
12
 * @property entityClass
13
 * @method retrieve($id) : CollectionableInterface
14
 */
15
trait DataTransformerLoaderTrait
16
{
17
    /**
18
     * Model -> View
19
     *
20
     * @see DataTransformerInterface::transform()
21
     */
22
    public function transform($entity)
23
    {
24
        if (null === $entity) {
25
            return '';
26
        }
27
        if (!is_subclass_of($entity, $this->entityClass)) {
0 ignored issues
show
Bug introduced by
The property entityClass does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
Bug introduced by
Due to PHP Bug #53727, is_subclass_of might return inconsistent results on some PHP versions if $this->entityClass can be an interface. If so, you could instead use ReflectionClass::implementsInterface.
Loading history...
28
            throw new \InvalidArgumentException(sprintf(
29
                'Unsupported entity "%s" into "%s" loader.',
30
                get_class($entity),
31
                __CLASS__
32
            ));
33
        }
34
35
        return $entity->getId();
36
    }
37
38
    /**
39
     * View -> Model
40
     *
41
     * @see DataTransformerInterface::reverseTransform()
42
     */
43
    public function reverseTransform($id)
44
    {
45
        if (!$id) {
46
            return null;
47
        }
48
        if (!$entity = $this->retrieve($id)) {
49
            throw new TransformationFailedException(sprintf(
50
                '%s#%s cannot be found.',
51
                $this->entityClass,
52
                $id
53
            ));
54
        }
55
56
        return $entity;
57
    }
58
}
59