Completed
Push — master ( ae6388...60bf23 )
by
unknown
15s queued 10s
created

Acknowledger::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
nc 1
nop 4
dl 0
loc 10
ccs 5
cts 5
cp 1
crap 1
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace ReceiptValidator\GooglePlay;
4
5
/**
6
 * Class Acknowledger.
7
 */
8
class Acknowledger
9
{
10
    const SUBSCRIPTION = 'SUBSCRIPTION';
11
    const PRODUCT = 'PRODUCT';
12
13
    /**
14
     * @var \Google_Service_AndroidPublisher
15
     */
16
    protected $androidPublisherService;
17
    /**
18
     * @var string
19
     */
20
    protected $packageName;
21
    /**
22
     * @var string
23
     */
24
    protected $purchaseToken;
25
    /**
26
     * @var string
27
     */
28
    protected $productId;
29
30
    /**
31
     * Acknowledger constructor.
32
     *
33
     * @param \Google_Service_AndroidPublisher $googleServiceAndroidPublisher
34
     * @param string                           $packageName
35
     * @param string                           $purchaseToken
36
     * @param string                           $productId
37
     */
38 1
    public function __construct(
39
        \Google_Service_AndroidPublisher $googleServiceAndroidPublisher,
40
        $packageName,
41
        $productId,
42
        $purchaseToken
43
    ) {
44 1
        $this->androidPublisherService = $googleServiceAndroidPublisher;
45 1
        $this->packageName = $packageName;
46 1
        $this->purchaseToken = $purchaseToken;
47 1
        $this->productId = $productId;
48 1
    }
49
50
    /**
51
     * @param string $type
52
     * @param string $developerPayload
53
     *
54
     * @return bool
55
     */
56 1
    public function acknowledge(string $type = self::SUBSCRIPTION, string $developerPayload = '')
57
    {
58
        try {
59
            switch ($type) {
60 1
                case self::SUBSCRIPTION:
61 1
                    $this->androidPublisherService->purchases_subscriptions->acknowledge(
62 1
                        $this->packageName,
63 1
                        $this->productId,
64 1
                        $this->purchaseToken,
65 1
                        new \Google_Service_AndroidPublisher_SubscriptionPurchasesAcknowledgeRequest(
66 1
                            ['developerPayload' => $developerPayload]
67
                        )
68
                    );
69 1
                    break;
70 1
                case self::PRODUCT:
71 1
                    $this->androidPublisherService->purchases_products->acknowledge(
72 1
                        $this->packageName,
73 1
                        $this->productId,
74 1
                        $this->purchaseToken,
75 1
                        new \Google_Service_AndroidPublisher_ProductPurchasesAcknowledgeRequest(
76 1
                            ['developerPayload' => $developerPayload]
77
                        )
78
                    );
79 1
                    break;
80
                default:
81
                    throw new \RuntimeException(
82
                        \sprintf(
83
                            'Invalid type provided : %s expected %s',
84
                            $type,
85
                            implode(',', [self::PRODUCT, self::SUBSCRIPTION])
86
                        )
87
                    );
88
            }
89
90 1
            return true;
91
        } catch (\Exception $e) {
92
            throw new \RuntimeException($e->getMessage(), $e);
0 ignored issues
show
Bug introduced by
$e of type Exception is incompatible with the type integer expected by parameter $code of RuntimeException::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

92
            throw new \RuntimeException($e->getMessage(), /** @scrutinizer ignore-type */ $e);
Loading history...
93
        }
94
    }
95
}
96