Completed
Push — symfony-fqcn-sylius-4 ( 896d93...f41f61 )
by Kamil
18:34
created

ProductAttributeController   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 8
dl 0
loc 81
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getAttributeTypesAction() 0 15 1
A renderAttributesAction() 0 10 1
A renderAttributeValueFormsAction() 0 20 2
A getAttributeForm() 0 11 1
1
<?php
2
3
/*
4
 * This file is part of the Sylius package.
5
 *
6
 * (c) Paweł Jędrzejewski
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Sylius\Bundle\ProductBundle\Controller;
13
14
use FOS\RestBundle\View\View;
15
use Sylius\Bundle\ProductBundle\Form\Type\ProductAttributeChoiceType;
16
use Sylius\Bundle\ResourceBundle\Controller\ResourceController;
17
use Sylius\Component\Attribute\Model\AttributeInterface;
18
use Symfony\Component\Form\FormView;
19
use Symfony\Component\HttpFoundation\Request;
20
use Symfony\Component\HttpFoundation\Response;
21
22
/**
23
 * @author Mateusz Zalewski <[email protected]>
24
 */
25
class ProductAttributeController extends ResourceController
26
{
27
    /**
28
     * @param Request $request
29
     * @param string $template
30
     *
31
     * @return Response
32
     */
33
    public function getAttributeTypesAction(Request $request, $template)
34
    {
35
        $configuration = $this->requestConfigurationFactory->create($this->metadata, $request);
36
37
        $view = View::create()
38
            ->setTemplate($template)
39
            ->setTemplateVar($this->metadata->getPluralName())
40
            ->setData([
41
                'types' => $this->get('sylius.registry.attribute_type')->all(),
42
                'metadata' => $this->metadata,
43
            ])
44
        ;
45
46
        return $this->viewHandler->handle($configuration, $view);
47
    }
48
49
    /**
50
     * @return Response
51
     */
52
    public function renderAttributesAction(Request $request)
53
    {
54
        $template = $request->attributes->get('template', 'SyliusAttributeBundle::attributeChoice.html.twig');
55
56
        $form = $this->get('form.factory')->create(ProductAttributeChoiceType::class, null, [
57
            'multiple' => true,
58
        ]);
59
60
        return $this->render($template, ['form' => $form->createView()]);
61
    }
62
63
    /**
64
     * @param Request $request
65
     *
66
     * @return Response
67
     */
68
    public function renderAttributeValueFormsAction(Request $request)
69
    {
70
        $template = $request->attributes->get('template', 'SyliusAttributeBundle::attributeValueForms.html.twig');
71
72
        $form = $this->get('form.factory')->create(ProductAttributeChoiceType::class, null, [
73
            'multiple' => true,
74
        ]);
75
        $form->handleRequest($request);
76
77
        $attributes = $form->getData();
78
        foreach ($attributes as $attribute) {
79
            $forms[$attribute->getId()] = $this->getAttributeForm($attribute);
0 ignored issues
show
Coding Style Comprehensibility introduced by
$forms was never initialized. Although not strictly required by PHP, it is generally a good practice to add $forms = 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...
80
        }
81
82
        return $this->render($template, [
83
            'forms' => $forms,
0 ignored issues
show
Bug introduced by
The variable $forms does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
84
            'count' => $request->query->get('count'),
85
            'metadata' => $this->metadata,
86
        ]);
87
    }
88
89
    /**
90
     * @param AttributeInterface $attribute
91
     *
92
     * @return FormView
93
     */
94
    private function getAttributeForm(AttributeInterface $attribute)
95
    {
96
        $attributeForm = $this->get('sylius.form_registry.attribute_type')->get($attribute->getType(), 'default');
97
98
        $form = $this
99
            ->get('form.factory')
100
            ->createNamed('value', $attributeForm, null, ['label' => $attribute->getName()])
101
        ;
102
103
        return $form->createView();
104
    }
105
}
106