Passed
Push — master ( e26adf...e12921 )
by Nicolaas
08:55
created

AddAndRemoveFromDb::run()   B

Complexity

Conditions 11
Paths 12

Size

Total Lines 20
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 11
eloc 15
nc 12
nop 2
dl 0
loc 20
rs 7.3166
c 1
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Sunnysideup\AssetsOverview\Api;
4
5
use SilverStripe\AssetAdmin\Controller\AssetAdmin;
0 ignored issues
show
Bug introduced by
The type SilverStripe\AssetAdmin\Controller\AssetAdmin 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...
6
use SilverStripe\Assets\File;
7
use SilverStripe\Assets\Image;
8
use SilverStripe\Core\Config\Configurable;
9
use SilverStripe\Core\Injector\Injectable;
10
use SilverStripe\Core\Injector\Injector;
11
use SilverStripe\ORM\DB;
12
use SilverStripe\Versioned\Versioned;
13
14
class AddAndRemoveFromDb
15
{
16
    use Injectable;
17
    use Configurable;
18
19
    private static $publish_recursive = true;
20
21
    public function run(array $oneFileInfoArray, ?string $mode = null)
22
    {
23
        if ($mode !== 'add' && $mode !== 'remove' && $mode !== null) {
24
            user_error('Mode must be either "add" or "remove" or not set at all', E_USER_ERROR);
25
        }
26
        $pathFromAssetsFolder = $oneFileInfoArray['PathFromAssetsFolder'];
27
        $localPath = $oneFileInfoArray['Path'];
28
        if ($oneFileInfoArray['IsDir']) {
29
            DB::alteration_message('Skipping ' . $pathFromAssetsFolder . ' as this is a folder', '');
30
        } elseif (! empty($oneFileInfoArray['IsResizedImage'])) {
31
            if (file_exists($localPath)) {
32
                DB::alteration_message('Deleting ' . $pathFromAssetsFolder, 'deleted');
33
                //unlink($localPath);
34
            }
35
        } elseif ($oneFileInfoArray['ErrorDBNotPresent'] && $mode !== 'remove') {
36
            DB::alteration_message('Adding file to database ' . $pathFromAssetsFolder, 'created');
37
            $this->addFileToDb($oneFileInfoArray);
38
        } elseif ($oneFileInfoArray['ErrorIsInFileSystem'] && $mode !== 'add') {
39
            DB::alteration_message('Removing from database ' . $pathFromAssetsFolder, 'deleted');
40
            $this->removeFileFromDb($oneFileInfoArray);
41
        }
42
    }
43
44
    public function removeFileFromDb(array $oneFileInfoArray)
45
    {
46
        $file = File::get()->byID($oneFileInfoArray['DBID']);
47
        if ($file) {
48
            $file->DeleteFromStage(Versioned::LIVE);
49
            $file->DeleteFromStage(Versioned::DRAFT);
50
        }
51
    }
52
53
    public function addFileToDb(array $oneFileInfoArray)
54
    {
55
        $localPath = $oneFileInfoArray['Path'];
56
        $pathFromAssetsFolder = $oneFileInfoArray['PathFromAssetsFolder'];
57
        $extension = File::get_file_extension($localPath);
58
        $newClass = File::get_class_for_file_extension($extension);
59
        $newFile = Injector::inst()->create($newClass);
60
        $newFile->setFromLocalFile($localPath, $pathFromAssetsFolder);
61
        $newFile->write();
62
63
        // If file is an image, generate thumbnails
64
        if (is_a($newFile, Image::class)) {
65
            $admin = AssetAdmin::create();
66
            $admin->generateThumbnails($newFile, true);
67
        }
68
69
        if ($this->Config()->publish_recursive) {
70
            $newFile->publishRecursive();
71
        }
72
    }
73
}
74