Passed
Pull Request — master (#51)
by
unknown
11:58
created

Voucher   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 23
c 2
b 0
f 0
dl 0
loc 50
rs 10
wmc 9

2 Methods

Rating   Name   Duplication   Size   Complexity  
A message() 0 9 3
A passes() 0 21 6
1
<?php
2
3
namespace BeyondCode\Vouchers\Rules;
4
5
use Vouchers;
0 ignored issues
show
Bug introduced by
The type Vouchers was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use Illuminate\Contracts\Validation\Rule;
7
use BeyondCode\Vouchers\Exceptions\VoucherExpired;
8
use BeyondCode\Vouchers\Exceptions\VoucherIsInvalid;
9
use BeyondCode\Vouchers\Exceptions\VoucherAlreadyRedeemed;
10
11
class Voucher implements Rule
12
{
13
    protected $isInvalid = false;
14
    protected $isExpired = false;
15
    protected $wasRedeemed = false;
16
17
    /**
18
     * Determine if the validation rule passes.
19
     *
20
     * @param  string $attribute
21
     * @param  mixed $value
22
     * @return bool
23
     */
24
    public function passes($attribute, $value)
25
    {
26
        try {
27
            $voucher = Vouchers::check($value);
28
29
            // Check if the voucher was already redeemed
30
            if (auth()->check() && $voucher->users()->wherePivot('user_id', auth()->id())->exists()) {
31
                throw VoucherAlreadyRedeemed::create($voucher);
32
            }
33
        } catch (VoucherIsInvalid $exception) {
34
            $this->isInvalid = true;
35
            return false;
36
        } catch (VoucherExpired $exception) {
37
            $this->isExpired = true;
38
            return false;
39
        } catch (VoucherAlreadyRedeemed $exception) {
40
            $this->wasRedeemed = true;
41
            return false;
42
        }
43
44
        return true;
45
    }
46
47
    /**
48
     * Get the validation error message.
49
     *
50
     * @return string
51
     */
52
    public function message()
53
    {
54
        if ($this->wasRedeemed) {
55
            return trans('vouchers::validation.code_redeemed');
0 ignored issues
show
Bug Best Practice introduced by
The expression return trans('vouchers::...idation.code_redeemed') also could return the type array which is incompatible with the documented return type string.
Loading history...
56
        }
57
        if ($this->isExpired) {
58
            return trans('vouchers::validation.code_expired');
0 ignored issues
show
Bug Best Practice introduced by
The expression return trans('vouchers::validation.code_expired') also could return the type array which is incompatible with the documented return type string.
Loading history...
59
        }
60
        return trans('vouchers::validation.code_invalid');
0 ignored issues
show
Bug Best Practice introduced by
The expression return trans('vouchers::validation.code_invalid') also could return the type array which is incompatible with the documented return type string.
Loading history...
61
    }
62
}
63