diff --git a/Security/AbstractGenericTokenFactory.php b/Security/AbstractGenericTokenFactory.php index ae0e206..f6d5cb5 100644 --- a/Security/AbstractGenericTokenFactory.php +++ b/Security/AbstractGenericTokenFactory.php @@ -28,7 +28,7 @@ abstract class AbstractGenericTokenFactory implements GenericTokenFactoryInterfa /** * @param StorageInterface $tokenStorage - * @param StorageRegistryInterface $payum + * @param StorageRegistryInterface $storageRegistry * @param string $capturePath * @param string $notifyPath */ @@ -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, '?')) { @@ -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()) { @@ -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); } diff --git a/Security/GenericTokenFactoryInterface.php b/Security/GenericTokenFactoryInterface.php index 2eca8f3..412d9d9 100644 --- a/Security/GenericTokenFactoryInterface.php +++ b/Security/GenericTokenFactoryInterface.php @@ -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); } diff --git a/Security/TokenFactoryInterface.php b/Security/TokenFactoryInterface.php index 7f653fe..23816d2 100644 --- a/Security/TokenFactoryInterface.php +++ b/Security/TokenFactoryInterface.php @@ -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 diff --git a/Tests/Security/GenericTokenFactoryTest.php b/Tests/Security/GenericTokenFactoryTest.php index fae3d88..9bb6361 100644 --- a/Tests/Security/GenericTokenFactoryTest.php +++ b/Tests/Security/GenericTokenFactoryTest.php @@ -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 */