Completed
Push — master ( 60e303...4c478a )
by
unknown
8s
created

CleanupGeneratedPdfBuildTask::run()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 3
nop 1
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace CWP\PDFExport\Tasks;
4
5
use CWP\CWP\PageTypes\BasePage;
6
use SilverStripe\Dev\BuildTask;
7
8
class CleanupGeneratedPdfBuildTask extends BuildTask
9
{
10
    private static $segment = 'CleanupGeneratedPdfBuildTask';
0 ignored issues
show
introduced by
The private property $segment is not used, and could be removed.
Loading history...
11
12
    protected $title = 'Cleanup generated PDFs';
13
14
    protected $description = 'Removes generated PDFs on the site, forcing a regeneration of all exports to PDF '
15
        . 'when users go to download them. This is most useful when templates have been changed so users should '
16
        . 'receive a new copy';
17
18
    public function run($request)
19
    {
20
        $path = sprintf('%s/%s', BASE_PATH, BasePage::config()->get('generated_pdf_path'));
21
        if (!file_exists($path)) {
22
            return false;
23
        }
24
25
        exec(sprintf('if [ "$(ls -A %s 2> /dev/null)" != "" ]; then rm %s/*; fi', $path, $path), $output, $return_val);
26
27
        // output any errors
28
        if ($return_val != 0) {
29
            user_error(sprintf('%s failed: ', get_class($this)) . implode("\n", $output), E_USER_ERROR);
30
        }
31
    }
32
}
33