ReceivesApplications::receivedApplicationsFrom()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 24

Duplication

Lines 12
Ratio 50 %

Importance

Changes 0
Metric Value
dl 12
loc 24
rs 9.536
c 0
b 0
f 0
cc 3
nc 4
nop 2
1
<?php
2
3
namespace Te7aHoudini\LaravelApplicant\Traits;
4
5
use Illuminate\Support\Arr;
6
use Illuminate\Support\Carbon;
7
use Te7aHoudini\LaravelApplicant\Models\Application;
8
9
trait ReceivesApplications
10
{
11
    /**
12
     * updates application record in db.
13
     *
14
     * @param array|object $model
15
     * @param array $criteria
16
     * @return bool
17
     */
18
    public function processApplicationFrom($model, $criteria = [], $newStatus = 'processed')
19
    {
20
        $applicationType = Arr::get($criteria, 'type', Arr::get($this->receiverCriteria, 'type', 'applicant'));
0 ignored issues
show
Bug introduced by
The property receiverCriteria 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...
21
22
        $applicationStatus = Arr::get($criteria, 'status', Arr::get($this->receiverCriteria, 'status', 'created'));
23
24
        $query = Application::query();
25
26 View Code Duplication
        if (is_array($model)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
27
            $query->where('type', $model['type'] ?? $applicationType)
28
                ->where('status', $model['status'] ?? $applicationStatus)
29
                ->where('applicant_id', $model['applicant_id'])
30
                ->where('applicant_type', $model['applicant_type']);
31
        }
32
33 View Code Duplication
        if (is_object($model)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
34
            $query->where('type', $applicationType)
35
                ->where('status', $applicationStatus)
36
                ->where('applicant_id', $model->id)
37
                ->where('applicant_type', get_class($model));
38
        }
39
40
        return $query->update([
41
            'status' => is_string($criteria) ? $criteria : $newStatus,
42
            'status_updated_by_id' => $this->id,
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...
43
            'status_updated_by_type' => get_class($this),
44
            'status_updated_at' => Carbon::now(),
45
        ]);
46
    }
47
48
    /**
49
     *  check if current model has received application from other model or not.
50
     *
51
     * @param array|object $model
52
     * @param array $criteria
53
     * @return bool
54
     */
55
    public function hasReceivedApplicationFrom($model, $criteria = [])
56
    {
57
        return $this->receivedApplicationsFrom($model, $criteria)->exists();
58
    }
59
60
    /**
61
     * get applications that applied on this model and this model was the receiver.
62
     *
63
     * @param array|object $model
64
     * @param array $criteria
65
     * @return \Illuminate\Database\Eloquent\Builder
66
     */
67
    public function receivedApplicationsFrom($model, $criteria = [])
68
    {
69
        $applicationType = Arr::get($criteria, 'type', Arr::get($this->applicantCriteria, 'type', 'applicant'));
0 ignored issues
show
Bug introduced by
The property applicantCriteria 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...
70
71
        $applicationStatus = Arr::get($criteria, 'status', Arr::get($this->applicantCriteria, 'status', 'created'));
72
73
        $query = $this->receivedApplications();
74
75 View Code Duplication
        if (is_array($model)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
76
            $query->where('type', $model['type'] ?? $applicationType)
77
                ->where('status', $model['status'] ?? $applicationStatus)
78
                ->where('applicant_id', $model['applicant_id'])
79
                ->where('applicant_type', $model['applicant_type']);
80
        }
81
82 View Code Duplication
        if (is_object($model)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
83
            $query->where('type', $applicationType)
84
                ->where('status', $applicationStatus)
85
                ->where('applicant_id', $model->id)
86
                ->where('applicant_type', get_class($model));
87
        }
88
89
        return $query;
90
    }
91
92
    /**
93
     * sets receiverCriteria attribute and returns $this to allow fluent api.
94
     *
95
     * @param array $criteria
96
     * @return self
97
     */
98
    public function setReceiverCriteria($criteria = [])
99
    {
100
        $this->receiverCriteria = $criteria;
101
102
        return $this;
103
    }
104
105
    /**
106
     * returns received applications for that model.
107
     *
108
     * @return \Illuminate\Database\Eloquent\Relations\MorphMany
109
     */
110
    public function receivedApplications()
111
    {
112
        return $this->morphMany(Application::class, 'receiver');
0 ignored issues
show
Bug introduced by
It seems like morphMany() 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...
113
    }
114
}
115