Completed
Pull Request — 3.1 (#348)
by Piotr
06:19 queued 04:50
created

Category   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

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

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getId() 0 4 1
A getClassName() 0 4 1
A initDataGrid() 0 25 1
A initDataSource() 0 14 1
A initForm() 0 17 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace FSi\FixturesBundle\Admin;
6
7
use FSi\Bundle\AdminBundle\Doctrine\Admin\CRUDElement;
8
use FSi\Component\DataGrid\DataGridFactoryInterface;
9
use FSi\Component\DataGrid\DataGridInterface;
10
use FSi\Component\DataSource\DataSourceFactoryInterface;
11
use FSi\Component\DataSource\DataSourceInterface;
12
use FSi\FixturesBundle\Entity;
13
use Symfony\Component\Form\Extension\Core\Type\FormType;
14
use Symfony\Component\Form\Extension\Core\Type\TextType;
15
use Symfony\Component\Form\FormFactoryInterface;
16
use Symfony\Component\Form\FormInterface;
17
18
class Category extends CRUDElement
19
{
20
    public function getId(): string
21
    {
22
        return 'category';
23
    }
24
25
    public function getClassName(): string
26
    {
27
        return Entity\Category::class;
28
    }
29
30
    protected function initDataGrid(DataGridFactoryInterface $factory): DataGridInterface
31
    {
32
        return $factory->createDataGrid($this->getId())
33
            ->addColumn('title', 'text', [
34
                'label' => 'admin.category.list.title',
35
                'field_mapping' => ['title'],
36
                'editable' => true
37
            ])->addColumn('actions', 'action', [
38
                'label' => 'admin.category.list.actions',
39
                'field_mapping' => ['id'],
40
                'actions' => [
41
                    'news' => [
42
                        'route_name' => 'fsi_admin_list',
43
                        'additional_parameters' => ['element' => 'category_news'],
44
                        'parameters_field_mapping' => ['parent' => 'id'],
45
                        'redirect_uri' => false,
46
                    ],
47
                    'edit' => [
48
                        'route_name' => 'fsi_admin_form',
49
                        'additional_parameters' => ['element' => $this->getId()],
50
                        'parameters_field_mapping' => ['id' => 'id']
51
                    ],
52
                ]
53
            ]);
54
    }
55
56
    protected function initDataSource(DataSourceFactoryInterface $factory): DataSourceInterface
57
    {
58
        return $factory
59
            ->createDataSource(
60
                'doctrine-orm',
61
                ['entity' => $this->getClassName()],
62
                'news'
63
            )->addField('title', 'text', 'like', [
64
                'sortable' => false,
65
                'form_options' => [
66
                    'label' => 'admin.category.list.title',
67
                ]
68
            ])->setMaxResults(10);
69
    }
70
71
    protected function initForm(FormFactoryInterface $factory, $data = null): FormInterface
72
    {
73
        $builder = $factory->createNamedBuilder(
74
            'category',
75
            FormType::class,
76
            $data,
77
            ['data_class' => $this->getClassName()]
78
        );
79
80
        $builder->add(
81
            'title',
82
            TextType::class,
83
            ['label' => 'admin.category.list.title']
84
        );
85
86
        return $builder->getForm();
87
    }
88
}
89