Passed
Pull Request — master (#1)
by Robbie
01:41
created

CleanupGeneratedPdfBuildTask   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 20
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 20
rs 10
c 0
b 0
f 0
wmc 3

1 Method

Rating   Name   Duplication   Size   Complexity  
A run() 0 12 3
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
    protected $title = 'Cleanup generated PDFs';
11
12
    protected $description = 'Removes generated PDFs on the site, forcing a regeneration of all exports to PDF '
13
        . 'when users go to download them. This is most useful when templates have been changed so users should '
14
        . 'receive a new copy';
15
16
    public function run($request)
17
    {
18
        $path = sprintf('%s/%s', BASE_PATH, BasePage::config()->get('generated_pdf_path'));
19
        if (!file_exists($path)) {
20
            return false;
21
        }
22
23
        exec(sprintf('if [ "$(ls -A %s 2> /dev/null)" != "" ]; then rm %s/*; fi', $path, $path), $output, $return_val);
24
25
        // output any errors
26
        if ($return_val != 0) {
27
            user_error(sprintf('%s failed: ', get_class($this)) . implode("\n", $output), E_USER_ERROR);
28
        }
29
    }
30
}
31