Completed
Pull Request — 2.1 (#242)
by Łukasz
08:11 queued 05:17
created

CategoryNews   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9

Importance

Changes 0
Metric Value
wmc 8
c 0
b 0
f 0
lcom 1
cbo 9
dl 0
loc 70
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getId() 0 4 1
A getParentId() 0 4 1
A getClassName() 0 4 1
B initDataGrid() 0 24 1
A initDataSource() 0 17 2
A initForm() 0 9 2
1
<?php
2
3
namespace FSi\FixturesBundle\Admin;
4
5
use FSi\Bundle\AdminBundle\Doctrine\Admin\DependentCRUDElement;
6
use FSi\Component\DataGrid\DataGridFactoryInterface;
7
use FSi\Component\DataSource\DataSourceFactoryInterface;
8
use FSi\FixturesBundle\DataGrid\NewsDataGridBuilder;
9
use FSi\FixturesBundle\DataSource\NewsDataSourceBuilder;
10
use FSi\FixturesBundle\Form\NewsType;
11
use Symfony\Component\Form\FormFactoryInterface;
12
13
class CategoryNews extends DependentCRUDElement
14
{
15
    public function getId()
16
    {
17
        return 'category_news';
18
    }
19
20
    public function getParentId()
21
    {
22
        return 'category';
23
    }
24
25
    public function getClassName()
26
    {
27
        return 'FSi\FixturesBundle\Entity\News';
28
    }
29
30
    protected function initDataGrid(DataGridFactoryInterface $factory)
31
    {
32
        /* @var $datagrid \FSi\Component\DataGrid\DataGrid */
33
        $datagrid = $factory->createDataGrid('category_news');
34
35
        NewsDataGridBuilder::buildNewsDataGrid($datagrid);
36
37
        $datagrid->addColumn('actions', 'action', [
38
            'label' => 'admin.news.list.actions',
39
            'field_mapping' => ['id'],
40
            'actions' => [
41
                'edit' => [
42
                    'route_name' => "fsi_admin_crud_edit",
43
                    'additional_parameters' => ['element' => $datagrid->getName()],
44
                    'parameters_field_mapping' => ['id' => 'id']
45
                ],
46
                'display' => [
47
                    'element' => CategoryNewsDisplay::ID
48
                ]
49
            ]
50
        ]);
51
52
        return $datagrid;
53
    }
54
55
    protected function initDataSource(DataSourceFactoryInterface $factory)
56
    {
57
        $queryBuilder = $this->getRepository()
58
            ->createQueryBuilder('n');
59
60
        if ($this->getParentObject()) {
61
            $queryBuilder->where(':category MEMBER OF n.categories')
62
                ->setParameter('category', $this->getParentObject());
63
        }
64
65
        /* @var $datasource \FSi\Component\DataSource\DataSource */
66
        $datasource = $factory->createDataSource('doctrine', ['qb' => $queryBuilder], 'category_news');
67
68
        NewsDataSourceBuilder::buildNewsDataSource($datasource);
69
70
        return $datasource;
71
    }
72
73
    protected function initForm(FormFactoryInterface $factory, $data = null)
74
    {
75
        if ($data === null) {
76
            $data = new \FSi\FixturesBundle\Entity\News();
77
            $data->addCategory($this->getParentObject());
0 ignored issues
show
Documentation introduced by
$this->getParentObject() is of type object|null, but the function expects a object<FSi\FixturesBundle\Entity\Category>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
78
        }
79
80
        return $factory->createNamed('news', new NewsType(), $data);
81
    }
82
}
83