EntityWrapper   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 27
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 2
dl 0
loc 27
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A registerConverter() 0 4 1
A convert() 0 9 3
A wrap() 0 4 1
1
<?php
2
3
namespace Drupal\easy_entity_reader;
4
5
use Drupal\easy_entity_reader\Converters\ConverterInterface;
6
use Drupal\Core\Entity\EntityInterface;
7
use Drupal\Core\Field\FieldItemInterface;
8
9
/**
10
 * Wrap value type to object to convert it.
11
 *
12
 * Class EntityWrapper.
13
 */
14
class EntityWrapper
15
{
16
    /**
17
     * @var ConverterInterface[]
18
     */
19
    private $converters;
20
21
    public function registerConverter(ConverterInterface $converter)
22
    {
23
        $this->converters[] = $converter;
24
    }
25
26
    public function convert(FieldItemInterface $value)
27
    {
28
        foreach ($this->converters as $converter) {
29
            if ($converter->canConvert($value)) {
30
                return $converter->convert($value);
31
            }
32
        }
33
        throw new \LogicException('Could not find a converter for value. Value type: '.get_class($value));
34
    }
35
36
    public function wrap(EntityInterface $entity) : EasyEntityAdapter
37
    {
38
        return new EasyEntityAdapter($entity, $this);
39
    }
40
}
41