Skip to content

Commit

Permalink
Merge pull request #148 from gsteel/identity-helper-refactor
Browse files Browse the repository at this point in the history
Refactor identity helper
  • Loading branch information
Ocramius authored Feb 22, 2022
2 parents 689753f + 3026484 commit 2cd6973
Show file tree
Hide file tree
Showing 9 changed files with 150 additions and 264 deletions.
63 changes: 2 additions & 61 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -702,11 +702,6 @@
<code>(bool) $useNamespaces</code>
</RedundantCastGivenDocblockType>
</file>
<file src="src/Helper/Identity.php">
<MissingConstructor occurrences="1">
<code>$authenticationService</code>
</MissingConstructor>
</file>
<file src="src/Helper/InlineScript.php">
<LessSpecificReturnStatement occurrences="1">
<code>parent::__invoke($mode, $spec, $placement, $attrs, $type)</code>
Expand Down Expand Up @@ -1629,21 +1624,6 @@
<code>$requestedName</code>
</PossiblyNullArgument>
</file>
<file src="src/Helper/Service/IdentityFactory.php">
<DeprecatedInterface occurrences="1">
<code>IdentityFactory</code>
</DeprecatedInterface>
<MixedInferredReturnType occurrences="1">
<code>null|AuthenticationServiceInterface</code>
</MixedInferredReturnType>
<MixedReturnStatement occurrences="3">
<code>$container-&gt;get(AuthenticationService::class)</code>
<code>$container-&gt;get(\Zend\Authentication\AuthenticationService::class)</code>
</MixedReturnStatement>
<PossiblyNullArgument occurrences="1">
<code>$cName</code>
</PossiblyNullArgument>
</file>
<file src="src/Helper/TranslatorAwareTrait.php">
<DeprecatedClass occurrences="3">
<code>$this</code>
Expand Down Expand Up @@ -2805,14 +2785,6 @@
<code>$escape('http://www.w3.org/1999/xhtml')</code>
</MixedArgument>
</file>
<file src="test/Helper/IdentityTest.php">
<PossiblyNullReference occurrences="1">
<code>setIdentity</code>
</PossiblyNullReference>
<UndefinedInterfaceMethod occurrences="1">
<code>setIdentity</code>
</UndefinedInterfaceMethod>
</file>
<file src="test/Helper/LayoutTest.php">
<UndefinedMethod occurrences="1">
<code>setTemplate</code>
Expand Down Expand Up @@ -3344,34 +3316,6 @@
<code>1337</code>
</InvalidScalarArgument>
</file>
<file src="test/Helper/Service/IdentityFactoryTest.php">
<ArgumentTypeCoercion occurrences="3">
<code>$this-&gt;getContainerForFactory()</code>
<code>$this-&gt;getContainerForFactory()</code>
<code>$this-&gt;getContainerForFactory()</code>
</ArgumentTypeCoercion>
<InvalidPropertyAssignmentValue occurrences="1">
<code>$this-&gt;prophesize(ServiceManager::class)</code>
</InvalidPropertyAssignmentValue>
<MixedInferredReturnType occurrences="1">
<code>ContainerInterface</code>
</MixedInferredReturnType>
<MixedMethodCall occurrences="10">
<code>willReturn</code>
<code>willReturn</code>
<code>willReturn</code>
<code>willReturn</code>
<code>willReturn</code>
<code>willReturn</code>
<code>willReturn</code>
<code>willReturn</code>
<code>willReturn</code>
<code>willReturn</code>
</MixedMethodCall>
<MixedReturnStatement occurrences="1">
<code>$this-&gt;services-&gt;reveal()</code>
</MixedReturnStatement>
</file>
<file src="test/Helper/TestAsset/Bar.php">
<PropertyNotSetInConstructor occurrences="2">
<code>Bar</code>
Expand Down Expand Up @@ -3459,18 +3403,15 @@
<code>$container</code>
<code>$container</code>
</MissingClosureParamType>
<MixedAssignment occurrences="8">
<code>$expected</code>
<MixedAssignment occurrences="6">
<code>$helper</code>
<code>$helper</code>
<code>$helper</code>
<code>$helper</code>
<code>$helper</code>
<code>$helperB</code>
<code>$identity</code>
</MixedAssignment>
<MixedMethodCall occurrences="7">
<code>getAuthenticationService</code>
<MixedMethodCall occurrences="6">
<code>getTranslator</code>
<code>getTranslator</code>
<code>getTranslator</code>
Expand Down
40 changes: 24 additions & 16 deletions src/Helper/Identity.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,56 +5,64 @@
namespace Laminas\View\Helper;

use Laminas\Authentication\AuthenticationServiceInterface;
use Laminas\View\Exception;
use Laminas\View\Exception\RuntimeException;

