Skip to content
This repository was archived by the owner on Aug 17, 2022. It is now read-only.

Commit 7d79d55

Browse files
committed
utilise autowiring; refactored DI
1 parent 05282ac commit 7d79d55

12 files changed

+114
-126
lines changed

AuthenticationMethod/AbstractAuthenticationMethod.php

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use Symfony\Component\HttpFoundation\Session\Session;
1919
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
2020
use Symfony\Component\Routing\RouterInterface;
21+
use Zikula\Common\Translator\TranslatorInterface;
2122
use Zikula\Bundle\CoreBundle\HttpKernel\ZikulaKernel;
2223
use Zikula\ExtensionsModule\Api\VariableApi;
2324
use Zikula\OAuthModule\Entity\MappingEntity;
@@ -26,6 +27,11 @@
2627

2728
abstract class AbstractAuthenticationMethod implements ReEntrantAuthenticationMethodInterface
2829
{
30+
/**
31+
* @var TranslatorInterface
32+
*/
33+
protected $translator;
34+
2935
/**
3036
* @var Session
3137
*/
@@ -68,18 +74,22 @@ abstract class AbstractAuthenticationMethod implements ReEntrantAuthenticationMe
6874

6975
/**
7076
* AbstractAuthenticationMethod constructor.
71-
* @param Session $session
77+
*
78+
* @param TranslatorInterface $translator
7279
* @param RequestStack $requestStack
7380
* @param RouterInterface $router
7481
* @param MappingRepository $repository
7582
* @param VariableApi $variableApi
7683
*/
77-
public function __construct(Session $session, RequestStack $requestStack, RouterInterface $router, MappingRepository $repository, VariableApi $variableApi)
78-
{
79-
if (version_compare(ZikulaKernel::VERSION, '1.4.3', '>=') && version_compare(ZikulaKernel::VERSION, '1.5.0', '<')) {
80-
require_once __DIR__ . '/../vendor/autoload.php';
81-
}
82-
$this->session = $session;
84+
public function __construct(
85+
TranslatorInterface $translator,
86+
RequestStack $requestStack,
87+
RouterInterface $router,
88+
MappingRepository $repository,
89+
VariableApi $variableApi
90+
) {
91+
$this->translator = $translator;
92+
$this->session = $requestStack->getCurrentRequest()->getSession();
8393
$this->requestStack = $requestStack;
8494
$this->router = $router;
8595
$this->repository = $repository;
@@ -136,7 +146,7 @@ public function authenticate(array $data = [])
136146
header('Location: ' . $authUrl);
137147
exit;
138148

139-
// Check given state against previously stored one to mitigate CSRF attack
149+
// Check given state against previously stored one to mitigate CSRF attack
140150
} elseif (empty($state) || ($state !== $this->session->get('oauth2state'))) {
141151
$this->session->remove('oauth2state');
142152
$this->session->getFlashBag()->add('error', 'Invalid State');
@@ -158,16 +168,20 @@ public function authenticate(array $data = [])
158168
} else {
159169
$registrationUrl = $this->router->generate('zikulausersmodule_registration_register');
160170
$this->session->remove('oauth2state');
161-
$this->session->getFlashBag()->add('error', sprintf(
162-
'This user is not locally registered. You must first %s on this site before logging in with %s',
163-
'<a href=\'' . $registrationUrl . '\'>create a new account</a>',
164-
$this->getDisplayName()
165-
));
171+
$registerLink = '<a href="' . $registrationUrl . '">' . $this->translator->__('create a new account') . '</a>';
172+
$this->session->getFlashBag()->add('error',
173+
$this->translator->__f(
174+
'This user is not locally registered. You must first %registerLink on this site before logging in with %displayName', [
175+
'%registerLink' => $registerLink,
176+
'%displayName' => $this->getDisplayName()
177+
]
178+
)
179+
);
166180
}
167181

168182
return $uid;
169-
} catch (\Exception $e) {
170-
$this->session->getFlashBag()->add('error', 'Could not obtain user details from Github. (' . $e->getMessage() . ')');
183+
} catch (\Exception $exception) {
184+
$this->session->getFlashBag()->add('error', $this->translator->__('Could not obtain user details from external service.') . ' (' . $exception->getMessage() . ')');
171185

172186
return null;
173187
}
@@ -177,7 +191,7 @@ public function authenticate(array $data = [])
177191
public function getId()
178192
{
179193
if (!$this->user) {
180-
throw new \LogicException('User must authenticate first.');
194+
throw new \LogicException($this->translator->__('User must authenticate first.'));
181195
}
182196

183197
return $this->user->getId();

AuthenticationMethod/FacebookAuthenticationMethod.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,12 @@ public function getAlias()
2424

2525
public function getDisplayName()
2626
{
27-
return 'Facebook';
27+
return $this->translator->__('Facebook');
2828
}
2929

3030
public function getDescription()
3131
{
32-
return 'Login using Facebook via OAuth.';
32+
return $this->translator->__('Login using Facebook via OAuth.');
3333
}
3434

3535
public function getUname()
@@ -46,14 +46,14 @@ protected function setProvider($redirectUri)
4646
{
4747
$settings = $this->variableApi->get('ZikulaOAuthModule', OAuthConstant::ALIAS_FACEBOOK);
4848
if (!isset($settings['id']) || !isset($settings['secret'])) {
49-
throw new InvalidProviderConfigException('Invalid settings for Facebook OAuth provider.');
49+
throw new InvalidProviderConfigException($this->translator->__('Invalid settings for Facebook OAuth provider.'));
5050
}
5151

5252
$this->provider = new Facebook([
5353
'clientId' => $settings['id'],
5454
'clientSecret' => $settings['secret'],
5555
'redirectUri' => $redirectUri,
56-
'graphApiVersion' => 'v2.9',
56+
'graphApiVersion' => 'v2.9'
5757
]);
5858
}
5959
}

AuthenticationMethod/GithubAuthenticationMethod.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,12 @@ public function getAlias()
2929

3030
public function getDisplayName()
3131
{
32-
return 'Github';
32+
return $this->translator->__('Github');
3333
}
3434

3535
public function getDescription()
3636
{
37-
return 'Login using Github via OAuth.';
37+
return $this->translator->__('Login using Github via OAuth.');
3838
}
3939

4040
public function getUname()
@@ -77,13 +77,13 @@ protected function setProvider($redirectUri)
7777
{
7878
$settings = $this->variableApi->get('ZikulaOAuthModule', OAuthConstant::ALIAS_GITHUB);
7979
if (!isset($settings['id']) || !isset($settings['secret'])) {
80-
throw new InvalidProviderConfigException('Invalid settings for Github OAuth provider.');
80+
throw new InvalidProviderConfigException($this->translator->__('Invalid settings for Github OAuth provider.'));
8181
}
8282

8383
$this->provider = new Github([
8484
'clientId' => $settings['id'],
8585
'clientSecret' => $settings['secret'],
86-
'redirectUri' => $redirectUri,
86+
'redirectUri' => $redirectUri
8787
]);
8888
}
8989
}

