File   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 89
c 0
b 0
f 0
wmc 9
lcom 1
cbo 1
ccs 22
cts 22
cp 1
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A isValid() 0 4 2
A getMessages() 0 8 2
A clear() 0 6 1
A confirm() 0 4 1
A __get() 0 8 2
1
<?php
2
declare(strict_types=1);
3
4
namespace Sirius\Upload\Result;
5
6
use Sirius\Upload\Container\ContainerInterface;
7
8
class File implements ResultInterface
9
{
10
11
    /**
12
     * Array containing the details of the uploaded file:
13
     * - name (uploaded name)
14
     * - original name
15
     * - tmp_name
16
     * etc
17
     *
18
     * @var array
19
     */
20
    protected $file;
21
22
    /**
23
     * The container to which this file belongs to
24
     * @var ContainerInterface
25
     */
26
    protected $container;
27
28
    /**
29
     * @param $file
30
     * @param ContainerInterface $container
31 11
     */
32
    public function __construct($file, ContainerInterface $container)
33 11
    {
34 11
        $this->file = $file;
35 11
        $this->container = $container;
36
    }
37
38
    /**
39
     * Returns if the uploaded file is valid
40
     *
41
     * @return bool
42 1
     */
43
    public function isValid():bool
44 1
    {
45
        return $this->file['name'] && count($this->getMessages()) === 0;
46
    }
47
48
    /**
49
     * Returns the validation error messages
50
     *
51
     * @return array
52 3
     */
53
    public function getMessages():array
54 3
    {
55 2
        if (isset($this->file['messages'])) {
56
            return $this->file['messages'];
57 1
        } else {
58
            return [];
59
        }
60
    }
61
62
    /**
63
     * The file that was saved during process() and has a .lock file attached
64
     * will be cleared, in case the form processing fails
65 2
     */
66
    public function clear()
67 2
    {
68 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...
69 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...
70 2
        $this->file['name'] = null;
71
    }
72
73
    /**
74
     * Remove the .lock file attached to the file that was saved during process()
75
     * This should happen if the form fails validation/processing
76 3
     */
77
    public function confirm()
78 3
    {
79 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...
80
    }
81
82
    /**
83
     * File attribute getter
84
     *
85
     * @param $name
86
     * @return mixed
87 8
     */
88
    public function __get($name)
89 8
    {
90 7
        if (isset($this->file[$name])) {
91
            return $this->file[$name];
92
        }
93 2
94
        return null;
95
    }
96
}
97