DeleteGeneratedImagesTask::getDescription()   A
last analyzed

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
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
namespace Sunnysideup\PerfectCmsImages\Tasks;
4
5
use League\Flysystem\Filesystem;
6
use ReflectionMethod;
7
use SilverStripe\Assets\Flysystem\FlysystemAssetStore;
8
use SilverStripe\Assets\Image;
9
use SilverStripe\Assets\Storage\AssetStore;
10
use SilverStripe\Control\HTTPRequest;
11
use SilverStripe\Core\Injector\Injector;
12
use SilverStripe\Dev\BuildTask;
13
use SilverStripe\ORM\DataObject;
14
15
/**
16
 * SOURCE: https://gist.github.com/blueo/6598bc349b406cf678f9a8f009587a95
17
 * Class DeleteGeneratedImagesTask.
18
 *
19
 * Hack to allow removing manipulated images
20
 * This is needed occasionally when manipulation functions change
21
 * It isn't directly possible with core so this is a workaround
22
 *
23
 * @see https://github.com/silverstripe/silverstripe-assets/issues/109
24
 * @codeCoverageIgnore
25
 */
26
class DeleteGeneratedImagesTask extends BuildTask
27
{
28
    public function getDescription(): string
29
    {
30
        return 'Regenerate Images for an asset';
31
    }
32
33
    /**
34
     * Create test jobs for the purposes of testing.
35
     *
36
     * @param HTTPRequest $request
37
     */
38
    public function run($request) // phpcs:ignore
39
    {
40
        $Id = $request->getVar('ID');
41
        if (! $Id) {
42
            echo 'No ID provided, make sure to supply an ID to the URL eg ?ID=2';
43
44
            return;
45
        }
46
47
        $image = DataObject::get_by_id(Image::class, $Id);
48
49
        if (! $image) {
0 ignored issues
show
introduced by
$image is of type SilverStripe\ORM\DataObject, thus it always evaluated to true.
Loading history...
50
            echo 'No Image found with that ID';
51
52
            return;
53
        }
54
55
        $asetValues = $image->File->getValue();
0 ignored issues
show
Bug introduced by
The method getValue() does not exist on null. ( Ignorable by Annotation )

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

55
        /** @scrutinizer ignore-call */ 
56
        $asetValues = $image->File->getValue();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
56
        $store = Injector::inst()->get(AssetStore::class);
57
58
        // warning - super hacky as accessing private methods
59
        $getID = new ReflectionMethod(FlysystemAssetStore::class, 'getFileID');
60
        $getID->setAccessible(true);
61
62
        $flyID = $getID->invoke($store, $asetValues['Filename'], $asetValues['Hash']);
63
        $getFileSystem = new ReflectionMethod(FlysystemAssetStore::class, 'getFilesystemFor');
64
65
        $getFileSystem->setAccessible(true);
66
        /** @var Filesystem $system */
67
        $system = $getFileSystem->invoke($store, $flyID);
68
69
        $findVariants = new ReflectionMethod(FlysystemAssetStore::class, 'findVariants');
70
        $findVariants->setAccessible(true);
71
        foreach ($findVariants->invoke($store, $flyID, $system) as $variant) {
72
            $isGenerated = strpos($variant, '__');
73
            if (! $isGenerated) {
74
                continue;
75
            }
76
77
            $system->delete($variant);
78
        }
79
80
        echo "Deleted generated images for {$image->Name}";
81
    }
82
}
83