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

AuditBlockService   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 5
dl 0
loc 40
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A execute() 0 17 2
A configureSettings() 0 7 1
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