Completed
Push — 3.x ( 1cd1d0...b03505 )
by Grégoire
16:27 queued 03:11
created

SetupAclCommand::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 1
nc 1
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\AdminBundle\Command;
15
16
use Sonata\AdminBundle\Admin\AdminInterface;
17
use Sonata\AdminBundle\Admin\Pool;
18
use Sonata\AdminBundle\Util\AdminAclManipulatorInterface;
19
use Symfony\Component\Console\Command\Command;
20
use Symfony\Component\Console\Input\InputInterface;
21
use Symfony\Component\Console\Output\OutputInterface;
22
23
/**
24
 * @author Thomas Rabaix <[email protected]>
25
 */
26
class SetupAclCommand extends Command
27
{
28
    /**
29
     * {@inheritdoc}
30
     */
31
    protected static $defaultName = 'sonata:admin:setup-acl';
32
33
    /**
34
     * @var Pool
35
     */
36
    private $pool;
37
38
    /**
39
     * @var AdminAclManipulatorInterface
40
     */
41
    private $aclManipulator;
42
43
    public function __construct(Pool $pool, AdminAclManipulatorInterface $aclManipulator)
44
    {
45
        $this->pool = $pool;
46
        $this->aclManipulator = $aclManipulator;
47
48
        parent::__construct();
49
    }
50
51
    public function configure()
52
    {
53
        $this->setDescription('Install ACL for Admin Classes');
54
    }
55
56
    public function execute(InputInterface $input, OutputInterface $output)
57
    {
58
        $output->writeln('Starting ACL AdminBundle configuration');
59
60
        foreach ($this->pool->getAdminServiceIds() as $id) {
61
            try {
62
                $admin = $this->pool->getInstance($id);
63
            } catch (\Exception $e) {
64
                $output->writeln('<error>Warning : The admin class cannot be initiated from the command line</error>');
65
                $output->writeln(sprintf('<error>%s</error>', $e->getMessage()));
66
67
                continue;
68
            }
69
70
            \assert($admin instanceof AdminInterface);
71
            $this->aclManipulator->configureAcls($output, $admin);
72
        }
73
    }
74
}
75