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

ConfirmationNeeded::__construct()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 19
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

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