Completed
Pull Request — master (#36)
by Daniel
03:44 queued 01:27
created

ViewBuilder   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 7
dl 0
loc 39
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A createView() 0 11 1
A resolveOptions() 0 13 1
1
<?php
2
3
namespace Psi\Component\ContentType\View;
4
5
use Psi\Component\ContentType\FieldLoader;
6
use Psi\Component\ContentType\LoadedField;
7
use Symfony\Component\OptionsResolver\OptionsResolver;
8
9
/**
10
 * TODO: This should probably be renamed to "factory".
11
 */
12
class ViewBuilder
13
{
14
    private $fieldLoader;
15
    private $viewRegistry;
16
17
    public function __construct(
18
        FieldLoader $fieldLoader,
19
        ViewRegistry $viewRegistry
20
    ) {
21
        $this->fieldLoader = $fieldLoader;
22
        $this->viewRegistry = $viewRegistry;
23
    }
24
25
    public function createView(string $fieldType, $data, array $options)
26
    {
27
        $fieldService = $this->fieldLoader->load($fieldType, $options);
28
        $viewService = $this->viewRegistry->get($fieldService->getInnerField()->getViewType());
29
        $options = $this->resolveOptions($fieldService, $viewService, $options);
30
31
        $view = new View($options['template']);
32
        $viewService->buildView($view, $data, $options);
33
34
        return $view;
35
    }
36
37
    private function resolveOptions(LoadedField $field, ViewInterface $view, array $options)
0 ignored issues
show
Unused Code introduced by
The parameter $options is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
38
    {
39
        $options = $field->getViewOptions();
40
41
        $viewOptionResolver = new OptionsResolver();
42
        $viewOptionResolver->setDefault('template', null);
43
        $viewOptionResolver->setAllowedTypes('template', ['string', 'null']);
44
45
        $view->configureOptions($viewOptionResolver);
46
        $options = $viewOptionResolver->resolve($options);
47
48
        return $options;
49
    }
50
}
51