Completed
Push — remove-genericness-from-adminb... ( 37f191...9ee1a7 )
by Kamil
35:30 queued 08:38
created

ProductAttributeController   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
c 1
b 0
f 0
lcom 1
cbo 9
dl 0
loc 84
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getAttributeTypesAction() 0 15 1
A renderAttributesAction() 0 10 1
A renderAttributeValueFormsAction() 0 23 3
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\JsonResponse;
20
use Symfony\Component\HttpFoundation\Request;
21
use Symfony\Component\HttpFoundation\Response;
22
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
23
24
/**
25
 * @author Mateusz Zalewski <[email protected]>
26
 */
27
class ProductAttributeController extends ResourceController
28
{
29
    /**
30
     * @param Request $request
31
     * @param string $template
32
     *
33
     * @return Response
34
     */
35
    public function getAttributeTypesAction(Request $request, $template)
36
    {
37
        $configuration = $this->requestConfigurationFactory->create($this->metadata, $request);
38
39
        $view = View::create()
40
            ->setTemplate($template)
41
            ->setTemplateVar($this->metadata->getPluralName())
42
            ->setData([
43
                'types' => $this->get('sylius.registry.attribute_type')->all(),
44
                'metadata' => $this->metadata,
45
            ])
46
        ;
47
48
        return $this->viewHandler->handle($configuration, $view);
49
    }
50
51
    /**
52
     * @return Response
53
     */
54
    public function renderAttributesAction(Request $request)
55
    {
56
        $template = $request->attributes->get('template', 'SyliusAttributeBundle::attributeChoice.html.twig');
57
58
        $form = $this->get('form.factory')->create(ProductAttributeChoiceType::class, null, [
59
            'multiple' => true,
60
        ]);
61
62
        return $this->render($template, ['form' => $form->createView()]);
63
    }
64
65
    /**
66
     * @param Request $request
67
     *
68
     * @return Response
69
     */
70
    public function renderAttributeValueFormsAction(Request $request)
71
    {
72
        $template = $request->attributes->get('template', 'SyliusAttributeBundle::attributeValueForms.html.twig');
73
74
        $form = $this->get('form.factory')->create(ProductAttributeChoiceType::class, null, [
75
            'multiple' => true,
76
        ]);
77
        $form->handleRequest($request);
78
79
        $attributes = $form->getData();
80
        if (null === $attributes) {
81
            throw new BadRequestHttpException();
82
        }
83
84
        foreach ($attributes as $attribute) {
85
            $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...
86
        }
87
88
        return $this->render($template, [
89
            '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...
90
            'metadata' => $this->metadata,
91
        ]);
92
    }
93
94
    /**
95
     * @param AttributeInterface $attribute
96
     *
97
     * @return FormView
98
     */
99
    protected function getAttributeForm(AttributeInterface $attribute)
100
    {
101
        $attributeForm = $this->get('sylius.form_registry.attribute_type')->get($attribute->getType(), 'default');
102
103
        $form = $this
104
            ->get('form.factory')
105
            ->createNamed('value', $attributeForm, null, ['label' => $attribute->getName()])
106
        ;
107
108
        return $form->createView();
109
    }
110
}
111