PdfExportExtension   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 88
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 22
c 3
b 0
f 0
dl 0
loc 88
rs 10
wmc 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A onAfterUnpublish() 0 5 2
A getPdfFilename() 0 10 2
A PdfLink() 0 12 4
A onAfterPublish() 0 5 2
1
<?php
2
3
namespace CWP\PDFExport\Extensions;
4
5
use SilverStripe\Assets\File;
6
use SilverStripe\Control\Director;
7
use SilverStripe\ORM\DataExtension;
8
use SilverStripe\Versioned\Versioned;
9
10
class PdfExportExtension extends DataExtension
11
{
12
    /**
13
     * @config
14
     * @var bool
15
     */
16
    private static $pdf_export = false;
0 ignored issues
show
introduced by
The private property $pdf_export is not used, and could be removed.
Loading history...
17
18
    /**
19
     * Domain to generate PDF's from, DOES not include protocol
20
     * i.e. google.com not http://google.com
21
     * @config
22
     * @var string
23
     */
24
    private static $pdf_base_url = '';
0 ignored issues
show
introduced by
The private property $pdf_base_url is not used, and could be removed.
Loading history...
25
26
    /**
27
     * Allow custom overriding of the path to the WKHTMLTOPDF binary, in cases
28
     * where multiple versions of the binary are available to choose from. This
29
     * should be the full path to the binary (e.g. /usr/local/bin/wkhtmltopdf)
30
     * @see BasePage_Controller::generatePDF();
31
     *
32
     * @deprecated 3.0 Use WKHTMLTOPDF_BINARY env var instead
33
     *
34
     * @config
35
     * @var string|null
36
     */
37
    private static $wkhtmltopdf_binary = null;
0 ignored issues
show
introduced by
The private property $wkhtmltopdf_binary is not used, and could be removed.
Loading history...
38
39
    /**
40
     * Where to store generated PDF files
41
     *
42
     * @config
43
     * @var string
44
     */
45
    private static $generated_pdf_path = 'assets/_generated_pdfs';
0 ignored issues
show
introduced by
The private property $generated_pdf_path is not used, and could be removed.
Loading history...
46
47
    /**
48
     * Return the full filename of the pdf file, including path & extension
49
     */
50
    public function getPdfFilename()
51
    {
52
        $baseName = sprintf('%s-%s', $this->owner->URLSegment, $this->owner->ID);
53
54
        $folderPath = $this->owner->config()->get('generated_pdf_path');
55
        if ($folderPath[0] !== '/') {
56
            $folderPath = File::join_paths(Director::publicFolder(), $folderPath);
57
        }
58
59
        return sprintf('%s/%s.pdf', $folderPath, $baseName);
60
    }
61
62
    /**
63
     * Build pdf link for template.
64
     */
65
    public function PdfLink()
66
    {
67
        if (!$this->owner->config()->get('pdf_export')) {
68
            return false;
69
        }
70
71
        $path = $this->getPdfFilename();
72
73
        if ((Versioned::get_stage() === Versioned::LIVE) && file_exists($path)) {
74
            return Director::baseURL() . preg_replace('#^/#', '', Director::makeRelative($path));
75
        }
76
        return $this->owner->Link('downloadpdf');
77
    }
78
79
    /**
80
     * Remove linked pdf when publishing the page, as it would be out of date.
81
     */
82
    public function onAfterPublish()
83
    {
84
        $filepath = $this->getPdfFilename();
85
        if (file_exists($filepath)) {
86
            unlink($filepath);
87
        }
88
    }
89
90
    /**
91
     * Remove linked pdf when unpublishing the page, so it's no longer valid.
92
     */
93
    public function onAfterUnpublish()
94
    {
95
        $filepath = $this->getPdfFilename();
96
        if (file_exists($filepath)) {
97
            unlink($filepath);
98
        }
99
    }
100
}
101