Passed
Pull Request — master (#70)
by Daniel
02:11
created

PublishableSiteTree::urlsToCache()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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