Gallery   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 136
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 50
dl 0
loc 136
rs 10
c 1
b 0
f 1
wmc 6

2 Methods

Rating   Name   Duplication   Size   Complexity  
B uploadGalleryImgPicker() 0 129 5
A uploadGallery() 0 3 1
1
<?php
2
3
namespace ByTIC\MediaLibraryModule\Controllers\Traits\Async;
4
5
use ByTIC\Common\Records\Record;
6
use ByTIC\MediaLibrary\HasMedia\HasMediaTrait;
7
use ImgPicker;
8
use Nip\Http\Request;
9
use Symfony\Component\HttpFoundation\File\UploadedFile;
10
11
/**
12
 * Class Gallery
13
 * @package ByTIC\MediaLibraryModule\Controllers\Traits\Async
14
 *
15
 * @method Request getRequest()
16
 * @method HasMediaTrait|Record getModelFromRequest()
17
 */
18
trait Gallery
19
{
20
    public function uploadGallery()
21
    {
22
        $this->uploadGalleryImgPicker();
23
    }
24
25
    public function uploadGalleryImgPicker()
26
    {
27
        $item = $this->getModelFromRequest();
28
        $newMedia = $item->getMedia('images')->newMedia();
0 ignored issues
show
Bug introduced by
The method newMedia() does not exist on Nip\Records\Collections\Collection. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

28
        $newMedia = $item->getMedia('images')->/** @scrutinizer ignore-call */ newMedia();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
29
        $minWidth = $item->getMedia('images')->getConstraint()->minWidth;
0 ignored issues
show
Bug introduced by
The method getConstraint() does not exist on Nip\Records\Collections\Collection. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

29
        $minWidth = $item->getMedia('images')->/** @scrutinizer ignore-call */ getConstraint()->minWidth;

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
Bug introduced by
The property minWidth does not seem to exist on Nip\Helpers\AbstractHelper.
Loading history...
30
        $minHeight = $item->getMedia('images')->getConstraint()->minHeight;
0 ignored issues
show
Bug introduced by
The property minHeight does not seem to exist on Nip\Helpers\AbstractHelper.
Loading history...
31
32
        $options = [
33
34
            // Upload directory path
35
            'upload_dir' => TMP_PATH,
0 ignored issues
show
Bug introduced by
The constant ByTIC\MediaLibraryModule...s\Traits\Async\TMP_PATH was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
36
37
            // Upload directory url:
38
            'upload_url' => TMP_URL,
0 ignored issues
show
Bug introduced by
The constant ByTIC\MediaLibraryModule...rs\Traits\Async\TMP_URL was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
39
40
            // Accepted file types:
41
            'accept_file_types' => 'png|jpg|jpeg|gif',
42
43
            // Directory mode:
44
            'mkdir_mode' => 0777,
45
46
            // File size restrictions (in bytes):
47
            'max_file_size' => UploadedFile::getMaxFilesize(),
48
            'min_file_size' => 1,
49
50
            // Image resolution restrictions (in px):
51
            'max_width' => null,
52
            'max_height' => null,
53
            'min_width' => $minWidth,
54
            'min_height' => $minHeight,
55
56
            /**
57
             *    Load callback
58
             *
59
             * @param    ImgPicker $instance
60
             * @return string|array
61
             */
62
            'load' => function ($instance) {
0 ignored issues
show
Unused Code introduced by
The parameter $instance is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

62
            'load' => function (/** @scrutinizer ignore-unused */ $instance) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
63
                //return 'avatar.jpg';
64
            },
65
66
            /**
67
             *    Delete callback
68
             *
69
             * @param  string $filename
70
             * @param    ImgPicker $instance
71
             * @return boolean
72
             */
73
            'delete' => function ($filename, $instance) {
0 ignored issues
show
Unused Code introduced by
The parameter $filename is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

73
            'delete' => function (/** @scrutinizer ignore-unused */ $filename, $instance) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $instance is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

73
            'delete' => function ($filename, /** @scrutinizer ignore-unused */ $instance) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
74
                return true;
75
            },
76
77
            /**
78
             *    Upload start callback
79
             *
80
             * @param    stdClass $image
81
             * @param    ImgPicker $instance
82
             * @return void
83
             */
84
            'upload_start' => function ($image, $instance) {
0 ignored issues
show
Unused Code introduced by
The parameter $instance is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

84
            'upload_start' => function ($image, /** @scrutinizer ignore-unused */ $instance) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
85
                $image->name = '~toCropFP' . $item->ID . '-' . rand(1000, 4000) . '.' . $image->type;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $item seems to be never defined.
Loading history...
86
            },
87
88
            /**
89
             *    Upload complete callback
90
             *
91
             * @param    stdClass $image
92
             * @param    ImgPicker $instance
93
             * @return void
94
             */
95
            'upload_complete' => function ($image, $instance) {
0 ignored issues
show
Unused Code introduced by
The parameter $instance is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

95
            'upload_complete' => function ($image, /** @scrutinizer ignore-unused */ $instance) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $image is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

95
            'upload_complete' => function (/** @scrutinizer ignore-unused */ $image, $instance) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
96
            },
97
98
            /**
99
             *    Crop start callback
100
             *
101
             * @param    stdClass $image
102
             * @param    ImgPicker $instance
103
             * @return void
104
             */
105
            'crop_start' => function ($image, $instance) {
0 ignored issues
show
Unused Code introduced by
The parameter $instance is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

105
            'crop_start' => function ($image, /** @scrutinizer ignore-unused */ $instance) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
106
                $image->name = sha1(microtime()) . '.' . $image->type;
107
            },
108
109
            /**
110
             *    Crop complete callback
111
             *
112
             * @param    stdClass $image
113
             * @param    ImgPicker $instance
114
             * @return void
115
             */
116
            'crop_complete' => function ($image, $instance) {
117
                foreach (['full', 'default'] as $version) {
118
                    $filename = $instance->getVersionFilename($image->name, $version);
119
                    $filepath = $instance->getUploadPath($filename, $version);
120
                    rename($filepath, str_replace('-' . $version . '.', '.', $filepath));
121
                }
122
            },
123
        ];
124
        if (@$_POST['action'] == 'crop') {
125
            $filesystem = $item->getMedia('images')->getFilesystem();
0 ignored issues
show
Bug introduced by
The method getFilesystem() does not exist on Nip\Records\Collections\Collection. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

125
            $filesystem = $item->getMedia('images')->/** @scrutinizer ignore-call */ getFilesystem();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
126
            $rootPath = $filesystem->getAdapter()->getPathPrefix();
0 ignored issues
show
Bug introduced by
The method getAdapter() does not exist on Nip\Helpers\AbstractHelper. It seems like you code against a sub-type of Nip\Helpers\AbstractHelper such as Nip_Helper_Url or Nip\Helpers\View\Stylesheets or Nip\Helpers\View\Strings or Nip\Helpers\View\Paginator or Nip\Helpers\View\Arrays or Nip\Helpers\View\Icontext or Nip\Helpers\View\Color or Nip\Helpers\View\Scripts or Nip\Helpers\View\Url. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

126
            $rootPath = $filesystem->/** @scrutinizer ignore-call */ getAdapter()->getPathPrefix();
Loading history...
127
            // Image versions:
128
            $options['versions'] = [
129
                'full' => [
130
                    'upload_dir' => $rootPath . $newMedia->getBasePath('full') . DIRECTORY_SEPARATOR,
0 ignored issues
show
Bug introduced by
The method getBasePath() does not exist on Nip\Helpers\AbstractHelper. It seems like you code against a sub-type of Nip\Helpers\AbstractHelper such as Nip_Helper_Url or Nip\Helpers\View\Stylesheets or Nip\Helpers\View\Strings or Nip\Helpers\View\Paginator or Nip\Helpers\View\Arrays or Nip\Helpers\View\Icontext or Nip\Helpers\View\Color or Nip\Helpers\View\Scripts or Nip\Helpers\View\Url. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

130
                    'upload_dir' => $rootPath . $newMedia->/** @scrutinizer ignore-call */ getBasePath('full') . DIRECTORY_SEPARATOR,
Loading history...
131
                    'upload_url' => $filesystem->getUrl($newMedia->getBasePath('full')),
0 ignored issues
show
Bug introduced by
The method getUrl() does not exist on Nip\Helpers\AbstractHelper. It seems like you code against a sub-type of Nip\Helpers\AbstractHelper such as Nip_Helper_Url or Nip\Helpers\View\Stylesheets or Nip\Helpers\View\Strings or Nip\Helpers\View\Paginator or Nip\Helpers\View\Arrays or Nip\Helpers\View\Icontext or Nip\Helpers\View\Color or Nip\Helpers\View\Scripts or Nip\Helpers\View\Url. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

131
                    'upload_url' => $filesystem->/** @scrutinizer ignore-call */ getUrl($newMedia->getBasePath('full')),
Loading history...
132
                    'max_width' => 1600,
133
                    'max_height' => 1600,
134
                ],
135
                'default' => [
136
                    'upload_dir' => $rootPath . $newMedia->getBasePath('default') . DIRECTORY_SEPARATOR,
137
                    'upload_url' => $filesystem->getUrl($newMedia->getBasePath('default')),
138
                    'max_width' => $minWidth,
139
                    'crop' => $minHeight,
140
                ],
141
            ];
142
        }
143
144
        if (intval($_SERVER['CONTENT_LENGTH']) > 0 && count($_POST) === 0) {
145
            $maxSize = round(($this->getRequest()->server->getMaxFileSize() / 1048576), 2) . 'MB';
0 ignored issues
show
Bug introduced by
The method getMaxFileSize() does not exist on Symfony\Component\HttpFoundation\ServerBag. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

145
            $maxSize = round(($this->getRequest()->server->/** @scrutinizer ignore-call */ getMaxFileSize() / 1048576), 2) . 'MB';

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
146
            $this->response['error'] = 'File to big. Max size [' . $maxSize . ']';
0 ignored issues
show
Bug Best Practice introduced by
The property response does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
147
148
            return $this->_output();
0 ignored issues
show
Bug introduced by
It seems like _output() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

148
            return $this->/** @scrutinizer ignore-call */ _output();
Loading history...
149
        }
150
151
        // Create new ImgPicker instance
152
        new ImgPicker($options);
153
        die('');
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
154
    }
155
}
156