Completed
Push — master ( 56dc59...f8231d )
by Derek Stephen
01:51
created

ClientCommand   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 8

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 5
c 0
b 0
f 0
lcom 2
cbo 8
dl 0
loc 78
ccs 0
cts 42
cp 0
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A configure() 0 6 1
B execute() 0 42 3
1
<?php
2
3
namespace OAuth\Command;
4
5
use Del\Service\UserService;
6
use OAuth\Client;
7
use OAuth\Service\ClientService;
8
use OAuth\User;
9
use Symfony\Component\Console\Command\Command;
10
use Symfony\Component\Console\Input\InputInterface;
11
use Symfony\Component\Console\Output\OutputInterface;
12
use Symfony\Component\Console\Question\ConfirmationQuestion;
13
use Symfony\Component\Console\Question\Question;
14
15
/**
16
 * Class ClientCommand
17
 * @package OAuth\Command
18
 */
19
class ClientCommand extends Command
20
{
21
    /**
22
     * @var ClientService $clientService
23
     */
24
    private $clientService;
25
26
    /**
27
     * @var UserService $userService
28
     */
29
    private $userService;
30
31
    public function __construct(ClientService $clientService, UserService $userService, ?string $name = null)
32
    {
33
        $this->clientService = $clientService;
34
        $this->userService = $userService;
35
        parent::__construct($name);
36
    }
37
38
    /**
39
     * configure options
40
     */
41
    protected function configure()
42
    {
43
        $this->setName('create-client');
44
        $this->setDescription('Creates a new user.');
45
        $this->setHelp('This command allows you to create a user...');
46
    }
47
48
    /**
49
     * @param InputInterface $input
50
     * @param OutputInterface $output
51
     * @return int|null|Client
52
     * @throws \Doctrine\ORM\OptimisticLockException
53
     */
54
    protected function execute(InputInterface $input, OutputInterface $output)
55
    {
56
        $output->writeln('Bone API client creator');
57
        $helper = $this->getHelper('question');
58
59
        $question = new Question('Enter the email of the account: ', false);
60
        $email = $helper->ask($input, $output, $question);
61
62
        $this->userService->setUserClass(User::class);
63
        /** @var User $user */
64
        $user = $this->userService->findUserByEmail($email);
65
66
        if (!$user) {
67
            $output->writeln('User not found. Exiting.');
68
            return null;
69
        }
70
71
        $question = new Question('Give a name for this application: ', false);
72
        $name = $helper->ask($input, $output, $question);
73
74
        $question = new Question('Give a redirect URI: ', false);
75
        $uri = $helper->ask($input, $output, $question);
76
77
        $question = new ConfirmationQuestion('Is this a phone app or JS client ? ', false);
78
        $public = $helper->ask($input, $output, $question);
79
80
81
        $client = new Client();
82
        $client->setName($name);
83
        $client->setIdentifier(md5($name));
84
        $client->setRedirectUri($uri);
85
        $client->setConfidential($public);
86
        $client->setUser($user);
87
88
        if ($public === false) {
89
            $this->clientService->generateSecret($client);
90
        }
91
92
        $this->clientService->getClientRepository()->create($client);
93
94
        return $client;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $client; (OAuth\Client) is incompatible with the return type of the parent method Symfony\Component\Console\Command\Command::execute of type null|integer.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
95
    }
96
}