Passed
Push — feature/depend-on-metadata-inf... ( ef854e...953c11 )
by
unknown
39:13 queued 32:26
created

AliasOverviewType   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 97
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 33
dl 0
loc 97
ccs 0
cts 55
cp 0
rs 10
c 0
b 0
f 0
wmc 9

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A configureOptions() 0 10 1
A buildView() 0 7 1
A getUrlAliases() 0 12 2
A getName() 0 3 1
A groupByMode() 0 9 2
A getBlockPrefix() 0 3 1
1
<?php
2
/**
3
 * @copyright Zicht Online <http://zicht.nl>
4
 */
5
6
namespace Zicht\Bundle\UrlBundle\Admin;
7
8
use Symfony\Bridge\Doctrine\RegistryInterface;
9
use Symfony\Component\Form\AbstractType;
10
use Symfony\Component\Form\FormInterface;
11
use Symfony\Component\Form\FormView;
12
use Symfony\Component\OptionsResolver\OptionsResolver;
13
use Zicht\Bundle\UrlBundle\Entity\UrlAlias;
14
use Zicht\Bundle\UrlBundle\Exception\UnsupportedException;
15
use Zicht\Bundle\UrlBundle\Url\Provider;
16
17
/**
18
 * Form type to render all available url aliases
19
 */
20
class AliasOverviewType extends AbstractType
21
{
22
    /** @var Provider */
23
    protected $provider;
24
25
    /** @var RegistryInterface */
26
    protected $doctrine;
27
28
    /**
29
     * AliasOverviewType constructor.
30
     *
31
     * @param Provider $provider
32
     * @param RegistryInterface $doctrine
33
     */
34
    public function __construct(Provider $provider, RegistryInterface $doctrine)
35
    {
36
        $this->provider = $provider;
37
        $this->doctrine = $doctrine;
38
    }
39
40
    /**
41
     * @{inheritDoc}
42
     */
43
    public function configureOptions(OptionsResolver $resolver)
44
    {
45
        parent::configureOptions($resolver);
46
        $resolver->setRequired('record');
47
        $resolver->setDefaults(
48
            [
49
                'translation_domain' => 'admin',
50
                'label' => 'admin.alias_overview_admin.label',
51
                'required' => false,
52
                'inherit_data' => true,
53
            ]
54
        );
55
    }
56
57
    /**
58
     * @{inheritDoc}
59
     */
60
    public function buildView(FormView $view, FormInterface $form, array $options)
61
    {
62
        parent::buildView($view, $form, $options);
63
        $aliases =  $this->getUrlAliases($options['record']);
64
        $view->vars['record'] = $options['record'];
65
        $view->vars['url_aliases'] = $aliases;
66
        $view->vars['url_aliases_grouped'] = $this->groupByMode($aliases);
67
    }
68
69
    /**
70
     * @param UrlAlias[] $aliases
71
     * @return array|UrlAlias[][]
72
     */
73
    private function groupByMode($aliases)
74
    {
75
        $property = new \ReflectionProperty(UrlAlias::class, 'mode');
76
        $property->setAccessible(true);
77
        $grouped = [];
78
        foreach ($aliases as $alias) {
79
            $grouped[$property->getValue($alias)][] = $alias;
80
        }
81
        return $grouped;
82
    }
83
84
    /**
85
     * Returns all UrlAlias entities associated to an object
86
     *
87
     * @param mixed $object
88
     */
89
    protected function getUrlAliases($object)
90
    {
91
        try {
92
            $internalUrl = $this->provider->url($object);
93
        } catch (UnsupportedException $exception) {
94
            return [];
95
        }
96
97
        return $this
98
            ->doctrine
99
            ->getRepository('ZichtUrlBundle:UrlAlias')
100
            ->findAllByInternalUrl($internalUrl);
0 ignored issues
show
Bug introduced by
The method findAllByInternalUrl() does not exist on Doctrine\Common\Persistence\ObjectRepository. Did you maybe mean findAll()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

100
            ->/** @scrutinizer ignore-call */ findAllByInternalUrl($internalUrl);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
101
    }
102
103
    /**
104
     * @{inheritDoc}
105
     */
106
    public function getName()
107
    {
108
        return 'alias_overview_type';
109
    }
110
111
    /**
112
     * {@inheritDoc}
113
     */
114
    public function getBlockPrefix()
115
    {
116
        return 'alias_overview_type';
117
    }
118
}
119