EntityWrapper::convert()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 3
eloc 5
nc 3
nop 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