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

AffiliateTracking   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
wmc 9
lcom 0
cbo 1
dl 0
loc 57
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A handle() 0 15 3
A hasStoreAffiliateId() 0 10 4
A storeAffiliateId() 0 10 2
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\Http\Middleware;
13
14
use Closure;
15
use Illuminate\Support\Facades\Cookie;
16
17
class AffiliateTracking
18
{
19
    /**
20
     * @param         $request
21
     * @param Closure $next
22
     *
23
     * @return $this|mixed
24
     */
25
    public function handle($request, Closure $next)
26
    {
27
        $response = $next($request);
28
29
        $ref = $request->query(config('referral.ref_query'));
30
31
        if ($ref && $this->hasStoreAffiliateId($ref)) {
32
            $minutes = config('referral.lifetime_minutes', 0);
33
            $cookie = $this->storeAffiliateId($ref, $minutes);
34
35
            return $response->withCookie($cookie);
36
        }
37
38
        return $response;
39
    }
40
41
    /**
42
     * @param $ref
43
     *
44
     * @return bool
45
     */
46
    public function hasStoreAffiliateId($ref)
47
    {
48
        if ($affiliateId = Cookie::get(config('referral.ref_cookie'))) {
49
            if (0 === strcmp($affiliateId, $ref) || false === config('referral.credit_last_referrer')) {
50
                return false;
51
            }
52
        }
53
54
        return true;
55
    }
56
57
    /**
58
     * @param     $ref
59
     * @param int $lifetime
60
     *
61
     * @return \Illuminate\Cookie\CookieJar|\Symfony\Component\HttpFoundation\Cookie
62
     */
63
    protected function storeAffiliateId($ref, $lifetime = 0)
64
    {
65
        $name = config('referral.ref_cookie');
66
67
        if ($lifetime > 0) {
68
            return cookie($name, $ref, $lifetime);
69
        }
70
71
        return cookie()->forever($name, $ref);
0 ignored issues
show
Bug introduced by
The method forever does only exist in Illuminate\Cookie\CookieJar, but not in Symfony\Component\HttpFoundation\Cookie.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
72
    }
73
}
74