Passed
Push — master ( 5d5552...0f1d40 )
by Nicolaas
07:45
created

DeleteGeneratedImagesTask::run()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 36
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

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

57
        /** @scrutinizer ignore-call */ 
58
        $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...
58
        $store = Injector::inst()->get(AssetStore::class);
59
60
        // warning - super hacky as accessing private methods
61
        $getID = new ReflectionMethod(FlysystemAssetStore::class, 'getFileID');
62
        $getID->setAccessible(true);
63
        $flyID = $getID->invoke($store, $asetValues['Filename'], $asetValues['Hash']);
64
        $getFileSystem = new ReflectionMethod(FlysystemAssetStore::class, 'getFilesystemFor');
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
            $system->delete($variant);
77
        }
78
        echo "Deleted generated images for $image->Name";
79
    }
80
}
81