Completed
Pull Request — master (#33)
by Daniel
15:07 queued 09:14
created

CollectionView::buildView()   B

Complexity

Conditions 5
Paths 3

Size

Total Lines 20
Code Lines 12

Duplication

Lines 6
Ratio 30 %

Importance

Changes 0
Metric Value
dl 6
loc 20
rs 8.8571
c 0
b 0
f 0
cc 5
eloc 12
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\View\View;
8
use Psi\Component\ContentType\View\ViewBuilder;
9
use Psi\Component\ContentType\View\ViewInterface;
10
use Psi\Component\ContentType\View\ViewIterator;
11
use Symfony\Component\OptionsResolver\OptionsResolver;
12
13
class CollectionView implements ViewInterface
14
{
15
    private $builder;
16
17
    public function __construct(ViewBuilder $builder)
18
    {
19
        $this->builder = $builder;
20
    }
21
22
    public function buildView(View $view, $data, array $options)
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
        $view->setValue(new ViewIterator(
36
            $this->builder,
37
            $data,
38
            $options['field_type'],
39
            $options['field_options']
40
        ));
41
    }
42
43
    public function configureOptions(OptionsResolver $options)
44
    {
45
        $options->setDefault('template', 'psi/collection');
46
        $options->setRequired([
47
            'field_type',
48
            'field_options',
49
        ]);
50
    }
51
}
52