Completed
Pull Request — 3.1 (#347)
by Łukasz
16:49 queued 11:36
created

CategoryNews   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

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

6 Methods

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