Image::isValid()   B
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 22
Code Lines 9

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 22
rs 8.9197
cc 4
eloc 9
nc 4
nop 0
1
<?php
2
3
/**
4
 * @author: Abdul Qureshi. <[email protected]>
5
 * 
6
 * This file has been modified from the original source.
7
 * See original here:
8
 *
9
 * @link: https://github.com/progsmile/request-validator
10
 */
11
12
namespace TheSupportGroup\Common\Validator\Rules;
13
14
use TheSupportGroup\Common\Validator\Contracts\Rules\RuleInterface;
15
16
class Image extends BaseRule implements RuleInterface
17
{
18
    public function isValid()
0 ignored issues
show
Coding Style introduced by
isValid uses the super-global variable $_FILES which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
19
    {
20
        //file not required and not send by user
21
        if ($this->isNotRequiredAndEmpty('file')) {
22
            return true;
23
        }
24
25
        $fileField = $this->getParams()[0];
26
27
        //uploading error: file is too big, or permissions, etc..
28
        if (!isset($_FILES[$fileField])) {
29
            return false;
30
        }
31
32
        if ($_FILES[$fileField]['tmp_name']) {
33
34
            //if file is image
35
            return is_array(getimagesize($_FILES[$fileField]['tmp_name']));
36
        }
37
38
        return false;
39
    }
40
41
    public function getMessage()
42
    {
43
        return 'Field :field: is not image or there are upload troubles';
44
    }
45
}
46