Passed
Push — develop ( 0cdda4...a8b917 )
by BENARD
02:02
created

ArticleAdmin::prePersist()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace ProjetNormandie\ArticleBundle\Admin;
4
5
use A2lix\TranslationFormBundle\Form\Type\TranslationsType;
6
use DateTime;
7
use Sonata\AdminBundle\Admin\AbstractAdmin;
0 ignored issues
show
Bug introduced by
The type Sonata\AdminBundle\Admin\AbstractAdmin was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
use Sonata\AdminBundle\Datagrid\ProxyQueryInterface;
0 ignored issues
show
Bug introduced by
The type Sonata\AdminBundle\Datagrid\ProxyQueryInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
9
use Sonata\AdminBundle\Show\ShowMapper;
0 ignored issues
show
Bug introduced by
The type Sonata\AdminBundle\Show\ShowMapper was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
10
use Sonata\AdminBundle\Form\FormMapper;
0 ignored issues
show
Bug introduced by
The type Sonata\AdminBundle\Form\FormMapper was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
11
use Sonata\AdminBundle\Form\Type\ModelAutocompleteType;
0 ignored issues
show
Bug introduced by
The type Sonata\AdminBundle\Form\Type\ModelAutocompleteType was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
12
use Sonata\AdminBundle\Datagrid\ListMapper;
0 ignored issues
show
Bug introduced by
The type Sonata\AdminBundle\Datagrid\ListMapper was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
13
use Sonata\AdminBundle\Datagrid\DatagridMapper;
0 ignored issues
show
Bug introduced by
The type Sonata\AdminBundle\Datagrid\DatagridMapper was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
14
use Sonata\AdminBundle\Route\RouteCollectionInterface;
0 ignored issues
show
Bug introduced by
The type Sonata\AdminBundle\Route\RouteCollectionInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
15
use Sonata\DoctrineORMAdminBundle\Filter\ModelFilter;
0 ignored issues
show
Bug introduced by
The type Sonata\DoctrineORMAdminBundle\Filter\ModelFilter was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
16
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
17
use Symfony\Component\Form\Extension\Core\Type\TextType;
18
use Symfony\Component\Form\Extension\Core\Type\DateTimeType;
19
use ProjetNormandie\ArticleBundle\Entity\Article;
20
use FOS\CKEditorBundle\Form\Type\CKEditorType;
21
use Doctrine\ORM\EntityManager;
22
use Symfony\Component\Security\Core\Security;
23
24
/**
25
 * Administration manager for the Article Bundle.
26
 */
