Completed
Push — master ( d8e2a0...679457 )
by Mahmoud
04:23
created

PaymentsFactory::charge()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 3
nc 1
nop 3
1
<?php
2
3
namespace App\Containers\Payments\Tasks;
4
5
use App\Containers\Payments\Contracts\Chargeable;
6
use App\Containers\Payments\Exceptions\ObjectNonChargeableException;
7
use App\Containers\Payments\Exceptions\PaymentMethodNotFoundException;
8
use App\Containers\Payments\Exceptions\UserNotSetInThePaymentTaskException;
9
use App\Containers\Paypal\Tasks\ChargeWithPaypalTask;
10
use App\Containers\Stripe\Tasks\ChargeWithStripeTask;
11
use App\Containers\User\Models\User;
12
use Illuminate\Support\Facades\App;
13
14
/**
15
 * Class PaymentsFactory
16
 *
17
 * @author  Mahmoud Zalt  <[email protected]>
18
 */
19
class PaymentsFactory
20
{
21
22
    protected $method = null;
23
24
    /**
25
     * @param \App\Containers\User\Models\User $user
26
     * @param                                  $amount
27
     * @param string                           $currency
28
     *
29
     * @return  mixed
30
     */
31
    public function charge(User $user, $amount, $currency = 'USD')
32
    {
33
        $this->setUserPaymentMethod($user);
34
35
        return $this->method->charge($user, $amount, $currency);
36
    }
37
38
    /**
39
     * @param \App\Containers\User\Models\User $user
40
     */
41
    public function setUserPaymentMethod(User $user)
42
    {
43
        if ($user->stripeAccount !== null) {
0 ignored issues
show
Documentation introduced by
The property stripeAccount does not exist on object<App\Containers\User\Models\User>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
44
            $this->method = App::make(ChargeWithStripeTask::class);
45
        } elseif ($user->paypalAccount !== null) {
0 ignored issues
show
Documentation introduced by
The property paypalAccount does not exist on object<App\Containers\User\Models\User>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
46
            $this->method = App::make(ChargeWithPaypalTask::class);
47
        }
48
//        elseif ($user->...Account !== null) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
48% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
49
//            $this->method = App::make(ChargeWith...Task::class);
50
//        }
51
52
        // validate a payment method was found
53
        if ($this->method == null) {
54
            throw new PaymentMethodNotFoundException();
55
        }
56
57
        // validate containing charge function
58
        if (!$this->method instanceof Chargeable) {
59
            throw (new ObjectNonChargeableException())
60
                ->debug('The payment services must implement the Chargeable interface');
61
        }
62
    }
63
64
    /**
65
     * @return  null
66
     */
67
    public function getPaymentMethod()
68
    {
69
        return $this->method;
70
    }
71
72
}
73