Test Failed
Push — master ( 518d4d...5d775d )
by Gerhard
21:38 queued 10:20
created

SaveActionType::createViewData()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 15
ccs 0
cts 8
cp 0
rs 9.7666
c 0
b 0
f 0
cc 2
nc 2
nop 2
crap 6
1
<?php
2
3
/**
4
 * CancelButton.php
5
 *
6
 * @since 29/05/16
7
 * @author gseidel
8
 */
9
10
namespace Enhavo\Bundle\AppBundle\Action\Type;
11
12
use Enhavo\Bundle\AppBundle\Action\AbstractActionType;
13
use Enhavo\Bundle\AppBundle\Action\ActionTypeInterface;
14
use Enhavo\Bundle\AppBundle\Util\ArrayUtil;
15
use Symfony\Component\OptionsResolver\OptionsResolver;
16
use Symfony\Component\Routing\RouterInterface;
17
use Symfony\Contracts\Translation\TranslatorInterface;
18
19
class SaveActionType extends AbstractActionType implements ActionTypeInterface
20
{
21
    /** @var RouterInterface */
22
    private $router;
23
24
    /**
25
     * CreateAction constructor.
26
     * @param TranslatorInterface $translator
27
     * @param RouterInterface $router
28
     */
29
    public function __construct(TranslatorInterface $translator, RouterInterface $router)
30
    {
31
        parent::__construct($translator);
32
        $this->router = $router;
33
    }
34
35
    public function createViewData(array $options, $resource = null)
36
    {
37
        $data = parent::createViewData($options, $resource);
38
39
        $url = null;
40
        if ($options['route']) {
41
            $url = $this->getUrl($options, $resource);
42
        }
43
44
        $data = ArrayUtil::merge($data, [
45
            'url' => $url
46
        ]);
47
48
        return $data;
49
    }
50
51
    private function getUrl(array $options, $resource = null)
52
    {
53
        $parameters['id'] = $resource->getId();
0 ignored issues
show
Coding Style Comprehensibility introduced by
$parameters was never initialized. Although not strictly required by PHP, it is generally a good practice to add $parameters = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
54
        $parameters = array_merge_recursive($parameters, $options['route_parameters']);
55
        return $this->router->generate($options['route'], $parameters);
56
    }
57
58
    public function configureOptions(OptionsResolver $resolver)
59
    {
60
        parent::configureOptions($resolver);
61
62
        $resolver->setDefaults([
63
            'component' => 'save-action',
64
            'label' => 'label.save',
65
            'translation_domain' => 'EnhavoAppBundle',
66
            'icon' => 'save',
67
            'route' => null,
68
            'route_parameters' => [],
69
        ]);
70
    }
71
72
    public function getType()
73
    {
74
        return 'save';
75
    }
76
}
77