Taxable::syncTaxGroups()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 6
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
3
namespace FeiMx\Tax\Traits;
4
5
use FeiMx\Tax\Exceptions\TaxErrorException;
6
use FeiMx\Tax\Models\TaxGroup;
7
use Illuminate\Database\Eloquent\Relations\MorphToMany;
8
9
trait Taxable
10
{
11
    /**
12
     * Column name used for get the price of the model.
13
     *
14
     * @return string Column name
15
     */
16
    public static function priceColumn()
17
    {
18
        return 'price';
19
    }
20
21
    /**
22
     * [taxGroups description].
23
     *
24
     * @return [type] [description]
0 ignored issues
show
Documentation introduced by
The doc-type [type] could not be parsed: Unknown type name "" at position 0. [(view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
25
     */
26
    public function taxGroups(): MorphToMany
27
    {
28
        return $this->morphToMany(
0 ignored issues
show
Bug introduced by
It seems like morphToMany() 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...
29
            TaxGroup::class,
30
            'model',
31
            'model_has_tax_groups',
32
            'model_id',
33
            'tax_group_id'
34
        );
35
    }
36
37
    public function hasTaxGroups()
38
    {
39
        return (bool) $this->taxGroups->count();
0 ignored issues
show
Bug introduced by
The property taxGroups 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...
40
    }
41
42
    /**
43
     * Determine if the model has (one of) the given tax group(s).
44
     *
45
     * @param string|array|\FeiMx\Tax\Models\TaxGroup|\Illuminate\Support\Collection $taxGroups
46
     *
47
     * @return bool
48
     */
49
    public function hasTaxGroup($taxGroups)
50
    {
51
        if (is_string($taxGroups)) {
52
            return $this->taxGroups->contains('name', $taxGroups);
53
        }
54
        if ($taxGroups instanceof TaxGroup) {
55
            return $this->taxGroups->contains('id', $taxGroups->id);
56
        }
57
        if (is_array($taxGroups)) {
58
            foreach ($taxGroups as $taxGroup) {
59
                if ($this->hasTaxGroup($taxGroup)) {
60
                    return true;
61
                }
62
            }
63
64
            return false;
65
        }
66
67
        return $taxGroups->intersect($this->taxGroups)->isNotEmpty();
68
    }
69
70
    /**
71
     * Assign the given taxGroup to the model.
72
     *
73
     * @param array|string|\FeiMx\Tax\Models\TaxGroup ...$taxGroups
74
     *
75
     * @return $this
76
     */
77
    public function assignTaxGroup(...$taxGroups)
78
    {
79
        if (0 == count($taxGroups)) {
80
            throw new TaxErrorException('You must pass a valid TaxGroup');
81
        }
82
83
        $taxGroups = collect($taxGroups)
84
            ->flatten()
85
            ->map(function ($taxGroup) {
86
                return $this->getStoredTaxGroup($taxGroup);
87
            })
88
            ->all();
89
90
        $this->taxGroups()->saveMany($taxGroups);
91
92
        return $this;
93
    }
94
95
    /**
96
     * Revoke the given role from the model.
97
     *
98
     * @param string|\FeiMx\Tax\Models\TaxGroup $taxGroup
99
     */
100
    public function removeTaxGroup($taxGroup)
101
    {
102
        $this->taxGroups()->detach($this->getStoredTaxGroup($taxGroup));
103
    }
104
105
    /**
106
     * Remove all current tax groups and set the given ones.
107
     *
108
     * @param array|\FeiMx\Tax\Models\TaxGroup|string ...$taxGroups
109
     *
110
     * @return $this
111
     */
112
    public function syncTaxGroups(...$taxGroups)
113
    {
114
        $this->taxGroups()->detach();
115
116
        return $this->assignTaxGroup($taxGroups);
117
    }
118
119
    /**
120
     * Get total amount for current TaxGroup.
121
     *
122
     * @param \FeiMx\Tax\Models\TaxGroup|string $taxGroup
123
     *
124
     * @return $total
0 ignored issues
show
Documentation introduced by
The doc-type $total could not be parsed: Unknown type name "$total" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
125
     */
126 View Code Duplication
    public function total($taxGroup = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
127
    {
128
        if (null === $taxGroup) {
129
            throw new TaxErrorException('You must pass a valid TaxGroup');
130
        }
131
132
        $taxGroup = $this->getStoredTaxGroup($taxGroup);
133
        $column = self::priceColumn();
134
135
        return $total = $taxGroup->taxManager($this->{$column})
0 ignored issues
show
Unused Code introduced by
$total is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
136
            ->addTaxes(
137
                $taxGroup->taxes->map(function ($tax) {
138
                    return $tax->info();
139
                })
140
                ->all()
141
            )
142
            ->total();
143
    }
144
145
    /**
146
     * Get a list of taxes with amount calculated for the given TaxGroup.
147
     *
148
     * @param \FeiMx\Tax\Models\TaxGroup|string $taxGroup
149
     *
150
     * @return $total
0 ignored issues
show
Documentation introduced by
The doc-type $total could not be parsed: Unknown type name "$total" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
151
     */
152 View Code Duplication
    public function getAmounts($taxGroup = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
153
    {
154
        if (null === $taxGroup) {
155
            throw new TaxErrorException('You must pass a valid TaxGroup');
156
        }
157
158
        $taxGroup = $this->getStoredTaxGroup($taxGroup);
159
        $column = self::priceColumn();
160
161
        return $taxGroup->taxManager($this->{$column})
162
            ->addTaxes(
163
                $taxGroup->taxes->map(function ($tax) {
164
                    return $tax->info();
165
                })
166
                ->all()
167
            )
168
            ->get();
169
    }
170
171
    protected function getStoredTaxGroup($taxGroup): TaxGroup
172
    {
173
        if (is_numeric($taxGroup)) {
174
            return TaxGroup::find($taxGroup);
175
        }
176
        if (is_string($taxGroup)) {
177
            return TaxGroup::whereName($taxGroup)->first();
178
        }
179
180
        return $taxGroup;
181
    }
182
}
183