AuthenticationMethod/GoogleAuthenticationMethod.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,12 @@ public function getAlias()
2424

2525
public function getDisplayName()
2626
{
27-
return 'Google';
27+
return $this->translator->__('Google');
2828
}
2929

3030
public function getDescription()
3131
{
32-
return 'Login using Google via OAuth.';
32+
return $this->translator->__('Login using Google via OAuth.');
3333
}
3434

3535
public function getUname()
@@ -46,13 +46,13 @@ protected function setProvider($redirectUri)
4646
{
4747
$settings = $this->variableApi->get('ZikulaOAuthModule', OAuthConstant::ALIAS_GOOGLE);
4848
if (!isset($settings['id']) || !isset($settings['secret'])) {
49-
throw new InvalidProviderConfigException('Invalid settings for Google OAuth provider.');
49+
throw new InvalidProviderConfigException($this->translator->__('Invalid settings for Google OAuth provider.'));
5050
}
5151

5252
$this->provider = new Google([
5353
'clientId' => $settings['id'],
5454
'clientSecret' => $settings['secret'],
55-
'redirectUri' => $redirectUri,
55+
'redirectUri' => $redirectUri
5656
]);
5757
}
5858
}

AuthenticationMethod/LinkedInAuthenticationMethod.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,12 @@ public function getAlias()
2424

2525
public function getDisplayName()
2626
{
27-
return 'LinkedIn';
27+
return $this->translator->__('LinkedIn');
2828
}
2929

3030
public function getDescription()
3131
{
32-
return 'Login using LinkedIn via OAuth.';
32+
return $this->translator->__('Login using LinkedIn via OAuth.');
3333
}
3434

3535
public function getUname()
@@ -46,13 +46,13 @@ protected function setProvider($redirectUri)
4646
{
4747
$settings = $this->variableApi->get('ZikulaOAuthModule', OAuthConstant::ALIAS_LINKEDIN);
4848
if (!isset($settings['id']) || !isset($settings['secret'])) {
49-
throw new InvalidProviderConfigException('Invalid settings for LinkedIn OAuth provider.');
49+
throw new InvalidProviderConfigException($this->translator->__('Invalid settings for LinkedIn OAuth provider.'));
5050
}
5151

5252
$this->provider = new LinkedIn([
5353
'clientId' => $settings['id'],
5454
'clientSecret' => $settings['secret'],
55-
'redirectUri' => $redirectUri,
55+
'redirectUri' => $redirectUri
5656
]);
5757
}
5858
}

Controller/ConfigController.php

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?php
1+
// <?php
22

33
/*
44
* This file is part of the Zikula package.
@@ -11,12 +11,14 @@
1111

1212
namespace Zikula\OAuthModule\Controller;
1313

14+
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
1415
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
1516
use Symfony\Component\HttpFoundation\Request;
16-
use Symfony\Component\Routing\Annotation\Route;
1717
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
1818
use Zikula\Core\Controller\AbstractController;
19+
use Zikula\OAuthModule\Form\Type\SettingsType;
1920
use Zikula\ThemeModule\Engine\Annotation\Theme;
21+
use Zikula\UsersModule\Collector\AuthenticationMethodCollector;
2022

2123
/**
2224
* Class ConfigController
@@ -27,20 +29,23 @@ class ConfigController extends AbstractController
2729
* @Route("/settings/{method}")
2830
* @Theme("admin")
2931
* @Template("ZikulaOAuthModule:Config:settings.html.twig")
32+
*
3033
* @param Request $request
34+
* @param AuthenticationMethodCollector $authenticationMethodCollector
3135
* @param string $method
36+
*
3237
* @return array
3338
*/
34-
public function settingsAction(Request $request, $method = 'github')
35-
{
39+
public function settingsAction(
40+
Request $request,
41+
AuthenticationMethodCollector $authenticationMethodCollector,
42+
$method = 'github'
43+
) {
3644
if (!$this->hasPermission('ZikulaOAuthModule', '::', ACCESS_ADMIN)) {
3745
throw new AccessDeniedException();
3846
}
3947

40-
$form = $this->createForm('Zikula\OAuthModule\Form\Type\SettingsType', $this->getVar($method, []), [
41-
'translator' => $this->getTranslator()
42-
]);
43-
48+
$form = $this->createForm(SettingsType::class, $this->getVar($method, []));
4449
$form->handleRequest($request);
4550
if ($form->isSubmitted() && $form->isValid()) {
4651
if ($form->get('save')->isClicked()) {
@@ -51,7 +56,7 @@ public function settingsAction(Request $request, $method = 'github')
5156

5257
return [
5358
'form' => $form->createView(),
54-
'method' => $this->get('zikula_users_module.internal.authentication_method_collector')->get($method)
59+
'method' => $authenticationMethodCollector->get($method)
5560
];
5661
}
5762
}

Controller/MappingController.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
1717
use Zikula\Core\Controller\AbstractController;
1818
use Zikula\ThemeModule\Engine\Annotation\Theme;
19+
use Zikula\OAuthModule\Entity\Repository\MappingRepository;
1920

2021
/**
2122
* Class MappingController
@@ -26,16 +27,17 @@ class MappingController extends AbstractController
2627
* @Route("/list")
2728
* @Template("ZikulaOAuthModule:Mapping:list.html.twig")
2829
* @Theme("admin")
30+
*
31+
* @param MappingRepository $mappingRepository
2932
*/
30-
public function listAction()
33+
public function listAction(MappingRepository $mappingRepository)
3134
{
3235
if (!$this->hasPermission('ZikulaOAuthModule', '::', ACCESS_ADMIN)) {
3336
throw new AccessDeniedException();
3437
}
35-
$mappings = $this->get('zikula_oauth_module.mapping_repository')->findAll();
3638

3739
return [
38-
'mappings' => $mappings
40+
'mappings' => $mappingRepository->findAll()
3941
];
4042
}
4143
}

Entity/Repository/MappingRepository.php

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,22 @@
1111

1212
namespace Zikula\OAuthModule\Entity\Repository;
1313

14-
use Doctrine\ORM\EntityRepository;
14+
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
15+
use Doctrine\Common\Persistence\ManagerRegistry;
1516
use Zikula\OAuthModule\Entity\MappingEntity;
1617

17-
class MappingRepository extends EntityRepository
18+
class MappingRepository extends ServiceEntityRepository
1819
{
20+
public function __construct(ManagerRegistry $registry)
21+
{
22+
parent::__construct($registry, MappingEntity::class);
23+
}
24+
1925
public function getZikulaId($method, $methodId)
2026
{
2127
$mapping = parent::findOneBy(['method' => $method, 'methodId' => $methodId]);
2228

23-
if (isset($mapping)) {
24-
return $mapping->getZikulaId();
25-
} else {
26-
return null;
27-
}
29+
return isset($mapping) ? $mapping->getZikulaId() : null;
2830
}
2931

3032
public function persistAndFlush(MappingEntity $entity)

0 commit comments

Comments
 (0)