LayoutBoxSubscriber::onLayoutBoxFormInit()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 19
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 19
rs 9.2
c 0
b 0
f 0
cc 4
eloc 12
nc 4
nop 1
1
<?php
2
/*
3
 * WellCommerce Open-Source E-Commerce Platform
4
 *
5
 * This file is part of the WellCommerce package.
6
 *
7
 * (c) Adam Piotrowski <[email protected]>
8
 *
9
 * For the full copyright and license information,
10
 * please view the LICENSE file that was distributed with this source code.
11
 */
12
namespace WellCommerce\Bundle\AppBundle\EventListener;
13
14
use Symfony\Component\HttpFoundation\Request;
15
use Symfony\Component\PropertyAccess\PropertyAccess;
16
use WellCommerce\Bundle\AppBundle\Entity\LayoutBox;
17
use WellCommerce\Bundle\CoreBundle\Doctrine\Event\EntityEvent;
18
use WellCommerce\Bundle\CoreBundle\EventListener\AbstractEventSubscriber;
19
use WellCommerce\Component\Form\Event\FormEvent;
20
use WellCommerce\Component\Layout\Configurator\LayoutBoxConfiguratorInterface;
21
22
/**
23
 * Class LayoutBoxSubscriber
24
 *
25
 * @author  Adam Piotrowski <[email protected]>
26
 */
27
class LayoutBoxSubscriber extends AbstractEventSubscriber
28
{
29
    public static function getSubscribedEvents()
30
    {
31
        return [
32
            'admin.layout_box.pre_form_init' => 'onLayoutBoxFormInit',
33
            'layout_box.pre_update'          => 'onLayoutBoxPreUpdate',
34
        ];
35
    }
36
    
37
    /**
38
     * Adds configurator fields to main layout box edit form.
39
     * Loops through all configurators, renders the fieldset and sets default data
40
     *
41
     * @param FormEvent $event
42
     */
43
    public function onLayoutBoxFormInit(FormEvent $event)
44
    {
45
        $builder       = $event->getFormBuilder();
46
        $form          = $event->getForm();
47
        $configurators = $this->container->get('layout_box.configurator.collection')->all();
48
        $resource      = $event->getResource();
49
        $boxSettings   = $resource->getSettings();
50
        
51
        foreach ($configurators as $configurator) {
52
            if ($configurator instanceof LayoutBoxConfiguratorInterface) {
53
                $defaults = [];
54
                if ($resource->getBoxType() == $configurator->getType()) {
55
                    $defaults = $boxSettings;
56
                }
57
                
58
                $configurator->addFormFields($builder, $form, $defaults);
59
            }
60
        }
61
    }
62
    
63
    /**
64
     * Sets resource settings fetched from fieldset corresponding to selected box type
65
     *
66
     * @param EntityEvent $event
67
     */
68
    public function onLayoutBoxPreUpdate(EntityEvent $event)
69
    {
70
        $resource = $event->getEntity();
71
        if ($resource instanceof LayoutBox) {
72
            $request  = $this->getRequestHelper()->getCurrentRequest();
73
            $settings = $this->getBoxSettingsFromRequest($request);
0 ignored issues
show
Bug introduced by
It seems like $request defined by $this->getRequestHelper()->getCurrentRequest() on line 72 can be null; however, WellCommerce\Bundle\AppB...oxSettingsFromRequest() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
74
            $settings = $this->mergeUnmodifiedSettings($resource->getSettings(), $settings);
75
            $resource->setSettings($settings);
76
        }
77
    }
78
    
79
    private function getBoxSettingsFromRequest(Request $request)
80
    {
81
        $settings   = [];
82
        $accessor   = PropertyAccess::createPropertyAccessor();
83
        $parameters = $request->request->all();
84
        $boxType    = $accessor->getValue($parameters, '[required_data][boxType]');
85
        if ($accessor->isReadable($parameters, '[' . $boxType . ']')) {
86
            $settings = $accessor->getValue($parameters, '[' . $boxType . ']');
87
        }
88
        
89
        return !is_array($settings) ? [] : $settings;
90
    }
91
    
92
    private function mergeUnmodifiedSettings(array $oldSettings, array $newSettings): array
93
    {
94
        foreach ($newSettings as $key => &$setting) {
95
            if (is_array($setting) && array_key_exists('unmodified', $setting) && isset($setting[0])) {
96
                $setting[0] = (0 !== (int)$setting[0]) ? $setting[0] : $oldSettings[$key][0];
97
            }
98
        }
99
        
100
        return $newSettings;
101
    }
102
}
103