Completed
Push — master ( e4a46d...3346e3 )
by Adam
06:33
created

SimilarProductSubscriber   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 9

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getSubscribedEvents() 0 7 1
A onProductInit() 0 7 2
A onProductFormAdminInit() 0 22 2
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
13
namespace WellCommerce\Bundle\SimilarProductBundle\EventListener;
14
15
use Doctrine\Common\Collections\ArrayCollection;
16
use WellCommerce\Bundle\CatalogBundle\Entity\Product;
17
use WellCommerce\Bundle\CoreBundle\Doctrine\Event\EntityEvent;
18
use WellCommerce\Bundle\CoreBundle\EventListener\AbstractEventSubscriber;
19
use WellCommerce\Component\Form\Event\FormEvent;
20
21
/**
22
 * Class SimilarProductSubscriber
23
 *
24
 * @author  Adam Piotrowski <[email protected]>
25
 */
26
class SimilarProductSubscriber extends AbstractEventSubscriber
27
{
28
    public static function getSubscribedEvents()
29
    {
30
        return [
31
            'admin.product.pre_form_init' => ['onProductFormAdminInit'],
32
            'product.post_init'           => ['onProductInit'],
33
        ];
34
    }
35
    
36
    public function onProductInit(EntityEvent $event)
37
    {
38
        $entity = $event->getEntity();
39
        if ($entity instanceof Product) {
40
            $entity->setSimilarProducts(new ArrayCollection());
41
        }
42
    }
43
    
44
    public function onProductFormAdminInit(FormEvent $event)
45
    {
46
        $resource = $event->getResource();
47
        if ($resource instanceof Product) {
48
            $form    = $event->getForm();
49
            $builder = $event->getFormBuilder();
50
            
51
            $similarProductData = $form->addChild($builder->getElement('nested_fieldset', [
52
                'name'  => 'similar_products',
53
                'label' => 'similar_product.fieldset.products',
54
            ]));
55
            
56
            $similarProductData->addChild($builder->getElement('product_select', [
57
                'name'        => 'similarProducts',
58
                'label'       => 'similar_product.fieldset.products',
59
                'load_route'  => 'admin.product.grid',
60
                'repeat_min'  => 0,
61
                'repeat_max'  => 50,
62
                'transformer' => $builder->getRepositoryTransformer('collection', $this->container->get('product.repository')),
63
            ]));
64
        }
65
    }
66
}
67