Completed
Push — master ( 76cc03...7ee247 )
by Zura
08:07
created

Rewardable::promocodes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace Gabievi\Promocodes\Traits;
4
5
use Carbon\Carbon;
6
use Gabievi\Promocodes\Model\Promocode;
7
use Gabievi\Promocodes\Facades\Promocodes;
8
9
trait Rewardable
10
{
11
    /**
12
     * Get the promocodes that are related to user.
13
     *
14
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
15
     */
16
    public function promocodes()
17
    {
18
        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...
19
    }
20
21
    /**
22
     * Apply promocode to user and get callback.
23
     *
24
     * @param string $code
25
     * @param null|\Closure $callback
26
     *
27
     * @return null|\Gabievi\Promocodes\Model\Promocode
28
     * @throws \Gabievi\Promocodes\Exceptions\AlreadyUsedExceprion
29
     */
30
    public function applyCode($code, $callback = null)
31
    {
32
        if ($promocode = Promocodes::check($code)) {
33
            if ($promocode->users()->wherePivot('user_id', $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...
34
                throw new AlreadyUsedExceprion;
35
            }
36
37
            $promocode->users()->attach($this->id, [
38
                'used_at' => Carbon::now(),
39
            ]);
40
41
            $promocode->load('users');
42
43
            if (is_callable($callback)) {
44
                $callback($promocode);
45
            }
46
47
            return $promocode;
48
        }
49
50
        if (is_callable($callback)) {
51
            $callback(null);
52
        }
53
54
        return null;
55
    }
56
}
57