Issues (35)

src/Control/Fix.php (7 issues)

1
<?php
2
3
namespace Sunnysideup\AssetsOverview\Control;
4
5
use SilverStripe\Assets\File;
6
use SilverStripe\CMS\Controllers\ContentController;
7
use SilverStripe\Core\Environment;
8
use SilverStripe\Core\Injector\Injector;
9
use SilverStripe\ORM\DB;
10
use SilverStripe\Security\Permission;
11
use SilverStripe\Security\Security;
12
use SilverStripe\Versioned\Versioned;
13
use SilverStripe\View\Requirements;
14
use SilverStripe\View\SSViewer;
15
use Sunnysideup\AssetsOverview\Files\AllFilesInfo;
16
use Sunnysideup\AssetsOverview\Files\OneFileInfo;
17
use Sunnysideup\AssetsOverview\Traits\FilesystemRelatedTraits;
18
19
/**
20
 * Class \Sunnysideup\AssetsOverview\Control\Fix
21
 *
22
 * @property \Sunnysideup\AssetsOverview\Control\Fix $dataRecord
23
 * @method \Sunnysideup\AssetsOverview\Control\Fix data()
24
 * @mixin \Sunnysideup\AssetsOverview\Control\Fix
25
 */
26
class Fix extends ContentController
27
{
28
    use FilesystemRelatedTraits;
29
30
    protected $intel = [];
31
32
    private static $allowed_actions = [
33
        'fix' => 'ADMIN',
34
    ];
35
36
    public function fix($request)
37
    {
38
        $path = $this->request->getVar('path');
39
        $error = $this->request->getVar('error');
40
        $paths = empty($path) ? $this->getRawData() : [$path];
41
        foreach ($paths as $path) {
42
            if ($path) {
43
                $this->intel = $this->getDataAboutOneFile($path);
44
                if ($error) {
45
                    $this->runMethod($error);
46
                } else {
47
                    foreach ($this->intel as $key => $value) {
48
                        if (true === $value) {
49
                            $method = 'fix' . $key;
50
                            $this->runMethod($method);
51
                        }
52
                    }
53
                }
54
            }
55
        }
56
    }
57
58
    protected function init()
59
    {
60
        parent::init();
61
        if (! Permission::check('ADMIN')) {
62
            return Security::permissionFailure($this);
63
        }
64
65
        Requirements::clear();
66
        ini_set('memory_limit', '1024M');
67
        Environment::increaseMemoryLimitTo();
68
        Environment::increaseTimeLimitTo(7200);
69
        SSViewer::config()->merge('theme_enabled', false);
70
        Versioned::set_stage(Versioned::DRAFT);
71
    }
72
73
    protected function runMethod($method)
74
    {
75
        $method = 'fix' . $method;
76
        if ('Error' === substr((string) $method, 0, 5) && $this->hasMethod($method)) {
77
            $this->{$method}();
78
        }
79
    }
80
81
    protected function getRawData(): array
82
    {
83
        //get data
84
        return Injector::inst()->get(AllFilesInfo::class)
85
            ->toArray();
86
    }
87
88
    protected function getDataAboutOneFile(string $absoluteLocation): array
89
    {
90
        $obj = OneFileInfo::inst($absoluteLocation);
91
        $obj->setNoCache(true);
92
        return $obj->toArray(true);
0 ignored issues
show
The call to Sunnysideup\AssetsOvervi...\OneFileInfo::toArray() has too many arguments starting with true. ( Ignorable by Annotation )

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

92
        return $obj->/** @scrutinizer ignore-call */ toArray(true);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
93
    }
94
95
    protected function fixErrorDBNotPresent()
96
    {
97
        $pathArray = pathinfo($this->path);
0 ignored issues
show
Bug Best Practice introduced by
The property path does not exist on Sunnysideup\AssetsOverview\Control\Fix. Since you implemented __get, consider adding a @property annotation.
Loading history...
It seems like $this->path can also be of type null; however, parameter $path of pathinfo() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

97
        $pathArray = pathinfo(/** @scrutinizer ignore-type */ $this->path);
Loading history...
98
        $ext = $pathArray['extension'];
99
        $className = File::get_class_for_file_extension($ext);
100
        if (class_exists($className)) {
101
            $obj = $className::create()->setFromLocalFile($this->path);
102
            $obj->writeToStage(Versioned::DRAFT);
103
            $obj->publishRecursive();
104
        }
105
    }
106
107
    protected function fixErrorDBNotPresentLive() {}
108
109
    protected function fixErrorDBNotPresentStaging() {}
110
111
    protected function fixErrorExtensionMisMatch()
112
    {
113
        $file = $this->getFileObject();
114
        if ($file) {
0 ignored issues
show
$file is of type SilverStripe\Assets\File, thus it always evaluated to true.
Loading history...
115
            $newFileName = $this->intel['PathFolderFromAssets'] . DIRECTORY_SEPARATOR .
116
                $this->intel['PathFileName'] . '.' . $this->intel['PathExtensionAsLower'];
117
            $file = $this->getFileObject();
118
            if ($file) {
0 ignored issues
show
$file is of type SilverStripe\Assets\File, thus it always evaluated to true.
Loading history...
119
                $file->renameFile($newFileName);
120
            }
121
        }
122
    }
123
124
    protected function fixErrorFindingFolder()
125
    {
126
        $this->fixFileInDB();
127
    }
128
129
    protected function fixErrorInFilename()
130
    {
131
        $this->fixFileInDB();
132
    }
133
134
    protected function fixErrorInSs3Ss4Comparison()
135
    {
136
        foreach (['', '_Live'] as $stage) {
137
            DB::query(
138
                '
139
                UPDATE "File"' . $stage . '
140
                SET "Filename" = "FileFileName"
141
                WHERE ID =' . $this->intel['DBID'] . ' AND "Filename" <> "FileFileName" AND "FileFileName" IS NOT NULL AND "FileFileName" <> \'\''
142
            );
143
            DB::query(
144
                '
145
                UPDATE "File"' . $stage . '
146
                SET "FileFileName" = "Filename"
147
                WHERE ID =' . $this->intel['DBID'] . ' AND "Filename" <> "FileFileName" AND "Filename" IS NOT NULL AND "Filename" <> \'\''
148
            );
149
        }
150
151
        return true;
152
    }
153
154
    protected function fixErrorParentID() {}
155
156
    protected function fixFileInDB()
157
    {
158
        $file = $this->getFileObject();
159
        if ($file) {
0 ignored issues
show
$file is of type SilverStripe\Assets\File, thus it always evaluated to true.
Loading history...
160
            $this->updateFilesystem();
0 ignored issues
show
The method updateFilesystem() does not exist on Sunnysideup\AssetsOverview\Control\Fix. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

160
            $this->/** @scrutinizer ignore-call */ 
161
                   updateFilesystem();
Loading history...
161
        } else {
162
            user_error('Can not find file ID');
163
        }
164
    }
165
166
    protected function getFileObject(): ?File
167
    {
168
        return File::get_by_id($this->intel['DBID']);
169
    }
170
}
171