Test Setup Failed
Push — master ( a4f66d...29baaf )
by Mihail
41:06
created

FormAvatarUpload::sources()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 6
rs 9.4286
cc 1
eloc 3
nc 1
nop 0
1
<?php
2
3
namespace Apps\Model\Front\Profile;
4
5
use Ffcms\Core\App;
6
use Ffcms\Core\Arch\Model;
7
use Ffcms\Core\Interfaces\iUser;
8
use Gregwar\Image\Image;
9
10
class FormAvatarUpload extends Model
11
{
12
    /** @var \Symfony\Component\HttpFoundation\File\UploadedFile */
13
    public $file;
14
15
    const AVATAR_SIZE = 2097152; // 2mb
16
    const COMPRESS_QUALITY = 90;
17
18
    /**
19
    * Example of usage magic labels for future form helper usage
20
    */
21
    public function labels()
22
    {
23
        return [
24
            'file' => __('Select avatar')
25
        ];
26
    }
27
28
    /**
29
    * Example of usage magic rules for future usage in condition $model->validate()
30
    */
31
    public function rules()
32
    {
33
        return [
34
            ['file', 'required'],
35
            ['file', 'isFile', ['jpg', 'png', 'gif', 'jpeg']],
36
            ['file', 'sizeFile', [1, static::AVATAR_SIZE]]
37
        ];
38
    }
39
40
    public function sources()
41
    {
42
        return [
43
            'file' => 'file'
44
        ];
45
    }
46
47
    public function copyFile(iUser $user)
48
    {
49
        // move file to original folder
50
        $upload = $this->file->move(root . '/upload/user/avatar/original/', $user->id . '.' . $this->file->guessExtension());
0 ignored issues
show
Bug introduced by
Accessing id on the interface Ffcms\Core\Interfaces\iUser suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
51
52
        try {
53
            // big image
54
            $this->resizeAndSave($upload, $user->id, 'big');
0 ignored issues
show
Bug introduced by
Accessing id on the interface Ffcms\Core\Interfaces\iUser suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
Compatibility introduced by
$upload of type object<Symfony\Component...tpFoundation\File\File> is not a sub-type of object<Symfony\Component...tion\File\UploadedFile>. It seems like you assume a child class of the class Symfony\Component\HttpFoundation\File\File to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
55
            $this->resizeAndSave($upload, $user->id, 'medium');
0 ignored issues
show
Bug introduced by
Accessing id on the interface Ffcms\Core\Interfaces\iUser suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
Compatibility introduced by
$upload of type object<Symfony\Component...tpFoundation\File\File> is not a sub-type of object<Symfony\Component...tion\File\UploadedFile>. It seems like you assume a child class of the class Symfony\Component\HttpFoundation\File\File to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
56
            $this->resizeAndSave($upload, $user->id, 'small');
0 ignored issues
show
Bug introduced by
Accessing id on the interface Ffcms\Core\Interfaces\iUser suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
Compatibility introduced by
$upload of type object<Symfony\Component...tpFoundation\File\File> is not a sub-type of object<Symfony\Component...tion\File\UploadedFile>. It seems like you assume a child class of the class Symfony\Component\HttpFoundation\File\File to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
57
58
        } catch (\Exception $e) {
59
            if (App::$Debug) {
60
                App::$Debug->addException($e);
61
            }
62
        }
63
    }
64
65
    /**
66
     * @param \Symfony\Component\HttpFoundation\File\UploadedFile $original
67
     * @param int $user_id
68
     * @param string $size
69
     * @throws \Exception
70
     * @return null
71
     */
72
    protected function resizeAndSave($original, $user_id, $size = 'small')
73
    {
74
        $sizeConvert = [
75
            'big' => [400, 400],
76
            'medium' => [200, 200],
77
            'small' => [100, 100]
78
        ];
79
80
        if (!array_key_exists($size, $sizeConvert)) {
81
            return null;
82
        }
83
84
        $image = new Image();
85
        $image->setCacheDir(root . '/Private/Cache/images');
86
87
        $image->open($original->getPathname())
88
            ->cropResize($sizeConvert[$size][0], $sizeConvert[$size][1])
89
            ->save(root . '/upload/user/avatar/' . $size . '/' . $user_id . '.jpg', 'jpg', static::COMPRESS_QUALITY);
90
91
        return null;
92
    }
93
}