Completed
Push — master ( 65eea1...15154e )
by Nikan
01:08
created

LaravelCafebazaar   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 5
dl 0
loc 80
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
B updateToken() 0 44 6
A getCache() 0 3 1
A setCache() 0 3 1
A getCode() 0 3 1
A verifyPurchase() 0 6 1
A handleRedirect() 0 6 2
1
<?php
2
3
namespace Nikandlv\LaravelCafebazaar;
4
5
use Exception;
6
use GuzzleHttp\Exception\RequestException;
7
use Illuminate\Support\Facades\Cache;
8
use Illuminate\Http\Request;
9
10
class LaravelCafebazaar {
11
    
12
    function __construct() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
13
        $this->guzzle = new \GuzzleHttp\Client(["base_uri" => "https://pardakht.cafebazaar.ir/devapi/v2/"]);
0 ignored issues
show
Bug introduced by
The property guzzle does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
14
        $this->data = $this->getCache();
0 ignored issues
show
Bug introduced by
The property data does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
15
        $this->code = $this->getCode();
0 ignored issues
show
Bug introduced by
The property code does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
16
        $this->updateToken();
17
    }
18
19
    protected function updateToken() {
20
21
        if(empty($this->code)) {  
22
            throw new Exception("Code not found. run php artisan Cafebazaar code");
23
        }
24
25
        if(empty($this->data)) {
26
            try {
27
                $response = $this->guzzle->post("auth/token/", [
28
                    'form_params' => [
29
                        "grant_type" => 'authorization_code',
30
                        'code' => $this->code,
31
                        "client_id" => config('laravel-cafebazaar.client_id'),
32
                        "client_secret" => config('laravel-cafebazaar.client_secret'),
33
                        "redirect_uri" => config('laravel-cafebazaar.redirect_uri'),    
34
                   ],
35
                ]);
36
                $this->data = json_decode($response->getBody()->getContents());
37
                $this->setCache($this->data);
38
            } catch (RequestException $exception) {
39
                throw $exception;
40
            }
41
        } else {
42
            try {
43
                if(!isset($this->data->refresh_token)) {
44
                    return;
45
                }
46
                $refresh_token = $this->data->refresh_token;
47
                $response = $this->guzzle->post("auth/token/", [
48
                    'form_params' => [
49
                        "grant_type" => 'refresh_token',
50
                        "client_id" => config('laravel-cafebazaar.client_id'),
51
                        "client_secret" => config('laravel-cafebazaar.client_secret'),
52
                        "refresh_token" => $refresh_token
53
                   ],
54
                ]);
55
                $this->data = json_decode($response->getBody()->getContents());
56
                $this->data->refresh_token = $refresh_token;
57
                $this->setCache($this->data);
58
            } catch (RequestException $exception) {
59
                throw $exception;
60
            }
61
        }
62
    }
63
64
    protected function getCache() {
65
        return Cache::get('laravel-cafebazaar');
66
    }
67
68
    protected function setCache($cache) {
69
        Cache::forever('laravel-cafebazaar', $cache);
70
    }
71
72
    protected function getCode() {
73
        return Cache::get('laravel-cafebazaar-code');
74
    }
75
    
76
    public function verifyPurchase($package_id, $product_id, $purchase_token) {
77
        $this->updateToken();
78
        $response = $this->guzzle->get("api/validate/$package_id/inapp/$product_id/purchases/$purchase_token?access_token=".$this->data->access_token);
79
        $data = json_decode($response->getBody()->getContents());
80
        return new CafebazaarPurchase($data);
81
    }
82
83
    public static function handleRedirect(Request $request) {
84
        $code = $request->input('code');
85
        if(!empty($code)) {
86
            Cache::forever('laravel-cafebazaar-code', $code);
87
        }
88
    }
89
}
90