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; |
|
|
|
|
95
|
|
|
} |
96
|
|
|
} |
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:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.