Passed
Pull Request — 4.4 (#8952)
by Guy
07:00
created

MigrateFileTask::run()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 26
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 16
nc 5
nop 1
dl 0
loc 26
rs 9.7333
c 0
b 0
f 0
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\Control\Director;
10
use SilverStripe\Core\Injector\Injector;
11
use SilverStripe\Logging\PreformattedEchoHandler;
12
use SilverStripe\ORM\DB;
13
use SilverStripe\Assets\FileMigrationHelper;
14
use SilverStripe\Dev\BuildTask;
15
16
/**
17
 * Migrates all 3.x file dataobjects to use the new DBFile field.
18
 */
19
class MigrateFileTask extends BuildTask
20
{
21
    private static $segment = 'MigrateFileTask';
0 ignored issues
show
introduced by
The private property $segment is not used, and could be removed.
Loading history...
22
23
    protected $title = 'Migrate File dataobjects from 3.x';
24
25
    protected $description =
26
        'Imports all files referenced by File dataobjects into the new Asset Persistence Layer introduced in 4.0. ' .
27
        'If the task fails or times out, run it again and it will start where it left off.';
28
29
    public function run($request)
30
    {
31
        $this->addLogHandlers();
32
33
        if (!class_exists(FileMigrationHelper::class)) {
34
            DB::alteration_message("No file migration helper detected", "notice");
35
            return;
36
        }
37
38
        DB::alteration_message(
39
            'If the task fails or times out, run it again and it will start where it left off.',
40
            "info"
41
        );
42
43
        $migrated = FileMigrationHelper::singleton()->run();
44
        if ($migrated) {
45
            DB::alteration_message("{$migrated} File DataObjects upgraded", "changed");
46
        } else {
47
            DB::alteration_message("No File DataObjects need upgrading", "notice");
48
        }
49
50
        if (!class_exists(ImageThumbnailHelper::class)) {
51
            DB::alteration_message("No image thumbnail helper detected", "notice");
52
            return;
53
        }
54
        ImageThumbnailHelper::singleton()->run();
55
    }
56
57
    /**
58
     * TODO Refactor this whole mess into Symfony Console on a TaskRunner level,
59
     * with a thin wrapper to show coloured console output via a browser:
60
     * https://github.com/silverstripe/silverstripe-framework/issues/5542
61
     * @throws \Exception
62
     */
63
    protected function addLogHandlers()
64
    {
65
        if ($logger = Injector::inst()->get(LoggerInterface::class)) {
66
            if (Director::is_cli()) {
67
                $logger->pushHandler(new StreamHandler('php://stdout'));
68
                $logger->pushHandler(new StreamHandler('php://stderr', Logger::WARNING));
69
            } else {
70
                $logger->pushHandler(new PreformattedEchoHandler());
71
            }
72
        }
73
    }
74
}
75