AddAllFilesFromDiskToDB   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
eloc 19
c 2
b 0
f 1
dl 0
loc 34
rs 10
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
27
        $files = AllFilesInfo::inst()->getAllFiles();
28
        $obj = Injector::inst()->get(AddAndRemoveFromDb::class);
29
        $obj->setIsDryRun($this->dryRun);
30
        foreach ($files as $file) {
31
            $array = $file->toMap();
32
            $obj->run($array, 'add');
33
        }
34
        $this->dryRunMessage();
35
        DB::alteration_message('=== DONE ===', '');
36
    }
37
38
39
    protected function dryRunMessage()
40
    {
41
        if ($this->dryRun) {
42
            DB::alteration_message('Please set forreal=true to actually do this', 'created');
43
        } else {
44
            DB::alteration_message('Doing it for real!', 'created');
45
        }
46
    }
47
}
48