Passed
Push — master ( bf7994...5e2021 )
by Nicolaas
02:52
created

AddAllFilesFromDiskToDB   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 20
dl 0
loc 33
rs 10
c 1
b 0
f 1
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A dryRunMessage() 0 6 2
A run() 0 14 3
1
<?php
2
3
namespace Vendor\Sunnysideup\AssetsOverview\Tasks;
4
5
use SilverStripe\Core\Injector\Injector;
6
use SilverStripe\Dev\BuildTask;
7
use SilverStripe\ORM\DB;
8
use Sunnysideup\AssetsOverview\Api\AddAndRemoveFromDb;
9
use Sunnysideup\AssetsOverview\Files\AllFilesInfo;
10
11
class AddAllFilesFromDiskToDB extends BuildTask
12
{
13
14
    protected $title = 'Add all files from disk to database';
15
16
    protected $description = 'This task will add all files from the disk to the database.';
17
18
    private static $segment = 'add-all-files-from-disk-to-db';
19
20
    protected $dryRun = true;
21
22
    public function run($request)
23
    {
24
        $this->dryRun = $request->getVar('forreal') ? false : true;
25
        $this->dryRunMessage();
26
        $allFilesInfoObj = AllFilesInfo::inst();
27
        $allFilesInfoObj->setVerbose(true);
28
        $files = $allFilesInfoObj->getFilesAsArrayList()->toArray();
29
        $obj = Injector::inst()->get(AddAndRemoveFromDb::class);
30
        $obj->setIsDryRun($this->dryRun);
31
        foreach ($files as $file) {
32
            $obj->run($file->toMap(), 'add');
33
        }
34
        $this->dryRunMessage();
35
        DB::alteration_message('=== DONE ===', '');
36
    }
37
38
    protected function dryRunMessage()
39
    {
40
        if ($this->dryRun) {
41
            DB::alteration_message('Please set forreal=true to actually add the files to the database', 'created');
42
        } else {
43
            DB::alteration_message('Adding all files from disk to database and removing files from database that are not on disk.', 'created');
44
        }
45
    }
46
}
47