| Conditions | 15 |
| Paths | 34 |
| Total Lines | 74 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 97 | public function execute(InputInterface $input, OutputInterface $output) |
||
| 98 | { |
||
| 99 | $output->writeln('Welcome to the AdminBundle object ACL generator'); |
||
| 100 | $output->writeln([ |
||
| 101 | '', |
||
| 102 | 'This command helps you to generate ACL entities for the objects handled by the AdminBundle.', |
||
| 103 | '', |
||
| 104 | 'If the step option is used, you will be asked if you want to generate the object ACL entities for each Admin.', |
||
| 105 | 'You must use the shortcut notation like <comment>AcmeDemoBundle:User</comment> if you want to set an object owner.', |
||
| 106 | '', |
||
| 107 | ]); |
||
| 108 | |||
| 109 | if (!$this->registry) { |
||
| 110 | $msg = sprintf('The command "%s" has a dependency on a non-existent service "doctrine".', static::$defaultName); |
||
| 111 | |||
| 112 | throw new ServiceNotFoundException('doctrine', static::class, null, [], $msg); |
||
| 113 | } |
||
| 114 | |||
| 115 | if ($input->getOption('user_entity')) { |
||
| 116 | try { |
||
| 117 | $this->getUserEntityClass($input, $output); |
||
| 118 | } catch (\Exception $e) { |
||
| 119 | $output->writeln(sprintf('<error>%s</error>', $e->getMessage())); |
||
| 120 | |||
| 121 | return; |
||
| 122 | } |
||
| 123 | } |
||
| 124 | |||
| 125 | if (!$this->aclObjectManipulators) { |
||
| 126 | $output->writeln('No manipulators are implemented : <info>ignoring</info>'); |
||
| 127 | |||
| 128 | return; |
||
| 129 | } |
||
| 130 | |||
| 131 | foreach ($this->pool->getAdminServiceIds() as $id) { |
||
| 132 | try { |
||
| 133 | $admin = $this->pool->getInstance($id); |
||
| 134 | } catch (\Exception $e) { |
||
| 135 | $output->writeln('<error>Warning : The admin class cannot be initiated from the command line</error>'); |
||
| 136 | $output->writeln(sprintf('<error>%s</error>', $e->getMessage())); |
||
| 137 | |||
| 138 | continue; |
||
| 139 | } |
||
| 140 | |||
| 141 | if ($input->getOption('step') && !$this->askConfirmation($input, $output, sprintf("<question>Generate ACLs for the object instances handled by \"%s\"?</question>\n", $id), 'no', '?')) { |
||
| 142 | continue; |
||
| 143 | } |
||
| 144 | |||
| 145 | $securityIdentity = null; |
||
| 146 | if ($input->getOption('step') && $this->askConfirmation($input, $output, "<question>Set an object owner?</question>\n", 'no', '?')) { |
||
| 147 | $username = $this->askAndValidate($input, $output, 'Please enter the username: ', '', 'Sonata\AdminBundle\Command\Validators::validateUsername'); |
||
| 148 | |||
| 149 | $securityIdentity = new UserSecurityIdentity($username, $this->getUserEntityClass($input, $output)); |
||
| 150 | } |
||
| 151 | if (!$input->getOption('step') && $input->getOption('object_owner')) { |
||
| 152 | $securityIdentity = new UserSecurityIdentity($input->getOption('object_owner'), $this->getUserEntityClass($input, $output)); |
||
| 153 | } |
||
| 154 | |||
| 155 | $manipulatorId = sprintf('sonata.admin.manipulator.acl.object.%s', $admin->getManagerType()); |
||
| 156 | if (!$manipulator = $this->aclObjectManipulators[$manipulatorId] ?? null) { |
||
| 157 | $output->writeln('Admin class is using a manager type that has no manipulator implemented : <info>ignoring</info>'); |
||
| 158 | |||
| 159 | continue; |
||
| 160 | } |
||
| 161 | if (!$manipulator instanceof ObjectAclManipulatorInterface) { |
||
| 162 | $output->writeln(sprintf('The interface "ObjectAclManipulatorInterface" is not implemented for %s: <info>ignoring</info>', \get_class($manipulator))); |
||
| 163 | |||
| 164 | continue; |
||
| 165 | } |
||
| 166 | |||
| 167 | \assert($admin instanceof AdminInterface); |
||
| 168 | $manipulator->batchConfigureAcls($output, $admin, $securityIdentity); |
||
| 169 | } |
||
| 170 | } |
||
| 171 | |||
| 194 |
If you suppress an error, we recommend checking for the error condition explicitly: