Skip to content
This repository has been archived by the owner on Dec 2, 2021. It is now read-only.

Add lazy Renderer service to speedup page load time #66

Merged
merged 7 commits into from Aug 17, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions Resources/config/security.xml
Expand Up @@ -12,6 +12,7 @@
<parameter key="scheb_two_factor.security_voter.class">Scheb\TwoFactorBundle\Security\TwoFactor\Voter</parameter>
<parameter key="scheb_two_factor.authentication_context.class">Scheb\TwoFactorBundle\Security\TwoFactor\AuthenticationContext</parameter>
<parameter key="scheb_two_factor.authentication_context_factory.class">Scheb\TwoFactorBundle\Security\TwoFactor\AuthenticationContextFactory</parameter>
<parameter key="scheb_two_factor.security.renderer.class">Scheb\TwoFactorBundle\Security\TwoFactor\Renderer</parameter>
</parameters>
<services>
<service id="scheb_two_factor.session_flag_manager" class="%scheb_two_factor.session_flag_manager.class%">
Expand Down
7 changes: 5 additions & 2 deletions Resources/config/security_email.xml
Expand Up @@ -28,9 +28,12 @@
<tag name="scheb_two_factor.provider" alias="email" />
<argument type="service" id="scheb_two_factor.security.email.code_generator" />
<argument type="service" id="scheb_two_factor.security.email.code_validator" />
<argument type="service" id="templating" />
<argument>%scheb_two_factor.email.template%</argument>
<argument type="service" id="scheb_two_factor.security.email.renderer" />
<argument>%scheb_two_factor.parameter_names.auth_code%</argument>
</service>
<service id="scheb_two_factor.security.email.renderer" class="%scheb_two_factor.security.renderer.class%" lazy="true">
<argument type="service" id="templating" />
<argument>%scheb_two_factor.email.template%</argument>
</service>
</services>
</container>
7 changes: 5 additions & 2 deletions Resources/config/security_google.xml
Expand Up @@ -25,9 +25,12 @@
<service id="scheb_two_factor.security.google.provider" class="%scheb_two_factor.security.google.provider.class%">
<tag name="scheb_two_factor.provider" alias="google" />
<argument type="service" id="scheb_two_factor.security.google.code_validator" />
<argument type="service" id="templating" />
<argument>%scheb_two_factor.google.template%</argument>
<argument type="service" id="scheb_two_factor.security.google.renderer" />
<argument>%scheb_two_factor.parameter_names.auth_code%</argument>
</service>
<service id="scheb_two_factor.security.google.renderer" class="%scheb_two_factor.security.renderer.class%" lazy="true">
<argument type="service" id="templating" />
<argument>%scheb_two_factor.google.template%</argument>
</service>
</services>
</container>
29 changes: 6 additions & 23 deletions Security/TwoFactor/Provider/Email/TwoFactorProvider.php
Expand Up @@ -7,7 +7,7 @@
use Scheb\TwoFactorBundle\Security\TwoFactor\Provider\Email\Generator\CodeGeneratorInterface;
use Scheb\TwoFactorBundle\Security\TwoFactor\Provider\Email\Validation\CodeValidatorInterface;
use Scheb\TwoFactorBundle\Security\TwoFactor\Provider\TwoFactorProviderInterface;
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface;
use Scheb\TwoFactorBundle\Security\TwoFactor\Renderer;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Response;

Expand All @@ -24,35 +24,20 @@ class TwoFactorProvider implements TwoFactorProviderInterface
private $authenticator;

/**
* @var EngineInterface
* @var Renderer
*/
private $templating;

/**
* @var string
*/
private $formTemplate;
private $renderer;

/**
* @var string
*/
private $authCodeParameter;

/**
* Construct provider for email authentication.
*
* @param CodeGeneratorInterface $codeGenerator
* @param CodeValidatorInterface $authenticator
* @param EngineInterface $templating
* @param string $formTemplate
* @param string $authCodeParameter
*/
public function __construct(CodeGeneratorInterface $codeGenerator, CodeValidatorInterface $authenticator, EngineInterface $templating, $formTemplate, $authCodeParameter)
public function __construct(CodeGeneratorInterface $codeGenerator, CodeValidatorInterface $authenticator, Renderer $renderer, $authCodeParameter)
{
$this->codeGenerator = $codeGenerator;
$this->authenticator = $authenticator;
$this->templating = $templating;
$this->formTemplate = $formTemplate;
$this->renderer = $renderer;
$this->authCodeParameter = $authCodeParameter;
}

Expand Down Expand Up @@ -103,8 +88,6 @@ public function requestAuthenticationCode(AuthenticationContextInterface $contex
}

// Force authentication code dialog
return $this->templating->renderResponse($this->formTemplate, array(
'useTrustedOption' => $context->useTrustedOption(),
));
return $this->renderer->render($context);
}
}
28 changes: 6 additions & 22 deletions Security/TwoFactor/Provider/Google/TwoFactorProvider.php
Expand Up @@ -6,7 +6,7 @@
use Scheb\TwoFactorBundle\Security\TwoFactor\AuthenticationContextInterface;
use Scheb\TwoFactorBundle\Security\TwoFactor\Provider\Google\Validation\CodeValidatorInterface;
use Scheb\TwoFactorBundle\Security\TwoFactor\Provider\TwoFactorProviderInterface;
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface;
use Scheb\TwoFactorBundle\Security\TwoFactor\Renderer;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Response;

Expand All @@ -18,33 +18,19 @@ class TwoFactorProvider implements TwoFactorProviderInterface
private $authenticator;

/**
* @var EngineInterface
* @var Renderer
*/
private $templating;

/**
* @var string
*/
private $formTemplate;
private $renderer;

/**
* @var string
*/
private $authCodeParameter;

/**
* Construct provider for Google authentication.
*
* @param CodeValidatorInterface $authenticator
* @param EngineInterface $templating
* @param string $formTemplate
* @param string $authCodeParameter
*/
public function __construct(CodeValidatorInterface $authenticator, EngineInterface $templating, $formTemplate, $authCodeParameter)
public function __construct(CodeValidatorInterface $authenticator, Renderer $renderer, $authCodeParameter)
{
$this->authenticator = $authenticator;
$this->templating = $templating;
$this->formTemplate = $formTemplate;
$this->renderer = $renderer;
$this->authCodeParameter = $authCodeParameter;
}

Expand Down Expand Up @@ -89,8 +75,6 @@ public function requestAuthenticationCode(AuthenticationContextInterface $contex
}

// Force authentication code dialog
return $this->templating->renderResponse($this->formTemplate, array(
'useTrustedOption' => $context->useTrustedOption(),
));
return $this->renderer->render($context);
}
}
24 changes: 24 additions & 0 deletions Security/TwoFactor/Renderer.php
@@ -0,0 +1,24 @@
<?php

namespace Scheb\TwoFactorBundle\Security\TwoFactor;

use Scheb\TwoFactorBundle\Security\TwoFactor\AuthenticationContextInterface;
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface;

class Renderer
{
private $templating;
private $formTemplate;

public function __construct(EngineInterface $templating, $formTemplate)
{
$this->templating = $templating;
$this->formTemplate = $formTemplate;
}

public function render(AuthenticationContextInterface $context) {
return $this->templating->renderResponse($this->formTemplate, array(
'useTrustedOption' => $context->useTrustedOption(),
));
}
}
30 changes: 13 additions & 17 deletions Tests/Security/TwoFactor/Provider/Email/TwoFactorProviderTest.php
Expand Up @@ -21,12 +21,7 @@ class TwoFactorProviderTest extends TestCase
/**
* @var \PHPUnit_Framework_MockObject_MockObject
*/
private $templating;

/**
* @var string
*/
private $formTemplate = 'AcmeTestBundle:Test:auth.html.twig';
private $renderer;

/**
* @var TwoFactorProvider
Expand All @@ -39,9 +34,9 @@ public function setUp()

$this->authenticator = $this->createMock('Scheb\TwoFactorBundle\Security\TwoFactor\Provider\Email\Validation\CodeValidatorInterface');

$this->templating = $this->createMock('Symfony\Bundle\FrameworkBundle\Templating\EngineInterface');
$this->renderer = $this->createMock('Scheb\TwoFactorBundle\Security\TwoFactor\Renderer');

$this->provider = new TwoFactorProvider($this->generator, $this->authenticator, $this->templating, $this->formTemplate, 'authCodeName');
$this->provider = new TwoFactorProvider($this->generator, $this->authenticator, $this->renderer, 'authCodeName');
}

/**
Expand Down Expand Up @@ -218,13 +213,14 @@ public function beginAuthentication_interfaceNotImplemented_returnFalse()
*/
public function requestAuthenticationCode_trustedOption_assignToTemplate($trustedOption)
{
$context = $this->getAuthenticationContext(null, null, null, $trustedOption);

//Mock the template engine
$this->templating
$this->renderer
->expects($this->once())
->method('renderResponse')
->with($this->formTemplate, array('useTrustedOption' => $trustedOption));
->method('render')
->with($context);

$context = $this->getAuthenticationContext(null, null, null, $trustedOption);
$this->provider->requestAuthenticationCode($context);
}

Expand Down Expand Up @@ -255,9 +251,9 @@ public function requestAuthenticationCode_notPostRequest_displayForm()
->method('checkCode');

//Mock the template engine
$this->templating
$this->renderer
->expects($this->once())
->method('renderResponse')
->method('render')
->will($this->returnValue(new Response('<form></form>')));

