VerifyDocuments::isValidEvidenceFile()   B
last analyzed

Complexity

Conditions 7
Paths 24

Size

Total Lines 29
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 17
c 1
b 0
f 0
dl 0
loc 29
rs 8.8333
cc 7
nc 24
nop 1
1
<?php
2
3
namespace Srmklive\PayPal\Services;
4
5
use GuzzleHttp\Psr7\MimeType;
6
7
class VerifyDocuments
8
{
9
    /**
10
     * @var array
11
     */
12
    protected static $dispute_evidence_types = [
13
        'application/pdf',
14
        'image/gif',
15
        'image/jpeg',
16
        'image/png',
17
    ];
18
19
    /**
20
     * @var string
21
     */
22
    protected static $dispute_evidence_file_size = 10;
23
24
    /**
25
     * @var string
26
     */
27
    protected static $dispute_evidences_size = 50;
28
29
    /**
30
     * Get Mime type from filename.
31
     *
32
     * @param string $file
33
     *
34
     * @return string
35
     */
36
    public static function getMimeType($file)
37
    {
38
        return MimeType::fromFilename($file);
39
    }
40
41
    /**
42
     * Check if the evidence file being submitted mime type is valid.
43
     *
44
     * @param array $files
45
     *
46
     * @return bool
47
     */
48
    public static function isValidEvidenceFile(array $files)
49
    {
50
        $validFile = true;
51
        $validSize = true;
52
        $total_size = 0;
53
54
        $basic = (1024 * 1024);
55
        $file_size = $basic * self::$dispute_evidence_file_size;
56
        $overall_size = $basic * self::$dispute_evidences_size;
57
58
        foreach ($files as $file) {
59
            $mime_type = self::getMimeType($file);
60
61
            if (!in_array($mime_type, self::$dispute_evidence_types)) {
62
                $validFile = false;
63
                break;
64
            }
65
66
            $size = filesize($file);
67
68
            if ($size > $file_size) {
69
                $validSize = false;
70
                break;
71
            }
72
73
            $total_size += $size;
74
        }
75
76
        return (($validFile === false) || ($validSize === false)) || ($total_size > $overall_size) ? false : true;
77
    }
78
}
79