Rewardable::applyCode()   B
last analyzed

Complexity

Conditions 7
Paths 7

Size

Total Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 32
rs 8.4746
c 0
b 0
f 0
cc 7
nc 7
nop 2
1
<?php
2
3
namespace Gabievi\Promocodes\Traits;
4
5
use Carbon\Carbon;
6
use Gabievi\Promocodes\Models\Promocode;
7
use Gabievi\Promocodes\Facades\Promocodes;
8
use Gabievi\Promocodes\Exceptions\AlreadyUsedException;
9
10
trait Rewardable
11
{
12
    /**
13
     * Get the promocodes that are related to user.
14
     *
15
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
16
     */
17
    public function promocodes()
18
    {
19
        return $this->belongsToMany(Promocode::class, config('promocodes.relation_table'))
0 ignored issues
show
Bug introduced by
It seems like belongsToMany() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
20
            ->withPivot('used_at');
21
    }
22
23
    /**
24
     * Redeem promocode to user and get callback.
25
     *
26
     * @param string $code
27
     * @param null|\Closure $callback
28
     *
29
     * @return null|\Gabievi\Promocodes\Models\Promocode
30
     * @throws AlreadyUsedException
31
     */
32
    public function redeemCode($code, $callback = null)
33
    {
34
        return $this->applyCode($code, $callback);
35
    }
36
37
    /**
38
     * Apply promocode to user and get callback.
39
     *
40
     * @param string $code
41
     * @param null|\Closure $callback
42
     *
43
     * @return bool|null|\Gabievi\Promocodes\Models\Promocode
44
     * @throws AlreadyUsedException
45
     */
46
    public function applyCode($code, $callback = null)
47
    {
48
        if ($promocode = Promocodes::check($code)) {
49
            if ($promocode->isDisposable() && $promocode->users()->wherePivot(config('promocodes.related_pivot_key'), $this->id)->exists()) {
0 ignored issues
show
Bug introduced by
The property id 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...
50
                throw new AlreadyUsedException;
51
            }
52
53
            $promocode->users()->attach($this->id, [
54
                config('promocodes.foreign_pivot_key') => $promocode->id,
55
                'used_at' => Carbon::now(),
56
            ]);
57
58
            if (!is_null($promocode->quantity)) {
59
                $promocode->quantity -= 1;
60
                $promocode->save();
61
            }
62
63
            $promocode->load('users');
64
65
            if (is_callable($callback)) {
66
                $callback($promocode);
67
            }
68
69
            return $promocode;
70
        }
71
72
        if (is_callable($callback)) {
73
            $callback(null);
74
        }
75
76
        return false;
77
    }
78
}
79