ZipContent   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 10
dl 0
loc 48
rs 10
c 3
b 0
f 0
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A passes() 0 5 1
A message() 0 4 1
A __construct() 0 5 2
1
<?php
2
3
namespace Orkhanahmadov\ZipValidator\Rules;
4
5
use Illuminate\Contracts\Validation\Rule;
6
use Orkhanahmadov\ZipValidator\Validator;
7
8
class ZipContent implements Rule
9
{
10
    /**
11
     * @var Validator
12
     */
13
    private $validator;
14
    /**
15
     * @var \Illuminate\Support\Collection
16
     */
17
    private $failedFiles;
18
19
    /**
20
     * Create a new rule instance.
21
     *
22
     * @param array|string $files
23
     * @param bool|string $allowEmpty
24
     */
25
    public function __construct($files, $allowEmpty = true)
26
    {
27
        $this->validator = new Validator(
28
            is_bool($allowEmpty) ? $files : func_get_args(),
29
            $allowEmpty
30
        );
31
    }
32
33
    /**
34
     * Determine if the validation rule passes.
35
     *
36
     * @param string $attribute
37
     * @param \Illuminate\Http\UploadedFile $zipFile
38
     * @return bool
39
     */
40
    public function passes($attribute, $zipFile): bool
41
    {
42
        $this->failedFiles = $this->validator->validate($zipFile->path());
43
44
        return $this->failedFiles->count() === 0;
45
    }
46
47
    /**
48
     * Get the validation error message.
49
     *
50
     * @return string
51
     */
52
    public function message(): string
53
    {
54
        return __('zipValidator::messages.failed', [
55
            'files' => $this->failedFiles->implode(', '),
56
        ]);
57
    }
58
}
59