DeleteUserCommand   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 6

Importance

Changes 0
Metric Value
wmc 4
lcom 2
cbo 6
dl 0
loc 46
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A configure() 0 12 1
A execute() 0 17 2
1
<?php
2
3
namespace SumoCoders\FrameworkMultiUserBundle\Console;
4
5
use SumoCoders\FrameworkMultiUserBundle\Command\DeleteUserHandler;
6
use SumoCoders\FrameworkMultiUserBundle\DataTransferObject\Interfaces\UserDataTransferObject;
7
use SumoCoders\FrameworkMultiUserBundle\User\Interfaces\UserRepository;
8
use Symfony\Component\Console\Command\Command;
9
use Symfony\Component\Console\Input\InputArgument;
10
use Symfony\Component\Console\Input\InputInterface;
11
use Symfony\Component\Console\Output\OutputInterface;
12
13
final class DeleteUserCommand extends Command
14
{
15
    /** @var UserRepository */
16
    private $userRepository;
17
18
    /** @var DeleteUserHandler */
19
    private $handler;
20
21
    public function __construct(UserRepository $userRepository, DeleteUserHandler $handler)
22
    {
23
        parent::__construct();
24
        $this->userRepository = $userRepository;
25
        $this->handler = $handler;
26
    }
27
28
    protected function configure(): void
29
    {
30
        $this
31
            ->setName('sumocoders:multiuser:delete')
32
            ->setDescription('Delete a user entity')
33
            ->addArgument(
34
                'username',
35
                InputArgument::REQUIRED,
36
                'The username of the user'
37
            )
38
        ;
39
    }
40
41
    protected function execute(InputInterface $input, OutputInterface $output): void
42
    {
43
        $username = $input->getArgument('username');
44
        $user = $this->userRepository->findByUsername($username);
0 ignored issues
show
Bug introduced by
It seems like $username defined by $input->getArgument('username') on line 43 can also be of type array<integer,string> or null; however, SumoCoders\FrameworkMult...itory::findByUsername() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
45
46
        if (!$user) {
47
            $output->writeln('<error>'.$username.' doesn\'t exists');
48
49
            return;
50
        }
51
52
        $baseUserTransferObject = UserDataTransferObject::fromUser($user);
53
54
        $this->handler->handle($baseUserTransferObject);
55
56
        $output->writeln($username . ' has been deleted');
57
    }
58
}
59