VerifiesPurchase   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

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

4 Methods

Rating   Name   Duplication   Size   Complexity  
A verifyPurchase() 0 18 3
A invalidKeyResponse() 0 4 1
A getCustomer() 0 4 1
A validKeyResponse() 0 4 1
1
<?php
2
3
namespace Elimuswift\Core\Traits;
4
5
trait VerifiesPurchase
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
    public function verifyPurchase()
18
    {
19
        $this->getCustomer();
20
        if (null != $this->customer) {
21
            return $this->validKeyResponse();
22
        }
23
24
        $response = app()->envatoapi->verifyPurchase(request('code'));
25
        if ('error' == $response['status']) {
26
            return $this->invalidKeyResponse();
27
        }
28
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
//end verifyPurchase()
37
38
    /**
39
     * Return invalid key response.
40
     *
41
     * @return Illuminate\Http\Response
42
     **/
43
    protected function invalidKeyResponse()
44
    {
45
        return response()->json(['status' => 'error', 'description' => 'Invalid purchase key'], 403);
46
    }
47
48
//end invalidKeyResponse()
49
50
    /**
51
     * Set the customer object.
52
     **/
53
    protected function getCustomer()
54
    {
55
        $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...
56
    }
57
58
//end getCustomer()
59
60
    /**
61
     * Return a success response if the purchase key is valid.
62
     *
63
     * @return Illuminate\Http\Response json object response
64
     **/
65
    protected function validKeyResponse()
66
    {
67
        return response()->json(['status' => 'success', 'description' => $this->customer->tojson()]);
68
    }
69
70
//end validKeyResponse()
71
}
72