Completed
Push — master ( 1a94da...33e4a6 )
by Adrian
02:31
created

File::confirm()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Sirius\Upload\Result;
4
5
use Sirius\Upload\Container\ContainerInterface;
6
7
class File
8
{
9
10
    /**
11
     * Array containing the details of the uploaded file:
12
     * - name (uploaded name)
13
     * - original name
14
     * - tmp_name
15
     * etc
16
     *
17
     * @var array
18
     */
19
    protected $file;
20
21
    /**
22
     * The container to which this file belongs to
23
     * @var \Sirius\Upload\Container\ContainerInterface
24
     */
25
    protected $container;
26
27
    /**
28
     * @param $file
29
     * @param ContainerInterface $container
30
     */
31 11
    public function __construct($file, ContainerInterface $container)
32
    {
33 11
        $this->file = $file;
34 11
        $this->container = $container;
35 11
    }
36
37
    /**
38
     * Returns if the uploaded file is valid
39
     *
40
     * @return bool
41
     */
42 1
    public function isValid()
43
    {
44 1
        return $this->file['name'] && count($this->getMessages()) === 0;
45
    }
46
47
    /**
48
     * Returns the validation error messages
49
     *
50
     * @return array
51
     */
52 3
    public function getMessages()
53
    {
54 3
        if (isset($this->file['messages'])) {
55 2
            return $this->file['messages'];
56
        } else {
57 1
            return array();
58
        }
59
    }
60
61
    /**
62
     * The file that was saved during process() and has a .lock file attached
63
     * will be cleared, in case the form processing fails
64
     */
65 2
    public function clear()
66
    {
67 2
        $this->container->delete($this->name);
0 ignored issues
show
Documentation introduced by
The property name does not exist on object<Sirius\Upload\Result\File>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
68 2
        $this->container->delete($this->name . '.lock');
0 ignored issues
show
Documentation introduced by
The property name does not exist on object<Sirius\Upload\Result\File>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
69 2
        $this->file['name'] = null;
70 2
    }
71
72
    /**
73
     * Remove the .lock file attached to the file that was saved during process()
74
     * This should happen if the form fails validation/processing
75
     */
76 3
    public function confirm()
77
    {
78 3
        $this->container->delete($this->name . '.lock');
0 ignored issues
show
Documentation introduced by
The property name does not exist on object<Sirius\Upload\Result\File>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
79 3
    }
80
81
    /**
82
     * File attribute getter
83
     *
84
     * @param $name
85
     * @return mixed
86
     */
87 8
    public function __get($name)
88
    {
89 8
        if (isset($this->file[$name])) {
90 7
            return $this->file[$name];
91
        }
92
93 2
        return null;
94
    }
95
}
96