Completed
Branch master (5a81ac)
by Albert
02:06
created

VerifiesPurchase   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 0
dl 0
loc 62
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A verifyPurchase() 0 15 3
A invalidKeyResponse() 0 4 1
A getCustomer() 0 4 1
A validKeyResponse() 0 4 1
1
<?php
2
namespace Elimuswift\Core\Traits;
3
4
trait VerifiesPurchase {
5
6
	
7
	/**
8
	 * Customer Entity
9
	 *
10
	 * @var mixed
11
	 **/
12
	protected $customer;
13
14
	/**
15
	 * Verify validity of the purchase key and if valid create customer
16
	 *
17
	 * @return void
18
	 * 
19
	 **/
20
	public function verifyPurchase()
21
	{
22
		$this->getCustomer();
23
		if(null != $this->customer)
24
			return $this->validKeyResponse();
25
26
		$response = app()->envatoapi->verifyPurchase(request('code'));
27
		if('error' == $response['status'])
28
			return $this->invalidKeyResponse();
29
		// If we are here then the key is valid
30
		// We now create a new customer record from the envato api response
31
		$this->customer = $this->customers->createCustomer($response['response']);
0 ignored issues
show
Bug introduced by
The property customers does not seem to exist. Did you mean customer?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
32
		// After creation of customer details return the response back to the user
33
		return $this->validKeyResponse();
34
	}
35
36
	/**
37
	 * Return invalid key response
38
	 *
39
	 * @return Illuminate\Http\Response 
40
	 **/
41
	protected function invalidKeyResponse()
42
	{
43
		return response()->json(['status' => 'error', 'description' => 'Invalid purchase key'], 403);
44
	}
45
46
	/**
47
	 * Set the customer object
48
	 *
49
	 * @return void 
50
	 **/
51
	protected function getCustomer()
52
	{
53
		$this->customer = $this->customers->findCustomer('purchaseKey',request()->get('code'));
0 ignored issues
show
Bug introduced by
The property customers does not seem to exist. Did you mean customer?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
54
	}
55
56
	/**
57
	 * Return a success response if the purchase key is valid.
58
	 *
59
	 * @return Illuminate\Http\Response json object response 
60
	 **/
61
	protected function validKeyResponse()
62
	{
63
		return response()->json(['status' => 'success', 'description' => $this->customer->tojson()]);
64
	}
65
}