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

VerifiesPurchase::verifyPurchase()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 9
nc 3
nop 0
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
}