Skip to content

Commit

Permalink
[security] allow create notify token without model set.
Browse files Browse the repository at this point in the history
  • Loading branch information
makasim committed May 1, 2014
1 parent 18868e3 commit 069d191
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 18 deletions.
26 changes: 11 additions & 15 deletions Security/AbstractGenericTokenFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ abstract class AbstractGenericTokenFactory implements GenericTokenFactoryInterfa

/**
* @param StorageInterface $tokenStorage
* @param StorageRegistryInterface $payum
* @param StorageRegistryInterface $storageRegistry
* @param string $capturePath
* @param string $notifyPath
*/
Expand All @@ -46,13 +46,17 @@ public function __construct(StorageInterface $tokenStorage, StorageRegistryInter
*/
public function createToken($paymentName, $model, $targetPath, array $targetParameters = array(), $afterPath = null, array $afterParameters = array())
{
$modelStorage = $this->storageRegistry->getStorageForClass($model, $paymentName);

/** @var TokenInterface $token */
$token = $this->tokenStorage->createModel();
$token->setDetails($modelStorage->getIdentificator($model));

$token->setPaymentName($paymentName);

if (null !== $model) {
$token->setDetails(
$this->storageRegistry->getStorageForClass($model, $paymentName)->getIdentificator($model)
);
}

$targetParameters = array_replace($targetParameters, array('payum_token' => $token->getHash()));
if (0 === strpos($targetPath, 'http')) {
if (false !== strpos($targetPath, '?')) {
Expand Down Expand Up @@ -84,12 +88,7 @@ public function createToken($paymentName, $model, $targetPath, array $targetPara
}

/**
* @param string $paymentName
* @param object $model
* @param string $afterPath
* @param array $afterParameters
*
* @return TokenInterface
* {@inheritDoc}
*/
public function createCaptureToken($paymentName, $model, $afterPath, array $afterParameters = array())
{
Expand All @@ -104,12 +103,9 @@ public function createCaptureToken($paymentName, $model, $afterPath, array $afte
}

/**
* @param string $paymentName
* @param object $model
*
* @return TokenInterface
* {@inheritDoc}
*/
public function createNotifyToken($paymentName, $model)
public function createNotifyToken($paymentName, $model = null)
{
return $this->createToken($paymentName, $model, $this->notifyPath);
}
Expand Down
4 changes: 2 additions & 2 deletions Security/GenericTokenFactoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ public function createCaptureToken($paymentName, $model, $afterPath, array $afte

/**
* @param string $paymentName
* @param object $model
* @param object|null $model
*
* @return TokenInterface
*/
public function createNotifyToken($paymentName, $model);
public function createNotifyToken($paymentName, $model = null);
}
2 changes: 1 addition & 1 deletion Security/TokenFactoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ interface TokenFactoryInterface
{
/**
* @param string $paymentName
* @param object $model
* @param object|null $model
* @param string $targetPath
* @param array $targetParameters
* @param string $afterPath
Expand Down
47 changes: 47 additions & 0 deletions Tests/Security/GenericTokenFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,53 @@ public function shouldCreateNotifyToken()
$this->assertNull($token->getAfterUrl());
}

/**
* @test
*/
public function shouldCreateNotifyTokenWithoutModel()
{
$token = new Token;

$tokenStorageMock = $this->createStorageMock();
$tokenStorageMock
->expects($this->once())
->method('createModel')
->will($this->returnValue($token))
;
$tokenStorageMock
->expects($this->once())
->method('updateModel')
->with($this->identicalTo($token))
;

$paymentName = 'thePaymentName';

$storageRegistryMock = $this->createStorageRegistryMock();
$storageRegistryMock
->expects($this->never())
->method('getStorageForClass')
;

$factory = new GenericTokenFactory(
$tokenStorageMock,
$storageRegistryMock,
'http://example.com',
'capture.php',
'notify.php'
);

$actualToken = $factory->createNotifyToken($paymentName, null);

$this->assertSame($token, $actualToken);
$this->assertEquals($paymentName, $token->getPaymentName());
$this->assertNull($token->getDetails());
$this->assertEquals(
'http://example.com/notify.php?payum_token='.$token->getHash(),
$token->getTargetUrl()
);
$this->assertNull($token->getAfterUrl());
}

/**
* @test
*/
Expand Down

0 comments on commit 069d191

Please sign in to comment.