Completed
Push — master ( 477ca5...2d5ed5 )
by Daniel
10s
created

CollectionType::createView()   B

Complexity

Conditions 5
Paths 3

Size

Total Lines 26
Code Lines 16

Duplication

Lines 6
Ratio 23.08 %

Importance

Changes 0
Metric Value
dl 6
loc 26
rs 8.439
c 0
b 0
f 0
cc 5
eloc 16
nc 3
nop 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Psi\Component\ContentType\Standard\View;
6
7
use Psi\Component\ContentType\FieldLoader;
8
use Psi\Component\ContentType\View\TypeInterface;
9
use Psi\Component\ContentType\View\ViewFactory;
10
use Psi\Component\ContentType\View\ViewInterface;
11
use Symfony\Component\OptionsResolver\OptionsResolver;
12
13
class CollectionType implements TypeInterface
14
{
15
    private $fieldLoader;
16
17
    public function __construct(FieldLoader $fieldLoader)
18
    {
19
        $this->fieldLoader = $fieldLoader;
20
    }
21
22
    public function createView(ViewFactory $factory, $data, array $options): ViewInterface
23
    {
24 View Code Duplication
        if (!is_array($data) && !$data instanceof \Traversable) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
25
            throw new \InvalidArgumentException(sprintf(
26
                'Data must be traversable or an array, got: "%s"',
27
                is_object($data) ? get_class($data) : gettype($data)
28
            ));
29
        }
30
31
        if (is_array($data)) {
32
            $data = new \ArrayIterator($data);
33
        }
34
35
        $field = $this->fieldLoader->load(
36
            $options['field_type'],
37
            $options['field_options']
38
        );
39
40
        return new CollectionView(
41
            $options['template'],
42
            $factory,
43
            $data,
44
            $field->getViewType(),
45
            $field->getViewOptions()
46
        );
47
    }
48
49
    public function configureOptions(OptionsResolver $options)
50
    {
51
        $options->setDefault('template', 'psi/collection');
52
        $options->setRequired([
53
            'field_type',
54
            'field_options',
55
        ]);
56
    }
57
}
58