27
class ArticleAdmin extends AbstractAdmin
28
{
29
    protected $baseRouteName = 'pnarticlebundle_admin_article';
30
31
    private Security $security;
32
33
    public function setSecurity(Security $security)
34
    {
35
        $this->security = $security;
36
    }
37
38
    /**
39
     * @param RouteCollectionInterface $collection
40
     */
41
    protected function configureRoutes(RouteCollectionInterface $collection): void
42
    {
43
        $collection->remove('export');
44
    }
45
46
    protected function configureDefaultSortValues(array &$sortValues): void
47
    {
48
        $sortValues['_page'] = 1;
49
        $sortValues['_sort_order'] = 'DESC';
50
        $sortValues['_sort_by'] = 'id';
51
    }
52
53
    /**
54
     * @param ProxyQueryInterface $query
55
     * @return ProxyQueryInterface
56
     */
57
    protected function configureQuery(ProxyQueryInterface $query): ProxyQueryInterface
58
    {
59
        $query = parent::configureQuery($query);
60
        $query->leftJoin($query->getRootAliases()[0]  . '.translations', 't')
61
            ->addSelect('t');
62
        return $query;
63
    }
64
65
66
    /**
67
     * @param FormMapper $form
68
     */
69
    protected function configureFormFields(FormMapper $form): void
70
    {
71
        $form
72
            ->add('id', TextType::class, ['label' => 'label.id', 'attr' => ['readonly' => true]])
73
            ->add(
74
                'status',
75
                ChoiceType::class,
76
                [
77
                    'label' => 'label.status',
78
                    'choices' => Article::getStatusChoices(),
79
                ]
80
            )
81
            ->add('publishedAt', DateTimeType::class, [
82
                'label' => 'label.publishedAt',
83
                'required' => false,
84
                'years' => range(2004, date('Y'))
85
            ])
86
            ->add('translations', TranslationsType::class, [
87
                'required' => true,
88
                'fields' => [
89
                    'title' => [
90
                        'field_type' => TextType::class,
91
                        'label' => 'label.title',
92
                    ],
93
                    'text' => [
94
                        'field_type' => CKEditorType::class,
95
                        'label' => 'label.text',
96
                    ]
97
                ]
98
            ]);
99
    }
100
101
    /**
102
     * @param DatagridMapper $filter
103
     */
104
    protected function configureDatagridFilters(DatagridMapper $filter): void
105
    {
106
        $filter
107
            ->add(
108
                'author', ModelFilter::class, [
109
                    'label' => 'label.author',
110
                    'field_type' => ModelAutocompleteType::class,
111
                    'field_options' => ['property' => 'username'],
112
                ]
113
            )
114
            ->add('translations.title', null, ['label' => 'label.title'])
115
            ->add('status', null, ['label' => 'label.status']);
116
    }
117
118
    /**
119
     * @param ListMapper $list
120
     */
121
    protected function configureListFields(ListMapper $list): void
122
    {
123
        $list->addIdentifier('id', null, ['label' => 'label.id'])
124
            ->add('getDefaultTitle', null, ['label' => 'label.title'])
125
            ->add('author', null, ['label' => 'label.author'])
126
            ->add(
127
                'status',
128
                'choice',
129
                [
130
                    'label' => 'label.status',
131
                    'editable' => false,
132
                    'choices' => Article::getStatusChoices(),
133
                ]
134
            )
135
            ->add('createdAt', null, ['label' => 'label.createdAt'])
136
            ->add('published_at', 'datetime', ['label' => 'label.publishedAt'])
137
            ->add('_action', 'actions', [
138
                'actions' => [
139
                    'show' => [],
140
                    'edit' => [],
141
                    'groups' => [
142
                        'template' => '@ProjetNormandieArticle/Admin/article_comments_link.html.twig'
143
                    ],
144
                ]
145
            ]);
146
    }
147
148
    /**
149
     * @param ShowMapper $show
150
     */
151
    protected function configureShowFields(ShowMapper $show): void
152
    {
153
        $show
154
            ->add('id', null, ['label' => 'label.id'])
155
            ->add('status', null, ['label' => 'label.status'])
156
            ->add('getDefaultTitle', null, ['label' => 'label.title'])
157
            ->add('getDefaultText', null, ['label' => 'label.text', 'safe' => true]);
158
    }
159
160
161
    /**
162
     * @param object $object
163
     */
164
    public function prePersist(object $object): void
165
    {
166
        $object->setAuthor($this->security->getUser());
167
        if ($object->getStatus() === Article::STATUS_PUBLISHED) {
168
            $object->setPublishedAt(new DateTime());
169
        }
170
    }
171
172
    /**
173
     * @param object $object
174
     */
175
    public function preUpdate(object $object): void
176
    {
177
        /** @var EntityManager $em */
178
        $em = $this->getModelManager()->getEntityManager($this->getClass());
179
        $originalObject = $em->getUnitOfWork()->getOriginalEntityData($object);
180
181
        // PUBLISHED
182
        if ($originalObject['status'] === Article::STATUS_UNDER_CONSTRUCTION
183
            && $object->getStatus() === Article::STATUS_PUBLISHED) {
184
            $object->setPublishedAt(new DateTime());
185
        }
186
    }
187
}
188