ConfirmationNeeded   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 20
c 1
b 0
f 0
dl 0
loc 59
rs 10
wmc 8

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 19 4
A passes() 0 10 3
A message() 0 3 1
1
<?php
2
3
namespace App\Rules;
4
5
use App\User;
6
use Illuminate\Contracts\Validation\Rule;
7
use Illuminate\Support\Arr;
8
9
class ConfirmationNeeded implements Rule
10
{
11
    protected $problems = [];
12
13
    /**
14
     * Create a new rule instance.
15
     *
16
     * @param User|null $user
17
     */
18
    public function __construct(User $user = null)
19
    {
20
        if (is_null($user)) {
21
            return;
22
        }
23
24
        if ($user->hasFees()) {
25
            $this->problems[] = sprintf('Brukeren har %d,- i utestående gebyr i Alma.', $user->fees);
26
        }
27
        if (count($user->blocks)) {
28
            $msgs = array_values(array_map(
29
                function ($b) {
30
                    return Arr::get($b, 'block_description.desc');
31
                },
32
                $user->blocks
33
            ));
34
            $this->problems[] = sprintf(
35
                'Brukeren har følgende merknader: <ul><li>%s</li></ul>',
36
                implode('</li><li>', $msgs)
37
            );
38
        }
39
    }
40
41
    /**
42
     * Determine if the validation rule passes.
43
     *
44
     * @param  string  $attribute
45
     * @param  mixed  $value
46
     * @return bool
47
     */
48
    public function passes($attribute, $value)
49
    {
50
        if (!count($this->problems)) {
51
            return true;
52
        }
53
        if ($value == 'checked') {
54
            return true;
55
        }
56
57
        return false;
58
    }
59
60
    /**
61
     * Get the validation error message.
62
     *
63
     * @return string
64
     */
65
    public function message()
66
    {
67
        return "<p>" . implode("</p><p>", $this->problems) . "</p>";
68
    }
69
}
70