Completed
Push — master ( fe927f...32a670 )
by Robbie
21:23 queued 12:16
created

MigrateFileTask   A

Complexity

Total Complexity 24

Size/Duplication

Total Lines 166
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 83
dl 0
loc 166
rs 10
c 0
b 0
f 0
wmc 24

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getDescription() 0 3 1
A setLogger() 0 5 1
A addLogHandlers() 0 8 3
A validateArgs() 0 5 3
A getStore() 0 3 1
F run() 0 85 15
1
<?php
2
3
namespace SilverStripe\Dev\Tasks;
4
5
use Monolog\Handler\StreamHandler;
6
use Monolog\Logger;
7
use Psr\Log\LoggerInterface;
8
use SilverStripe\AssetAdmin\Helper\ImageThumbnailHelper;
0 ignored issues
show
Bug introduced by
The type SilverStripe\AssetAdmin\...er\ImageThumbnailHelper was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
9
use SilverStripe\Assets\Dev\Tasks\LegacyThumbnailMigrationHelper;
0 ignored issues
show
Bug introduced by
The type SilverStripe\Assets\Dev\...humbnailMigrationHelper was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
10
use SilverStripe\Assets\FileMigrationHelper;
11
use SilverStripe\Assets\Storage\AssetStore;
12
use SilverStripe\Control\Director;
13
use SilverStripe\Core\Injector\Injector;
14
use SilverStripe\Logging\PreformattedEchoHandler;
15
use SilverStripe\Dev\BuildTask;
16
use SilverStripe\Assets\Dev\Tasks\SecureAssetsMigrationHelper;
0 ignored issues
show
Bug introduced by
The type SilverStripe\Assets\Dev\...reAssetsMigrationHelper was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
17
18
/**
19
 * Migrates all 3.x file dataobjects to use the new DBFile field.
20
 */