/**
* View helper plugin to fetch the authenticated identity.
*/
class Identity extends AbstractHelper
{
/**
* AuthenticationService instance
*
* @var AuthenticationServiceInterface
*/
use DeprecatedAbstractHelperHierarchyTrait;

/** @var AuthenticationServiceInterface|null */
protected $authenticationService;

public function __construct(?AuthenticationServiceInterface $authenticationService = null)
{
$this->authenticationService = $authenticationService;
}

/**
* Retrieve the current identity, if any.
*
* If none available, returns null.
*
* @throws Exception\RuntimeException
* @return mixed|null
*/
public function __invoke()
{
if (! $this->authenticationService instanceof AuthenticationServiceInterface) {
throw new Exception\RuntimeException('No AuthenticationServiceInterface instance provided');
}

if (! $this->authenticationService->hasIdentity()) {
return;
$service = $this->authenticationService;
if (! $service instanceof AuthenticationServiceInterface) {
throw new RuntimeException('No AuthenticationServiceInterface instance provided');
}

return $this->authenticationService->getIdentity();
return $service->hasIdentity()
? $service->getIdentity()
: null;
}

/**
* Set AuthenticationService instance
*
* @return Identity
* @deprecated since >= 2.20.0. The authentication service should be provided to the constructor. This method will
* be removed in version 3.0 of this component
*
* @return $this
*/
public function setAuthenticationService(AuthenticationServiceInterface $authenticationService)
{
$this->authenticationService = $authenticationService;

return $this;
}

/**
* Get AuthenticationService instance
*
* @return AuthenticationServiceInterface
* @deprecated since >= 2.20.0. The authentication service should be provided to the constructor. This method will
* be removed in version 3.0 of this component
*
* @return null|object
*/
public function getAuthenticationService()
{
Expand Down
50 changes: 27 additions & 23 deletions src/Helper/Service/IdentityFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,19 @@
use Laminas\ServiceManager\ServiceLocatorInterface;
use Laminas\View\Helper\Identity;

/**
* @psalm-suppress DeprecatedInterface
*/
class IdentityFactory implements FactoryInterface
{
/**
* @param string $name
* @param null|array $options
* @param string|null $name
* @param array<array-key, mixed>|null $options
* @return Identity
*/
public function __invoke(ContainerInterface $container, $name, ?array $options = null)
{
$helper = new Identity();

if (null !== ($authenticationService = $this->discoverAuthenticationService($container))) {
$helper->setAuthenticationService($authenticationService);
}

return $helper;
return new Identity($this->discoverAuthenticationService($container));
}

/**
Expand All @@ -41,23 +38,30 @@ public function createService(ServiceLocatorInterface $serviceLocator, $rName =
return $this($serviceLocator, $cName);
}

/**
* @return null|AuthenticationServiceInterface
*/
private function discoverAuthenticationService(ContainerInterface $container)
private function discoverAuthenticationService(ContainerInterface $container): ?AuthenticationServiceInterface
{
if ($container->has(AuthenticationService::class)) {
return $container->get(AuthenticationService::class);
}
// phpcs:disable WebimpressCodingStandard.Formatting.StringClassReference
$search = [
AuthenticationService::class,
AuthenticationServiceInterface::class,
'Zend\Authentication\AuthenticationService',
'Zend\Authentication\AuthenticationServiceInterface',
];
// phpcs:enable

foreach ($search as $id) {
if (! $container->has($id)) {
continue;
}

$service = $container->get($id);
if (! $service instanceof AuthenticationServiceInterface) {
continue;
}

if ($container->has(\Zend\Authentication\AuthenticationService::class)) {
return $container->get(\Zend\Authentication\AuthenticationService::class);
return $service;
}

return $container->has(AuthenticationServiceInterface::class)
? $container->get(AuthenticationServiceInterface::class)
: ($container->has(\Zend\Authentication\AuthenticationServiceInterface::class)
? $container->get(\Zend\Authentication\AuthenticationServiceInterface::class)
: null);
return null;
}
}
42 changes: 21 additions & 21 deletions test/Helper/IdentityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,35 +4,35 @@

namespace LaminasTest\View\Helper;

use Laminas\Authentication\AuthenticationService;
use Laminas\Authentication\Storage\NonPersistent as NonPersistentStorage;
use Laminas\View\Exception\RuntimeException;
use Laminas\View\Helper\Identity as IdentityHelper;
use LaminasTest\View\Helper\TestAsset\AuthenticationServiceStub;
use PHPUnit\Framework\TestCase;

class IdentityTest extends TestCase
{
public function testGetIdentity(): void
private function authService(?string $id): AuthenticationServiceStub
{
$identity = new TestAsset\IdentityObject();
$identity->setUsername('a username');
$identity->setPassword('a password');

$authenticationService = new AuthenticationService(
new NonPersistentStorage(),
new TestAsset\AuthenticationAdapter()
);

$identityHelper = new IdentityHelper();
$identityHelper->setAuthenticationService($authenticationService);

$this->assertNull($identityHelper());
return new AuthenticationServiceStub($id);
}

$this->assertFalse($authenticationService->hasIdentity());
public function testIdentityIsNullWhenTheAuthServiceDoesNotHaveAnIdentity(): void
{
$helper = new IdentityHelper($this->authService(null));
self::assertNull($helper());
}

$authenticationService->getAdapter()->setIdentity($identity);
$result = $authenticationService->authenticate();
$this->assertTrue($result->isValid());
public function testAnExceptionIsThrownWhenThereIsNoAuthServiceAtAll(): void
{
$helper = new IdentityHelper();
$this->expectException(RuntimeException::class);
$this->expectExceptionMessage('No AuthenticationServiceInterface instance provided');
self::assertNull($helper());
}

$this->assertEquals($identity, $identityHelper());
public function testIdentityIsTheExpectedValueWhenTheAuthServiceHasAnIdentity(): void
{
$helper = new IdentityHelper($this->authService('goat-man'));
self::assertSame('goat-man', $helper());
}
}
Loading

0 comments on commit 2cd6973

Please sign in to comment.