Passed
Push — master ( 1f9cb7...c92ce6 )
by Nicolaas
09:06
created

MoveFiles   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 42
dl 0
loc 70
rs 10
c 0
b 0
f 0
wmc 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A run() 0 16 3
B addToDb() 0 30 7
1
<?php
2
3
namespace SilverStripe\MoveLargeFilesToAssets;
4
5
use SilverStripe\Assets\File;
6
use SilverStripe\Assets\Folder;
7
use SilverStripe\Control\Controller;
8
use SilverStripe\Control\Director;
9
use SilverStripe\Dev\BuildTask;
10
use SilverStripe\ORM\DB;
11
12
class MoveFiles extends BuildTask
13
{
14
    /**
15
     * {@inheritDoc}
16
     */
17
    protected $title = 'Copy (large) files to assets';
18
19
    /**
20
     * {@inheritDoc}
21
     */
22
    protected $description = 'Move public/new-files to assets/new-files where new-files can be set as you see fit.';
23
24
    /**
25
     * {@inheritDoc}
26
     */
27
    protected $enabled = true;
28
29
    private static $folder_name = 'new-files';
30
31
    /**
32
     * {@inheritDoc}
33
     */
34
    public function run($request)
35
    {
36
        $folderName = self::config()->get('folder_name');
37
        $oldPath = Controller::join_links(ASSETS_PATH, '..', $folderName);
38
        $newPath = Controller::join_links(ASSETS_PATH, $folderName);
39
        if (! file_exists($newPath)) {
40
            if (file_exists($oldPath)) {
41
                DB::alteration_message('Moved ' . $oldPath . ' to ' . $newPath . '');
42
                rename($oldPath, $newPath);
43
            } else {
44
                DB::alteration_message('Could not move ' . $oldPath . ' to ' . $newPath . ' because ' . $oldPath . ' does not exist.');
45
            }
46
        } else {
47
            DB::alteration_message('Could not move ' . $oldPath . ' to ' . $newPath . ' because ' . $newPath . ' already exists.');
48
        }
49
        $this->addToDb($newPath);
50
    }
51
52
    public function addToDb(string $newFolderPath)
53
    {
54
        DB::alteration_message('scanning ' . $newFolderPath);
55
        $paths = scandir($newFolderPath);
56
        if ($paths && is_array($paths) && count($paths) < 100) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $paths of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
57
            foreach ($paths as $path) {
58
                $path = Controller::join_links($newFolderPath, $path);
59
                DB::alteration_message('considering ' . $path);
60
                if (! is_dir($path)) {
61
                    $fileName = basename($path);
62
                    $folderPath = dirname($path);
63
                    $folder = Folder::find_or_make($folderPath);
64
                    $filter = ['Name' => $fileName, 'ParentID' => $folder->ID];
65
                    $file = File::get()->filter($filter)->first();
66
                    if (! $file) {
67
                        DB::alteration_message('New file: ' . $path);
68
                        $file = File::create();
69
                        $file->setFromLocalFile($path);
70
                        $file->ParentID = $folder->ID;
71
                        $file->write();
72
                        $file->doPublish();
73
                    } else {
74
                        DB::alteration_message('existing file: ' . $path);
75
                    }
76
                } else {
77
                    DB::alteration_message('skipping ' . $path);
78
                }
79
            }
80
        } else {
81
            DB::alteration_message('nothing to add');
82
        }
83
    }
84
}
85