Completed
Push — master ( 34c865...2a7f9b )
by Orkhan
03:57
created

ZipContent::readContent()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 7
c 2
b 0
f 0
dl 0
loc 13
rs 10
cc 2
nc 2
nop 1
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