Completed
Pull Request — master (#288)
by Piotr
03:39
created

CategoryNews::initDataSource()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 17
rs 9.4285
cc 2
eloc 9
nc 2
nop 1
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());
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...
79
        }
80
81
        return $factory->createNamed(
82
            'news',
83
            TypeSolver::getFormType('FSi\FixturesBundle\Form\NewsType', new NewsType()),
0 ignored issues
show
Bug introduced by
It seems like \FSi\Bundle\AdminBundle\...Bundle\Form\NewsType()) targeting FSi\Bundle\AdminBundle\F...peSolver::getFormType() can also be of type object<Symfony\Component\Form\FormTypeInterface>; however, Symfony\Component\Form\F...nterface::createNamed() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
84
            $data
85
        );
86
    }
87
}
88