Skip to content

Commit

Permalink
refactor from code review
Browse files Browse the repository at this point in the history
  • Loading branch information
Orkin committed Aug 3, 2020
1 parent 06f2fb4 commit efe7bf0
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 94 deletions.
73 changes: 42 additions & 31 deletions src/GooglePlay/Acknowledger.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@

use Exception;
use Google_Service_AndroidPublisher;
use Google_Service_AndroidPublisher_ProductPurchasesAcknowledgeRequest;
use Google_Service_AndroidPublisher_SubscriptionPurchasesAcknowledgeRequest;
use ReceiptValidator\GooglePlay\Exception\AlreadyAcknowledgeException;
use ReceiptValidator\RunTimeException;

/**
Expand Down Expand Up @@ -61,7 +58,7 @@ public function __construct(
$purchaseToken,
$strategy = self::ACKNOWLEDGE_STRATEGY_EXPLICIT
) {
if (! in_array($strategy, [self::ACKNOWLEDGE_STRATEGY_EXPLICIT, self::ACKNOWLEDGE_STRATEGY_IMPLICIT])) {
if (!in_array($strategy, [self::ACKNOWLEDGE_STRATEGY_EXPLICIT, self::ACKNOWLEDGE_STRATEGY_IMPLICIT])) {
throw new RuntimeException(sprintf('Invalid strategy provided %s', $strategy));
}

Expand All @@ -76,58 +73,72 @@ public function __construct(
* @param string $type
* @param string $developerPayload
*
* @return bool
* @throws RunTimeException
*
* @return bool
*/
public function acknowledge(string $type = self::SUBSCRIPTION, string $developerPayload = '')
{
try {
switch ($type) {
case self::SUBSCRIPTION:
$subscriptionPurchase = $this->androidPublisherService->purchases_subscriptions->get(
$this->packageName,
$this->productId,
$this->purchaseToken
);

if ($this->strategy === self::ACKNOWLEDGE_STRATEGY_EXPLICIT
&& $subscriptionPurchase->getAcknowledgementState() === 1) {
throw AlreadyAcknowledgeException::fromSubscriptionPurchase($subscriptionPurchase);
}

if ($subscriptionPurchase->getAcknowledgementState() != 1) {
if ($this->strategy === self::ACKNOWLEDGE_STRATEGY_EXPLICIT) {
// Here exception might be thrown as previously, so no BC break here
$this->androidPublisherService->purchases_subscriptions->acknowledge(
$this->packageName,
$this->productId,
$this->purchaseToken,
new Google_Service_AndroidPublisher_SubscriptionPurchasesAcknowledgeRequest(
new \Google_Service_AndroidPublisher_SubscriptionPurchasesAcknowledgeRequest(
['developerPayload' => $developerPayload]
)
);
} elseif ($this->strategy === self::ACKNOWLEDGE_STRATEGY_IMPLICIT) {
$subscriptionPurchase = $this->androidPublisherService->purchases_subscriptions->get(
$this->packageName,
$this->productId,
$this->purchaseToken
);

if ($subscriptionPurchase->getAcknowledgementState() !== AbstractResponse::ACKNOWLEDGEMENT_STATE_DONE) {
$this->androidPublisherService->purchases_subscriptions->acknowledge(
$this->packageName,
$this->productId,
$this->purchaseToken,
new \Google_Service_AndroidPublisher_SubscriptionPurchasesAcknowledgeRequest(
['developerPayload' => $developerPayload]
)
);
}
}
break;
case self::PRODUCT:
$productPurchase = $this->androidPublisherService->purchases_products->get(
$this->packageName,
$this->productId,
$this->purchaseToken
);

if ($this->strategy === self::ACKNOWLEDGE_STRATEGY_EXPLICIT
&& $productPurchase->getAcknowledgementState() === 1) {
throw AlreadyAcknowledgeException::fromProductPurchase($productPurchase);
}

if ($productPurchase->getAcknowledgementState() != 1) {
if ($this->strategy === self::ACKNOWLEDGE_STRATEGY_EXPLICIT) {
// Here exception might be thrown as previously, so no BC break here
$this->androidPublisherService->purchases_products->acknowledge(
$this->packageName,
$this->productId,
$this->purchaseToken,
new Google_Service_AndroidPublisher_ProductPurchasesAcknowledgeRequest(
new \Google_Service_AndroidPublisher_ProductPurchasesAcknowledgeRequest(
['developerPayload' => $developerPayload]
)
);
} elseif ($this->strategy === self::ACKNOWLEDGE_STRATEGY_IMPLICIT) {
$productPurchase = $this->androidPublisherService->purchases_products->get(
$this->packageName,
$this->productId,
$this->purchaseToken
);

if ($productPurchase->getAcknowledgementState() !== AbstractResponse::ACKNOWLEDGEMENT_STATE_DONE) {
$this->androidPublisherService->purchases_products->acknowledge(
$this->packageName,
$this->productId,
$this->purchaseToken,
new \Google_Service_AndroidPublisher_ProductPurchasesAcknowledgeRequest(
['developerPayload' => $developerPayload]
)
);
}
}
break;
default:
Expand Down
29 changes: 0 additions & 29 deletions src/GooglePlay/Exception/AlreadyAcknowledgeException.php

This file was deleted.

42 changes: 8 additions & 34 deletions tests/GooglePlay/GooglePlayAcknowledgerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
use Google_Service_AndroidPublisher_SubscriptionPurchase;
use PHPUnit\Framework\TestCase;
use ReceiptValidator\GooglePlay\Acknowledger;
use ReceiptValidator\GooglePlay\Exception\AlreadyAcknowledgeException;

/**
* @group library
Expand Down Expand Up @@ -48,12 +47,6 @@ public function testValidateWithNonAcknowledgedPurchase(): void
$googleServiceAndroidPublisherMock->purchases_products = $purchasesProductsMock;
$googleServiceAndroidPublisherMock->purchases_subscriptions = $purchasesSubscriptionsMock;

$purchasesProductsMock->expects($this->once())->method('get')
->with(
$packageName,
$productId,
$purchaseToken
)->willReturn($productPurchaseMock);
$purchasesProductsMock->expects($this->once())->method('acknowledge')
->with(
$packageName,
Expand All @@ -64,12 +57,6 @@ public function testValidateWithNonAcknowledgedPurchase(): void
)
);

$purchasesSubscriptionsMock->expects($this->once())->method('get')
->with(
$packageName,
$productId,
$purchaseToken
)->willReturn($subscriptionPurchaseMock);
$purchasesSubscriptionsMock->expects($this->once())->method('acknowledge')
->with(
$packageName,
Expand Down Expand Up @@ -169,35 +156,30 @@ public function testValidateWithAcknowledgedPurchaseAndImplicitStrategy(): void

public function testValidateWithAcknowledgedPurchaseAndExplicitStrategyForSubscription(): void
{
$this->expectException(AlreadyAcknowledgeException::class);

$packageName = 'testPackage';
$productId = '15';
$purchaseToken = 'testPurchaseToken';

// mock objects
$googleServiceAndroidPublisherMock = $this->getMockBuilder(Google_Service_AndroidPublisher::class)
->disableOriginalConstructor()->getMock();
->disableOriginalConstructor()
->getMock();

// subscriptions
$purchasesSubscriptionsMock = $this->getMockBuilder(
Google_Service_AndroidPublisher_Resource_PurchasesSubscriptions::class
)
->disableOriginalConstructor()->getMock();
->disableOriginalConstructor()
->getMock();
$subscriptionPurchaseMock = $this->getMockBuilder(Google_Service_AndroidPublisher_SubscriptionPurchase::class)
->disableOriginalConstructor()->getMock();
->disableOriginalConstructor()
->getMock();
$subscriptionPurchaseMock->expects($this->any())->method('getAcknowledgementState')->willReturn(1);

// mock expectations
$googleServiceAndroidPublisherMock->purchases_subscriptions = $purchasesSubscriptionsMock;

$purchasesSubscriptionsMock->expects($this->once())->method('get')
->with(
$packageName,
$productId,
$purchaseToken
)->willReturn($subscriptionPurchaseMock);
$purchasesSubscriptionsMock->expects($this->never())->method('acknowledge')
$purchasesSubscriptionsMock->expects($this->once())->method('acknowledge')
->with(
$packageName,
$productId,
Expand All @@ -219,8 +201,6 @@ public function testValidateWithAcknowledgedPurchaseAndExplicitStrategyForSubscr

public function testValidateWithAcknowledgedPurchaseAndExplicitStrategyForProduct(): void
{
$this->expectException(AlreadyAcknowledgeException::class);

$packageName = 'testPackage';
$productId = '15';
$purchaseToken = 'testPurchaseToken';
Expand All @@ -241,13 +221,7 @@ public function testValidateWithAcknowledgedPurchaseAndExplicitStrategyForProduc
// mock expectations
$googleServiceAndroidPublisherMock->purchases_products = $purchasesProductsMock;

$purchasesProductsMock->expects($this->once())->method('get')
->with(
$packageName,
$productId,
$purchaseToken
)->willReturn($productPurchaseMock);
$purchasesProductsMock->expects($this->never())->method('acknowledge')
$purchasesProductsMock->expects($this->once())->method('acknowledge')
->with(
$packageName,
$productId,
Expand Down

0 comments on commit efe7bf0

Please sign in to comment.