$returnValue = $this->provider->requestAuthenticationCode($context);
Expand Down Expand Up @@ -301,10 +297,10 @@ public function requestAuthenticationCode_invalidCode_displayFlashMessage()
->with('two_factor', 'scheb_two_factor.code_invalid');

//Mock the template engine
$this->templating
$this->renderer
->expects($this->once())
->method('renderResponse')
->with($this->formTemplate, $this->anything())
->method('render')
->with($context)
->will($this->returnValue(new Response('<form></form>')));

$returnValue = $this->provider->requestAuthenticationCode($context);
Expand Down
30 changes: 13 additions & 17 deletions Tests/Security/TwoFactor/Provider/Google/TwoFactorProviderTest.php
Expand Up @@ -16,12 +16,7 @@ class TwoFactorProviderTest extends TestCase
/**
* @var \PHPUnit_Framework_MockObject_MockObject
*/
private $templating;

/**
* @var string
*/
private $formTemplate = 'AcmeTestBundle:Test:auth.html.twig';
private $renderer;

/**
* @var TwoFactorProvider
Expand All @@ -31,8 +26,8 @@ class TwoFactorProviderTest extends TestCase
public function setUp()
{
$this->authenticator = $this->createMock('Scheb\TwoFactorBundle\Security\TwoFactor\Provider\Google\Validation\CodeValidatorInterface');
$this->templating = $this->createMock('Symfony\Bundle\FrameworkBundle\Templating\EngineInterface');
$this->provider = new TwoFactorProvider($this->authenticator, $this->templating, $this->formTemplate, 'authCodeName');
$this->renderer = $this->createMock('Scheb\TwoFactorBundle\Security\TwoFactor\Renderer');
$this->provider = new TwoFactorProvider($this->authenticator, $this->renderer, 'authCodeName');
}

/**
Expand Down Expand Up @@ -192,13 +187,14 @@ public function beginAuthentication_interfaceNotImplemented_returnFalse()
*/
public function requestAuthenticationCode_trustedOption_assignToTemplate($trustedOption)
{
$context = $this->getAuthenticationContext(null, null, null, $trustedOption);

//Mock the template engine
$this->templating
$this->renderer
->expects($this->once())
->method('renderResponse')
->with($this->formTemplate, array('useTrustedOption' => $trustedOption));
->method('render')
->with($context);

$context = $this->getAuthenticationContext(null, null, null, $trustedOption);
$this->provider->requestAuthenticationCode($context);
}

Expand Down Expand Up @@ -229,9 +225,9 @@ public function requestAuthenticationCode_notPostRequest_displayForm()
->method('checkCode');

//Mock the template engine
$this->templating
$this->renderer
->expects($this->once())
->method('renderResponse')
->method('render')
->will($this->returnValue(new Response('<form></form>')));

$returnValue = $this->provider->requestAuthenticationCode($context);
Expand Down Expand Up @@ -275,10 +271,10 @@ public function requestAuthenticationCode_invalidCode_displayFlashMessage()
->with('two_factor', 'scheb_two_factor.code_invalid');

//Mock the template engine
$this->templating
$this->renderer
->expects($this->once())
->method('renderResponse')
->with($this->formTemplate, $this->anything())
->method('render')
->with($context)
->will($this->returnValue(new Response('<form></form>')));

$returnValue = $this->provider->requestAuthenticationCode($context);
Expand Down
28 changes: 28 additions & 0 deletions Tests/Security/TwoFactor/RendererTest.php
@@ -0,0 +1,28 @@
<?php

namespace Scheb\TwoFactorBundle\Tests\Security\TwoFactor;

use Scheb\TwoFactorBundle\Security\TwoFactor\Renderer;
use Scheb\TwoFactorBundle\Tests\TestCase;

class RendererTest extends TestCase
{
/**
* @test
*/
public function render_returnResponse()
{
$response = $this->createMock('Symfony\Component\HttpFoundation\Response');
$templating = $this->createMock('Symfony\Bundle\FrameworkBundle\Templating\EngineInterface');
$templating
->expects($this->any())
->method('renderResponse')
->will($this->returnValue($response))
;
$context = $this->createMock('Scheb\TwoFactorBundle\Security\TwoFactor\AuthenticationContextInterface');

$renderer = new Renderer($templating, 'AcmeTestBundle:Test:auth.html.twig');

$this->assertEquals($response, $renderer->render($context));
}
}
1 change: 1 addition & 0 deletions composer.json
Expand Up @@ -18,6 +18,7 @@
"ocramius/proxy-manager": "~1.0|~2.0"
},
"require-dev": {
"doctrine/orm": "~2.4,>=2.4.5",
"phpunit/phpunit": ">=4.8,<6.0",
"symfony/phpunit-bridge": "~2.7|~3.0",
"swiftmailer/swiftmailer": ">=4.3,<6.0",
Expand Down