Skip to content

Commit f167268

Browse files
alanpoulaindkarlovi
authored andcommitted
Fix: DI-related issues (#548)
* Use EventDispatcherInterface instead of EventDispatcher * Remove container from AuthorizeController * Session can be null in AuthorizeController * Remove BaseCommand and inject dependencies for commands * Fix comment * Fix tests for commands * Fix tests for AuthorizeController
1 parent 5aee442 commit f167268

File tree

9 files changed

+106
-186
lines changed

9 files changed

+106
-186
lines changed

Command/BaseCommand.php

Lines changed: 0 additions & 40 deletions
This file was deleted.

Command/CleanCommand.php

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,30 @@
1111

1212
namespace FOS\OAuthServerBundle\Command;
1313

14+
use Symfony\Component\Console\Command\Command;
1415
use Symfony\Component\Console\Input\InputInterface;
1516
use Symfony\Component\Console\Output\OutputInterface;
1617
use FOS\OAuthServerBundle\Model\TokenManagerInterface;
1718
use FOS\OAuthServerBundle\Model\AuthCodeManagerInterface;
1819

19-
class CleanCommand extends BaseCommand
20+
class CleanCommand extends Command
2021
{
22+
private $accessTokenManager;
23+
private $refreshTokenManager;
24+
private $authCodeManager;
25+
26+
public function __construct(
27+
TokenManagerInterface $accessTokenManager,
28+
TokenManagerInterface $refreshTokenManager,
29+
AuthCodeManagerInterface $authCodeManager)
30+
{
31+
parent::__construct();
32+
33+
$this->accessTokenManager = $accessTokenManager;
34+
$this->refreshTokenManager = $refreshTokenManager;
35+
$this->authCodeManager = $authCodeManager;
36+
}
37+
2138
/**
2239
* {@inheritdoc}
2340
*/
@@ -41,19 +58,9 @@ protected function configure()
4158
*/
4259
protected function execute(InputInterface $input, OutputInterface $output)
4360
{
44-
$services = [
45-
'fos_oauth_server.access_token_manager' => 'Access token',
46-
'fos_oauth_server.refresh_token_manager' => 'Refresh token',
47-
'fos_oauth_server.auth_code_manager' => 'Auth code',
48-
];
49-
50-
foreach ($services as $service => $name) {
51-
/** @var $instance TokenManagerInterface */
52-
$instance = $this->getContainer()->get($service);
53-
if ($instance instanceof TokenManagerInterface || $instance instanceof AuthCodeManagerInterface) {
54-
$result = $instance->deleteExpired();
55-
$output->writeln(sprintf('Removed <info>%d</info> items from <comment>%s</comment> storage.', $result, $name));
56-
}
61+
foreach ([$this->accessTokenManager, $this->refreshTokenManager, $this->authCodeManager] as $service) {
62+
$result = $service->deleteExpired();
63+
$output->writeln(sprintf('Removed <info>%d</info> items from <comment>%s</comment> storage.', $result, get_class($service)));
5764
}
5865
}
5966
}

Command/CreateClientCommand.php

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,24 @@
1111

1212
namespace FOS\OAuthServerBundle\Command;
1313

14+
use Symfony\Component\Console\Command\Command;
1415
use Symfony\Component\Console\Input\InputOption;
1516
use Symfony\Component\Console\Style\SymfonyStyle;
1617
use Symfony\Component\Console\Input\InputInterface;
1718
use Symfony\Component\Console\Output\OutputInterface;
1819
use FOS\OAuthServerBundle\Model\ClientManagerInterface;
1920

20-
class CreateClientCommand extends BaseCommand
21+
class CreateClientCommand extends Command
2122
{
23+
private $clientManager;
24+
25+
public function __construct(ClientManagerInterface $clientManager)
26+
{
27+
parent::__construct();
28+
29+
$this->clientManager = $clientManager;
30+
}
31+
2232
/**
2333
* {@inheritdoc}
2434
*/
@@ -61,18 +71,14 @@ protected function execute(InputInterface $input, OutputInterface $output)
6171

6272
$io->title('Client Credentials');
6373

64-
// Get the client manager
65-
/** @var ClientManagerInterface $clientManager */
66-
$clientManager = $this->getContainer()->get('fos_oauth_server.client_manager.default');
67-
6874
// Create a new client
69-
$client = $clientManager->createClient();
75+
$client = $this->clientManager->createClient();
7076

7177
$client->setRedirectUris($input->getOption('redirect-uri'));
7278
$client->setAllowedGrantTypes($input->getOption('grant-type'));
7379

7480
// Save the client
75-
$clientManager->updateClient($client);
81+
$this->clientManager->updateClient($client);
7682

7783
// Give the credentials back to the user
7884
$headers = ['Client ID', 'Client Secret'];

Controller/AuthorizeController.php

Lines changed: 20 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,8 @@
1717
use FOS\OAuthServerBundle\Model\ClientManagerInterface;
1818
use OAuth2\OAuth2;
1919
use OAuth2\OAuth2ServerException;
20-
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
21-
use Symfony\Component\DependencyInjection\ContainerInterface;
2220
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface;
23-
use Symfony\Component\EventDispatcher\EventDispatcher;
21+
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
2422
use Symfony\Component\Form\Form;
2523
use Symfony\Component\HttpFoundation\Request;
2624
use Symfony\Component\HttpFoundation\RequestStack;
@@ -37,18 +35,13 @@
3735
*
3836
* @author Chris Jones <[email protected]>
3937
*/
40-
class AuthorizeController implements ContainerAwareInterface
38+
class AuthorizeController
4139
{
4240
/**
4341
* @var ClientInterface
4442
*/
4543
private $client;
4644

47-
/**
48-
* @var ContainerInterface
49-
*/
50-
protected $container;
51-
5245
/**
5346
* @var SessionInterface
5447
*/
@@ -100,48 +93,38 @@ class AuthorizeController implements ContainerAwareInterface
10093
private $templateEngineType;
10194

10295
/**
103-
* @var EventDispatcher
96+
* @var EventDispatcherInterface
10497
*/
10598
private $eventDispatcher;
10699

107-
/**
108-
* Sets the container.
109-
*
110-
* @param ContainerInterface|null $container A ContainerInterface instance or null
111-
*/
112-
public function setContainer(ContainerInterface $container = null)
113-
{
114-
$this->container = $container;
115-
}
116-
117100
/**
118101
* This controller had been made as a service due to support symfony 4 where all* services are private by default.
119-
* Thus, there is considered a bad practice to fetch services directly from container.
120-
* @todo This controller could be refactored to do not rely on so many dependencies
102+
* Thus, this is considered a bad practice to fetch services directly from container.
103+
* @todo This controller could be refactored to not rely on so many dependencies
121104
*
122-
* @param RequestStack $requestStack
123-
* @param SessionInterface $session
124-
* @param Form $authorizeForm
125-
* @param AuthorizeFormHandler $authorizeFormHandler
126-
* @param OAuth2 $oAuth2Server
127-
* @param EngineInterface $templating
128-
* @param TokenStorageInterface $tokenStorage
129-
* @param UrlGeneratorInterface $router
130-
* @param ClientManagerInterface $clientManager
131-
* @param EventDispatcher $eventDispatcher
132-
* @param string $templateEngineType
105+
* @param RequestStack $requestStack
106+
* @param Form $authorizeForm
107+
* @param AuthorizeFormHandler $authorizeFormHandler
108+
* @param OAuth2 $oAuth2Server
109+
* @param EngineInterface $templating
110+
* @param TokenStorageInterface $tokenStorage
111+
* @param UrlGeneratorInterface $router
112+
* @param ClientManagerInterface $clientManager
113+
* @param EventDispatcherInterface $eventDispatcher
114+
* @param SessionInterface $session
115+
* @param string $templateEngineType
133116
*/
134117
public function __construct(
135118
RequestStack $requestStack,
136-
SessionInterface $session,
137119
Form $authorizeForm,
138120
AuthorizeFormHandler $authorizeFormHandler,
139121
OAuth2 $oAuth2Server,
140122
EngineInterface $templating,
141123
TokenStorageInterface $tokenStorage,
142124
UrlGeneratorInterface $router,
143125
ClientManagerInterface $clientManager,
144-
EventDispatcher $eventDispatcher,
126+
EventDispatcherInterface $eventDispatcher,
127+
SessionInterface $session = null,
145128
$templateEngineType = 'twig'
146129
) {
147130
$this->requestStack = $requestStack;
@@ -168,7 +151,7 @@ public function authorizeAction(Request $request)
168151
throw new AccessDeniedException('This user does not have access to this section.');
169152
}
170153

171-
if (true === $this->session->get('_fos_oauth_server.ensure_logout')) {
154+
if ($this->session && true === $this->session->get('_fos_oauth_server.ensure_logout')) {
172155
$this->session->invalidate(600);
173156
$this->session->set('_fos_oauth_server.ensure_logout', true);
174157
}
@@ -209,7 +192,7 @@ public function authorizeAction(Request $request)
209192
*/
210193
protected function processSuccess(UserInterface $user, AuthorizeFormHandler $formHandler, Request $request)
211194
{
212-
if (true === $this->session->get('_fos_oauth_server.ensure_logout')) {
195+
if ($this->session && true === $this->session->get('_fos_oauth_server.ensure_logout')) {
213196
$this->tokenStorage->setToken(null);
214197
$this->session->invalidate();
215198
}

Resources/config/authorize.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525

2626
<service id="fos_oauth_server.controller.authorize" class="FOS\OAuthServerBundle\Controller\AuthorizeController" public="true">
2727
<argument type="service" id="request_stack" />
28-
<argument type="service" id="session" />
2928
<argument type="service" id="fos_oauth_server.authorize.form" />
3029
<argument type="service" id="fos_oauth_server.authorize.form.handler" />
3130
<argument type="service" id="fos_oauth_server.server" />
@@ -34,6 +33,7 @@
3433
<argument type="service" id="router" />
3534
<argument type="service" id="fos_oauth_server.client_manager" />
3635
<argument type="service" id="event_dispatcher" />
36+
<argument type="service" id="session" on-invalid="null" />
3737
<argument>%fos_oauth_server.template.engine%</argument>
3838
</service>
3939
</services>

Resources/config/oauth.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,14 @@
3030
<service id="fos_oauth_server.controller.token" alias="FOS\OAuthServerBundle\Controller\TokenController" public="true" />
3131

3232
<service id="fos_oauth_server.clean_command" class="FOS\OAuthServerBundle\Command\CleanCommand">
33+
<argument type="service" id="fos_oauth_server.access_token_manager" />
34+
<argument type="service" id="fos_oauth_server.refresh_token_manager" />
35+
<argument type="service" id="fos_oauth_server.auth_code_manager" />
3336
<tag name="console.command" />
3437
</service>
3538

3639
<service id="fos_oauth_server.create_client_command" class="FOS\OAuthServerBundle\Command\CreateClientCommand">
40+
<argument type="service" id="fos_oauth_server.client_manager.default" />
3741
<tag name="console.command" />
3842
</service>
3943
</services>

0 commit comments

Comments
 (0)