DataTransformerLoaderTrait::transform()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 15
ccs 0
cts 14
cp 0
rs 9.4285
cc 3
eloc 9
nc 3
nop 1
crap 12
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