UserExists::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 4
c 1
b 0
f 1
nc 1
nop 3
dl 0
loc 6
rs 10
1
<?php
2
3
namespace App\Rules;
4
5
use App\User;
6
use Illuminate\Contracts\Validation\Rule;
7
8
class UserExists implements Rule
9
{
10
    protected $alma;
11
    protected $user;
12
13
    /**
14
     * Create a new rule instance.
15
     *
16
     * @param User $user
17
     * @param array $suggestions
18
     * @param bool $alma
19
     */
20
    public function __construct(User $user = null, $suggestions = [], $alma = false)
21
    {
22
        $this->alma = $alma;
23
        $this->user = $user;
24
        $suggestions['user'] = '_new';
25
        $this->suggestions = $suggestions;
0 ignored issues
show
Bug Best Practice introduced by
The property suggestions does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
26
    }
27
28
    /**
29
     * Determine if the validation rule passes.
30
     *
31
     * @param  string  $attribute
32
     * @param  mixed  $value
33
     * @return bool
34
     */
35
    public function passes($attribute, $value)
36
    {
37
        return !is_null($this->user);
38
    }
39
40
    /**
41
     * Get the validation error message.
42
     *
43
     * @return string
44
     */
45
    public function message()
46
    {
47
        $whereWeLooked = $this->alma ? ' lokalt eller i Alma' : '';
48
49
        if (isset($this->suggestions['barcode'])) {
50
            return sprintf(
51
                'Strekkoden ble ikke funnet%s. ' .
52
                'Sjekk om brukeren har fått nytt kort ved å søke på brukerens navn, eller ' .
53
                '<a href="%s">opprett en lokal bruker</a>.',
54
                $whereWeLooked,
55
                action('UsersController@getEdit', $this->suggestions)
56
            );
57
        }
58
59
        return sprintf(
60
            'Brukeren ble ikke funnet%s. ' .
61
            'Du kan registrere hen i <a href="%s" target="_blank">BIM</a> eller evt. ' .
62
            '<a href="%s">opprette en lokal bruker</a>.',
63
            $whereWeLooked,
64
            'https://bim.bibsys.no/',
65
            action('UsersController@getEdit', $this->suggestions)
66
        );
67
    }
68
}
69