Passed
Push — master ( af710d...2c0b17 )
by Dan Michael O.
08:29
created

TemporaryBarcodeExists   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 12
dl 0
loc 48
rs 10
c 0
b 0
f 0
wmc 6

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getNormalizedValue() 0 3 1
A message() 0 3 1
A __construct() 0 4 1
A passes() 0 13 3
1
<?php
2
3
namespace App\Rules;
4
5
use Illuminate\Contracts\Validation\Rule;
0 ignored issues
show
Bug introduced by
The type Illuminate\Contracts\Validation\Rule 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 Scriptotek\Alma\Client as AlmaClient;
0 ignored issues
show
Bug introduced by
The type Scriptotek\Alma\Client 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...
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