PublishableSiteTree::urlsToCache()   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
nc 1
nop 0
dl 0
loc 3
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace SilverStripe\StaticPublishQueue\Extension\Publishable;
4
5
use SilverStripe\CMS\Model\VirtualPage;
6
use SilverStripe\Control\Director;
7
use SilverStripe\ORM\DataExtension;
8
use SilverStripe\StaticPublishQueue\Contract\StaticallyPublishable;
9
use SilverStripe\StaticPublishQueue\Contract\StaticPublishingTrigger;
10
11
/**
12
 * Bare-bones implementation of a publishable page.
13
 *
14
 * You can override this either by implementing one of the interfaces the class directly, or by applying
15
 * an extension via the config system ordering (inject your extension "before" the PublishableSiteTree).
16
 *
17
 * @TODO: re-implement optional publishing of all the ancestors up to the root? Currently it only republishes the parent
18
 *
19
 * @see SiteTreePublishingEngine
20
 */
21
class PublishableSiteTree extends DataExtension implements StaticallyPublishable, StaticPublishingTrigger
22
{
23
    public function getMyVirtualPages()
24
    {
25
        return VirtualPage::get()->filter(['CopyContentFrom.ID' => $this->owner->ID]);
26
    }
27
28
    /**
29
     * @param array $context
30
     * @return array
31
     */
32
    public function objectsToUpdate($context)
33
    {
34
        $list = [];
35
        switch ($context['action']) {
36
            case 'publish':
37
                // Trigger refresh of the page itself.
38
                $list[] = $this->getOwner();
39
40
                // Refresh the parent.
41
                if ($this->getOwner()->ParentID) {
42
                    $list[] = $this->getOwner()->Parent();
43
                }
44
45
                // Refresh related virtual pages.
46
                $virtuals = $this->getOwner()->getMyVirtualPages();
47
                if ($virtuals->exists()) {
48
                    foreach ($virtuals as $virtual) {
49
                        $list[] = $virtual;
50
                    }
51
                }
52
                break;
53
54
            case 'unpublish':
55
                // Refresh the parent
56
                if ($this->getOwner()->ParentID) {
57
                    $list[] = $this->getOwner()->Parent();
58
                }
59
                break;
60
        }
61
        return $list;
62
    }
63
64
    /**
65
     * @param array $context
66
     * @return array
67
     */
68
    public function objectsToDelete($context)
69
    {
70
        $list = [];
71
        switch ($context['action']) {
72
            case 'unpublish':
73
                // Trigger cache removal for this page.
74
                $list[] = $this->getOwner();
75
76
                // Trigger removal of the related virtual pages.
77
                $virtuals = $this->getOwner()->getMyVirtualPages();
78
                if ($virtuals->exists()) {
79
                    foreach ($virtuals as $virtual) {
80
                        $list[] = $virtual;
81
                    }
82
                }
83
                break;
84
        }
85
        return $list;
86
    }
87
88
    /**
89
     * The only URL belonging to this object is it's own URL.
90
     */
91
    public function urlsToCache()
92
    {
93
        return [Director::absoluteURL($this->getOwner()->Link()) => 0];
94
    }
95
}
96