TestimonyObserver   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 0
Metric Value
wmc 5
lcom 0
cbo 3
dl 0
loc 47
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A created() 0 9 2
A updated() 0 4 1
A deleted() 0 9 2
1
<?php
2
3
namespace Faithgen\Testimonies\Observers;
4
5
use FaithGen\SDK\Traits\FileTraits;
6
use Faithgen\Testimonies\Jobs\ProcessImages;
7
use Faithgen\Testimonies\Jobs\S3Upload;
8
use Faithgen\Testimonies\Jobs\UploadImages;
9
use Faithgen\Testimonies\Models\Testimony;
10
use Illuminate\Support\Facades\DB;
11
12
final class TestimonyObserver
13
{
14
    use FileTraits;
15
16
    /**
17
     * Handle the testimony "created" event.
18
     *
19
     * @param  \Faithgen\Testimonies\Models\Testimony  $testimony
20
     * @return void
21
     */
22
    public function created(Testimony $testimony)
23
    {
24
        if (auth()->user()->account->level !== 'Free') {
0 ignored issues
show
Bug introduced by
The method user does only exist in Illuminate\Contracts\Auth\Guard, but not in Illuminate\Contracts\Auth\Factory.

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...
25
            UploadImages::withChain([
26
                new ProcessImages($testimony),
27
                new S3Upload(),
28
            ])->dispatch($testimony, request('images'));
29
        }
30
    }
31
32
    /**
33
     * Handle the testimony "updated" event.
34
     *
35
     * @param  \Faithgen\Testimonies\Models\Testimony  $testimony
36
     * @return void
37
     */
38
    public function updated(Testimony $testimony)
39
    {
40
        //
41
    }
42
43
    /**
44
     * Handle the testimony "deleted" event.
45
     *
46
     * @param  \Faithgen\Testimonies\Models\Testimony  $testimony
47
     * @return void
48
     */
49
    public function deleted(Testimony $testimony)
50
    {
51
        if ($testimony->images()->exists()) {
52
            $this->deleteFiles($testimony);
53
            DB::table('images')
54
                ->whereIn('id', $testimony->images()->pluck('id')->toArray())
55
                ->delete();
56
        }
57
    }
58
}
59