Completed
Branch 2.0 (238d23)
by Zhengchao
02:57 queued 02:05
created

UserAffiliate   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

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

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getAffiliateLink() 0 6 1
A referrals() 0 4 1
A bootUserAffiliate() 0 13 3
A generateAffiliateId() 0 4 1
1
<?php
2
3
/*
4
 * This file is part of questocat/laravel-referral package.
5
 *
6
 * (c) questocat <[email protected]>
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
namespace Questocat\Referral\Traits;
13
14
use Illuminate\Support\Facades\Cookie;
15
use Questocat\Referral\Referral;
16
use Ramsey\Uuid\Uuid;
17
18
trait UserAffiliate
19
{
20
    /**
21
     * @param string $path
22
     * @param array  $parameters
23
     * @param null   $secure
24
     *
25
     * @return string
26
     */
27
    public function getAffiliateLink($path = '/', $parameters = [], $secure = null)
28
    {
29
        $refQuery = config('referral.ref_query');
30
31
        return url($path, $parameters, $secure).'/?'.$refQuery.'='.$this->affiliate_id;
0 ignored issues
show
Bug introduced by
The property affiliate_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...
32
    }
33
34
    /**
35
     * @return mixed
36
     */
37
    public function referrals()
38
    {
39
        return $this->hasMany(Referral::class, 'referrer_id');
0 ignored issues
show
Bug introduced by
It seems like hasMany() 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...
40
    }
41
42
    protected static function bootUserAffiliate()
43
    {
44
        static::creating(function ($model) {
45
            $model->affiliate_id = self::generateAffiliateId();
46
        });
47
48
        static::created(function ($model) {
49
            $affiliateId = Cookie::get(config('referral.ref_cookie'));
50
            if ($affiliateId && $referrer = static::whereAffiliateId($affiliateId)->first()) {
51
                Referral::create(['referrer_id' => $referrer->id, 'referral_id' => $model->id]);
0 ignored issues
show
Bug introduced by
The method create() does not exist on Questocat\Referral\Referral. Did you maybe mean created()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
52
            }
53
        });
54
    }
55
56
    /**
57
     * Generate an affiliate id.
58
     *
59
     * @return \Ramsey\Uuid\UuidInterface
60
     *
61
     * @throws \Exception
62
     */
63
    protected static function generateAffiliateId()
64
    {
65
        return Uuid::uuid1();
66
    }
67
}
68