Completed
Pull Request — master (#1073)
by Vincent
01:29
created

AuditBlockService::execute()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 17
rs 9.7
c 0
b 0
f 0
cc 2
nc 2
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Sonata Project package.
7
 *
8
 * (c) Thomas Rabaix <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Sonata\DoctrineORMAdminBundle\Block;
15
16
use SimpleThings\EntityAudit\AuditReader;
17
use Sonata\BlockBundle\Block\BlockContextInterface;
18
use Sonata\BlockBundle\Block\Service\AbstractBlockService;
19
use Symfony\Component\HttpFoundation\Response;
20
use Symfony\Component\OptionsResolver\OptionsResolver;
21
use Twig\Environment;
22
23
/**
24
 * @final since sonata-project/doctrine-orm-admin-bundle 3.x
25
 *
26
 * @author Thomas Rabaix <[email protected]>
27
 */
28
class AuditBlockService extends AbstractBlockService
29
{
30
    /**
31
     * @var AuditReader
32
     */
33
    protected $auditReader;
34
35
    public function __construct(Environment $twig, AuditReader $auditReader)
36
    {
37
        parent::__construct($twig);
38
39
        $this->auditReader = $auditReader;
40
    }
41
42
    public function execute(BlockContextInterface $blockContext, ?Response $response = null): Response
43
    {
44
        $revisions = [];
45
46
        foreach ($this->auditReader->findRevisionHistory($blockContext->getSetting('limit'), 0) as $revision) {
47
            $revisions[] = [
48
                'revision' => $revision,
49
                'entities' => $this->auditReader->findEntitiesChangedAtRevision($revision->getRev()),
50
            ];
51
        }
52
53
        return $this->renderResponse($blockContext->getTemplate(), [
54
            'block' => $blockContext->getBlock(),
55
            'settings' => $blockContext->getSettings(),
56
            'revisions' => $revisions,
57
        ], $response);
58
    }
59
60
    public function configureSettings(OptionsResolver $resolver): void
61
    {
62
        $resolver->setDefaults([
63
            'limit' => 10,
64
            'template' => '@SonataDoctrineORMAdmin/Block/block_audit.html.twig',
65
        ]);
66
    }
67
}
68