Passed
Push — master ( 9520f5...5a7301 )
by Nicolaas
08:34
created

MoveUnexpectedImages::getTitle()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Sunnysideup\PerfectCmsImages\Tasks;
4
5
use SilverStripe\Control\Director;
6
use SilverStripe\Control\Controller;
7
use SilverStripe\Control\HTTPRequest;
8
use SilverStripe\Dev\BuildTask;
9
use SilverStripe\ORM\DB;
10
11
use SilverStripe\Assets\Image;
12
use SilverStripe\Assets\Folder;
13
14
use Sunnysideup\PerfectCmsImages\Api\PerfectCMSImages;
15
16
class MoveUnexpectedImages extends BuildTask
17
{
18
19
    /**
20
     * @var Folder
21
     */
22
    public $unusedImagesFolder = null;
23
24
    private static $unused_images_folder_name = 'unusedimages';
25
26
    public function getTitle(): string
27
    {
28
        return 'Careful: experimental - DELETE ALL IMAGES THAT ARE IN A FOLDER AND SHOULD NOT BE THERE';
29
    }
30
31
    public function getDescription(): string
32
    {
33
        return 'Goes through all the perfect cms images, checks what folder they write to and moves any images that should not be there.';
34
    }
35
36
    /**
37
     * Create test jobs for the purposes of testing.
38
     *
39
     * @param HTTPRequest $request
40
     */
41
    public function run($request) // phpcs:ignore
42
    {
43
        $this->unusedImagesFolder = Folder::find_or_make($this->config()->get('unused_images_folder_name'));
44
        $data = PerfectCMSImages::get_all_values_for_images();
45
46
        // check folders
47
        $folderArray = [];
48
        foreach($data as $name => $dataInner) {
49
            $folder = $dataInner['folder'] ?? '';
50
            if($folder) {
51
                $folderArray[$folder] = [];
52
                $classes = $dataInner['used_by'] ?? [];
53
                foreach($classes as $class) {
54
                    $folderArray[$folder][] = $class;
55
                }
56
            }
57
        }
58
59
        $folderArray = [];
60
        $listOfImageIds = [];
61
        foreach($folderArray as $folderName => $classAndMethodList) {
62
63
            // find all images that should be there...
64
            $listOfIds = [];
65
            foreach($classAndMethodList as $classAndMethod) {
66
                list($class, $method) = explode('.', $classAndMethod);
67
                $listOfImageIds = array_merge(
68
                    $listOfImageIds,
69
                    $class::get()->columnUnique($method.'ID')
70
                );
71
            }
72
            // remove files
73
            if (count($listOfIds)) {
74
                $this->removeUnusedFiles($folderName, $listOfImageIds);
75
            }
76
        }
77
    }
78
79
80
    protected function removeUnusedFiles(string $folderName, array $listOfImageIds)
0 ignored issues
show
Unused Code introduced by
The parameter $listOfImageIds is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

80
    protected function removeUnusedFiles(string $folderName, /** @scrutinizer ignore-unused */ array $listOfImageIds)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $folderName is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

80
    protected function removeUnusedFiles(/** @scrutinizer ignore-unused */ string $folderName, array $listOfImageIds)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
81
    {
82
        $folder = Folder::find_or_make($this->config()->get('unused_images_folder_name'));
83
        $unusedFolderName = $this->unusedImagesFolder->Name;
84
        $where = " ParentID = " . $folder->ID. ' AND File.ID NOT IN('.implode('.$listOfImageIds.').')';
0 ignored issues
show
Bug introduced by
'.$listOfImageIds.' of type string is incompatible with the type array expected by parameter $pieces of implode(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

84
        $where = " ParentID = " . $folder->ID. ' AND File.ID NOT IN('.implode(/** @scrutinizer ignore-type */ '.$listOfImageIds.').')';
Loading history...
85
        $unused = Image::get()->where($where);
86
        if ($unused->exists()) {
87
            foreach ($unused as $file) {
88
                $oldName = $file->getFullPath();
89
                $file->ParentID = $this->unusedImagesFolder->ID;
90
                $file->write();
91
                $file->doPublish();
92
                $newName = str_replace($folder->Name, $unusedFolderName, $oldName);
93
                $oldNameFull = Controller::join_links(ASSETS_PATH, $oldName);
94
                $newNameFull = Controller::join_links(ASSETS_PATH, $newName);
95
                if (file_exists($oldNameFull) && $newNameFull !== $oldNameFull) {
96
                    if(file_exists($newNameFull)) {
97
                        unlink($newNameFull);
98
                    }
99
                    rename($oldNameFull, $newNameFull);
100
                }
101
            }
102
        }
103
    }
104
105
}
106