21
class MigrateFileTask extends BuildTask
22
{
23
    private static $segment = 'MigrateFileTask';
0 ignored issues
show
introduced by
The private property $segment is not used, and could be removed.
Loading history...
24
25
    protected $title = 'Migrate File dataobjects from 3.x and successive iterations in 4.x';
26
27
    protected $defaultSubtasks = [
28
        'move-files',
29
        'move-thumbnails',
30
        'generate-cms-thumbnails',
31
        'fix-folder-permissions',
32
        'fix-secureassets',
33
    ];
34
35
    private static $dependencies = [
0 ignored issues
show
introduced by
The private property $dependencies is not used, and could be removed.
Loading history...
36
        'logger' => '%$' . LoggerInterface::class,
37
    ];
38
39
    /** @var Logger */
40
    private $logger;
41
42
    public function run($request)
43
    {
44
        $this->addLogHandlers();
45
46
        $args = $request->getVars();
47
        $this->validateArgs($args);
48
49
        $subtasks = !empty($args['only']) ? explode(',', $args['only']) : $this->defaultSubtasks;
50
51
        if (in_array('move-files', $subtasks)) {
52
            if (!class_exists(FileMigrationHelper::class)) {
53
                $this->logger->error("No file migration helper detected");
54
                return;
55
            }
56
57
            $this->logger->info('### Migrating filesystem and database records (move-files)');
58
59
            $this->logger->info('If the task fails or times out, run it again and it will start where it left off.');
60
61
            $migrated = FileMigrationHelper::singleton()->run();
62
            if ($migrated) {
63
                $this->logger->info("{$migrated} File DataObjects upgraded");
64
            } else {
65
                $this->logger->info("No File DataObjects need upgrading");
66
            }
67
        }
68
69
        if (in_array('move-thumbnails', $subtasks)) {
70
            if (!class_exists(LegacyThumbnailMigrationHelper::class)) {
71
                $this->logger->error("LegacyThumbnailMigrationHelper not found");
72
                return;
73
            }
74
75
            $this->logger->info('### Migrating existing thumbnails (move-thumbnails)');
76
77
            $moved = LegacyThumbnailMigrationHelper::singleton()
78
                ->setLogger($this->logger)
79
                ->run($this->getStore());
80
81
            if ($moved) {
82
                $this->logger->info(sprintf("%d thumbnails moved", count($moved)));
83
            } else {
84
                $this->logger->info("No thumbnails moved");
85
            }
86
        }
87
88
        if (in_array('generate-cms-thumbnails', $subtasks)) {
89
            if (!class_exists(ImageThumbnailHelper::class)) {
90
                $this->logger->error("ImageThumbnailHelper not found");
91
                return;
92
            }
93
94
            $this->logger->info('### Generating new CMS UI thumbnails (generate-cms-thumbnails)');
95
96
            ImageThumbnailHelper::singleton()->run();
97
        }
98
99
        if (in_array('fix-folder-permissions', $subtasks)) {
100
            if (!class_exists(FixFolderPermissionsHelper::class)) {
0 ignored issues
show
Bug introduced by
The type SilverStripe\Dev\Tasks\FixFolderPermissionsHelper was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
101
                $this->logger->error("FixFolderPermissionsHelper not found");
102
                return;
103
            }
104
105
            $this->logger->info('### Fixing folder permissions (fix-folder-permissions)');
106
107
            $updated = FixFolderPermissionsHelper::singleton()->run();
108
109
            if ($updated > 0) {
110
                $this->logger->info("Repaired {$updated} folders with broken CanViewType settings");
111
            } else {
112
                $this->logger->info("No folders required fixes");
113
            }
114
        }
115
116
        if (in_array('fix-secureassets', $subtasks)) {
117
            if (!class_exists(SecureAssetsMigrationHelper::class)) {
118
                $this->logger->error("SecureAssetsMigrationHelper not found");
119
                return;
120
            }
121
122
            $this->logger->info('### Fixing secure-assets (fix-secureassets)');
123
124
            $moved = SecureAssetsMigrationHelper::singleton()
0 ignored issues
show
Unused Code introduced by
The assignment to $moved is dead and can be removed.
Loading history...
125
                ->setLogger($this->logger)
126
                ->run($this->getStore());
127
        }
128
    }
129
130
    public function getDescription()
131
    {
132
        return <<<TXT
133
Imports all files referenced by File dataobjects into the new Asset Persistence Layer introduced in 4.0.
134
Moves existing thumbnails, and generates new thumbnail sizes for the CMS UI.
135
Fixes file permissions.
136
If the task fails or times out, run it again and it will start where it left off.
137
You need to flush your cache after running this task via CLI.
138
See https://docs.silverstripe.org/en/4/developer_guides/files/file_migration/.
139
TXT;
140
    }
141
142
    /**
143
     * @param LoggerInterface $logger
144
     */
145
    public function setLogger(LoggerInterface $logger)
146
    {
147
        $this->logger = $logger;
0 ignored issues
show
Documentation Bug introduced by
$logger is of type Psr\Log\LoggerInterface, but the property $logger was declared to be of type Monolog\Logger. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
148
149
        return $this;
150
    }
151
152
    /**
153
     * @return AssetStore
154
     */
155
    protected function getStore()
156
    {
157
        return singleton(AssetStore::class);
158
    }
159
160
    /**
161
     * @param array $args
162
     * @throws \InvalidArgumentException
163
     */
164
    protected function validateArgs($args)
165
    {
166
        if (!empty($args['only'])) {
167
            if (array_diff(explode(',', $args['only']), $this->defaultSubtasks)) {
168
                throw new \InvalidArgumentException('Invalid subtasks detected: ' . $args['only']);
169
            }
170
        }
171
    }
172
173
    /**
174
     * TODO Refactor this whole mess into Symfony Console on a TaskRunner level,
175
     * with a thin wrapper to show coloured console output via a browser:
176
     * https://github.com/silverstripe/silverstripe-framework/issues/5542
177
     * @throws \Exception
178
     */
179
    protected function addLogHandlers()
180
    {
181
        if ($logger = Injector::inst()->get(LoggerInterface::class)) {
182
            if (Director::is_cli()) {
183
                $logger->pushHandler(new StreamHandler('php://stdout'));
184
                $logger->pushHandler(new StreamHandler('php://stderr', Logger::WARNING));
185
            } else {
186
                $logger->pushHandler(new PreformattedEchoHandler());
187
            }
188
        }
189
    }
190
}
191