TemporaryBarcodeExists::getNormalizedValue()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
namespace App\Rules;
4
5
use Illuminate\Contracts\Validation\Rule;
6
use Scriptotek\Alma\Client as AlmaClient;
7
8
class TemporaryBarcodeExists implements Rule
9
{
10
    /**
11
     * Create a new rule instance.
12
     *
13
     * @return void
14
     */
15
    public function __construct(AlmaClient $alma)
16
    {
17
        $this->alma = $alma;
0 ignored issues
show
Bug Best Practice introduced by
The property alma does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
18
        $this->normalizedValue = null;
0 ignored issues
show
Bug Best Practice introduced by
The property normalizedValue does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
19
    }
20
21
    /**
22
     * Determine if the validation rule passes.
23
     *
24
     * @param  string  $attribute
25
     * @param  mixed  $value
26
     * @return bool
27
     */
28
    public function passes($attribute, $value)
29
    {
30
        if (empty($value)) {
31
            return true;
32
        }
33
34
        $almaUser = $this->alma->users->findOne('ALL~' . $value);
35
        if (!is_null($almaUser)) {
36
            $this->normalizedValue = $almaUser->getPrimaryId();
0 ignored issues
show
Bug Best Practice introduced by
The property normalizedValue does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
37
            return true;
38
        }
39
40
        return false;
41
    }
42
43
    /**
44
     * Get the validation error message.
45
     *
46
     * @return string
47
     */
48
    public function message()
49
    {
50
        return 'Det midlertidige lånekortet må eksistere i Alma.';
51
    }
52
53
    public function getNormalizedValue()
54
    {
55
        return $this->normalizedValue;
56
    }
57
}
58