Passed
Push — release/3.x ( 784098...94bfaa )
by
unknown
26:51 queued 15:56
created

AliasOverviewType   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 32
dl 0
loc 89
ccs 0
cts 51
cp 0
rs 10
c 0
b 0
f 0
wmc 8

6 Methods

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

101
            ->/** @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...
102
    }
103
104
    /**
105
     * @{inheritDoc}
106
     */
107
    public function getName()
108
    {
109
        return 'alias_overview_type';
110
    }
111
}
112