Completed
Push — master ( 0ab606...221a77 )
by Andrea Marco
03:56 queued 11s
created

ValidatesInput::formatErrors()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
namespace Cerbero\CommandValidator;
4
5
use Symfony\Component\Console\Input\InputInterface;
6
use Symfony\Component\Console\Output\OutputInterface;
7
use Illuminate\Contracts\Validation\Validator;
8
use Symfony\Component\Console\Exception\InvalidArgumentException;
9
10
/**
11
 * The trait to validate console commands input.
12
 *
13
 */
14
trait ValidatesInput
15
{
16
    /**
17
     * The command input validator.
18
     *
19
     * @var \Illuminate\Contracts\Validation\Validator
20
     */
21
    protected $validator;
22
23
    /**
24
     * Retrieve the rules to validate data against
25
     *
26
     * @return array
27
     */
28
    abstract protected function rules(): array;
29
30
    /**
31
     * Execute the console command.
32
     *
33
     * @param  \Symfony\Component\Console\Input\InputInterface  $input
34
     * @param  \Symfony\Component\Console\Output\OutputInterface  $output
35
     * @return mixed
36
     * @throws \Symfony\Component\Console\Exception\InvalidArgumentException
37
     */
38 6
    protected function execute(InputInterface $input, OutputInterface $output)
39
    {
40 6
        if ($this->validator()->fails()) {
41 3
            throw new InvalidArgumentException($this->formatErrors());
42
        }
43
44 3
        return parent::execute($input, $output);
45
    }
46
47
    /**
48
     * Retrieve the command input validator
49
     *
50
     * @return \Illuminate\Contracts\Validation\Validator
51
     */
52 6
    protected function validator(): Validator
53
    {
54 6
        if (isset($this->validator)) {
55 3
            return $this->validator;
56
        }
57
58 6
        return $this->validator = $this->laravel['validator']->make(
59 6
            $this->getDataToValidate(),
60 6
            $this->rules(),
61 6
            $this->messages(),
62 6
            $this->attributes()
63
        );
64
    }
65
66
    /**
67
     * Retrieve the data to validate
68
     *
69
     * @return array
70
     */
71 6
    protected function getDataToValidate(): array
72
    {
73 6
        $data = array_merge($this->argument(), $this->option());
0 ignored issues
show
Bug introduced by
It seems like argument() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

73
        $data = array_merge($this->/** @scrutinizer ignore-call */ argument(), $this->option());
Loading history...
Bug introduced by
It seems like option() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

73
        $data = array_merge($this->argument(), $this->/** @scrutinizer ignore-call */ option());
Loading history...
74
75 4
        return array_filter($data, function ($value) {
76 6
            return $value !== null;
77 6
        });
78
    }
79
80
    /**
81
     * Format the validation errors
82
     *
83
     * @return string
84
     */
85 3
    protected function formatErrors(): string
86
    {
87 3
        return implode(PHP_EOL, $this->validator()->errors()->all());
88
    }
89
90
    /**
91
     * Retrieve the custom error messages
92
     *
93
     * @return array
94
     */
95
    protected function messages(): array
96
    {
97
        return [];
98
    }
99
100
    /**
101
     * Retrieve the custom attribute names for error messages
102
     *
103
     * @return array
104
     */
105
    protected function attributes(): array
106
    {
107
        return [];
108
    }
109
}
110