Completed
Pull Request — master (#304)
by Piotr
07:03
created

CategoryNews   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 10

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 10
dl 0
loc 74
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace FSi\FixturesBundle\Admin;
4
5
use FSi\Bundle\AdminBundle\Doctrine\Admin\DependentCRUDElement;
6
use FSi\Bundle\AdminBundle\Form\TypeSolver;
7
use FSi\Component\DataGrid\DataGridFactoryInterface;
8
use FSi\Component\DataSource\DataSourceFactoryInterface;
9
use FSi\FixturesBundle\DataGrid\NewsDataGridBuilder;
10
use FSi\FixturesBundle\DataSource\NewsDataSourceBuilder;
11
use FSi\FixturesBundle\Form\NewsType;
12
use Symfony\Component\Form\FormFactoryInterface;
13
14
class CategoryNews extends DependentCRUDElement
15
{
16
    public function getId()
17
    {
18
        return 'category_news';
19
    }
20
21
    public function getParentId()
22
    {
23
        return 'category';
24
    }
25
26
    public function getClassName()
27
    {
28
        return 'FSi\FixturesBundle\Entity\News';
29
    }
30
31
    protected function initDataGrid(DataGridFactoryInterface $factory)
32
    {
33
        /* @var $datagrid \FSi\Component\DataGrid\DataGrid */
34
        $datagrid = $factory->createDataGrid('category_news');
35
36
        NewsDataGridBuilder::buildNewsDataGrid($datagrid);
37
38
        $datagrid->addColumn('actions', 'action', [
39
            'label' => 'admin.news.list.actions',
40
            'field_mapping' => ['id'],
41
            'actions' => [
42
                'edit' => [
43
                    'route_name' => "fsi_admin_crud_edit",
44
                    'additional_parameters' => ['element' => $datagrid->getName()],
45
                    'parameters_field_mapping' => ['id' => 'id']
46
                ],
47
                'display' => [
48
                    'element' => CategoryNewsDisplay::ID
49
                ]
50
            ]
51
        ]);
52
53
        return $datagrid;
54
    }
55
56
    protected function initDataSource(DataSourceFactoryInterface $factory)
57
    {
58
        $queryBuilder = $this->getRepository()
59
            ->createQueryBuilder('n');
60
61
        if ($this->getParentObject()) {
62
            $queryBuilder->where(':category MEMBER OF n.categories')
63
                ->setParameter('category', $this->getParentObject());
64
        }
65
66
        /* @var $datasource \FSi\Component\DataSource\DataSource */
67
        $datasource = $factory->createDataSource('doctrine', ['qb' => $queryBuilder], 'category_news');
68
69
        NewsDataSourceBuilder::buildNewsDataSource($datasource);
70
71
        return $datasource;
72
    }
73
74
    protected function initForm(FormFactoryInterface $factory, $data = null)
75
    {
76
        if ($data === null) {
77
            $data = new \FSi\FixturesBundle\Entity\News();
78
            $data->addCategory($this->getParentObject());
79
        }
80
81
        return $factory->createNamed(
82
            'news',
83
            TypeSolver::getFormType('FSi\FixturesBundle\Form\NewsType', new NewsType()),
84
            $data
85
        );
86
    }
87
}
88