Passed
Push — develop ( 7be6d2...4b290b )
by Andrea Marco
04:56
created

ValidatesInput::formatErrors()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 5
ccs 3
cts 3
cp 1
crap 1
rs 10
c 0
b 0
f 0
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
9
/**
10
 * The trait to validate console commands input.
11
 *
12
 */
13
trait ValidatesInput
14
{
15
    /**
16
     * The command input validator.
17
     *
18
     * @var \Illuminate\Contracts\Validation\Validator
19
     */
20
    protected $validator;
21
22
    /**
23
     * Retrieve the rules to validate data against
24
     *
25
     * @return array
26
     */
27
    abstract protected function rules() : array;
28
29
    /**
30
     * Execute the console command.
31
     *
32
     * @param  \Symfony\Component\Console\Input\InputInterface  $input
33
     * @param  \Symfony\Component\Console\Output\OutputInterface  $output
34
     * @return mixed
35
     */
36 6
    protected function execute(InputInterface $input, OutputInterface $output)
37
    {
38 6
        if ($this->validator()->fails()) {
39 3
            $this->error($this->formatErrors());
0 ignored issues
show
Bug introduced by
It seems like error() 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

39
            $this->/** @scrutinizer ignore-call */ 
40
                   error($this->formatErrors());
Loading history...
40
            // Exit with status code 1
41 3
            return 1;
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
     * Retrieve the custom error messages
82
     *
83
     * @return array
84
     */
85
    protected function messages() : array
86
    {
87
        return [];
88
    }
89
90
    /**
91
     * Retrieve the custom attribute names for error messages
92
     *
93
     * @return array
94
     */
95
    protected function attributes() : array
96
    {
97
        return [];
98
    }
99
100
    /**
101
     * Format the validation errors
102
     *
103
     * @return string
104
     */
105 3
    protected function formatErrors() : string
106
    {
107 3
        $errors = implode(PHP_EOL, $this->getErrorMessages());
108
109 3
        return PHP_EOL . PHP_EOL . $errors . PHP_EOL;
110
    }
111
112
    /**
113
     * Retrieve the error messages
114
     *
115
     * @return array
116
     */
117 3
    protected function getErrorMessages() : array
118
    {
119 3
        return $this->validator()->errors()->all();
120
    }
121
}
122