Passed
Pull Request — master (#133)
by
unknown
01:55
created

SiteTreePublishingEngine::onBeforeUnpublish()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 6
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace SilverStripe\StaticPublishQueue\Extension\Engine;
4
5
use SilverStripe\CMS\Model\SiteTreeExtension;
6
use SilverStripe\Core\Environment;
7
use SilverStripe\Core\Injector\Injector;
8
use SilverStripe\StaticPublishQueue\Contract\StaticPublishingTrigger;
9
use SilverStripe\StaticPublishQueue\Extension\Publishable\PublishableSiteTree;
10
use SilverStripe\StaticPublishQueue\Job\DeleteStaticCacheJob;
11
use SilverStripe\StaticPublishQueue\Job\GenerateStaticCacheJob;
12
use Symbiote\QueuedJobs\Services\QueuedJobService;
13
14
/**
15
 * This extension couples to the StaticallyPublishable and StaticPublishingTrigger implementations
16
 * on the SiteTree objects and makes sure the actual change to SiteTree is triggered/enqueued.
17
 *
18
 * Provides the following information as a context to StaticPublishingTrigger:
19
 * * action - name of the executed action: publish or unpublish
20
 *
21
 * @see PublishableSiteTree
22
 */
23
class SiteTreePublishingEngine extends SiteTreeExtension
24
{
25
    /**
26
     * Queues the urls to be flushed into the queue.
27
     *
28
     * @var array
29
     */
30
    private $toUpdate = [];
31
32
    /**
33
     * Queues the urls to be deleted as part of a next flush operation.
34
     *
35
     * @var array
36
     */
37
    private $toDelete = [];
38
39
    /**
40
     * @return array
41
     */
42
    public function getToUpdate()
43
    {
44
        return $this->toUpdate;
45
    }
46
47
    /**
48
     * @return array
49
     */
50
    public function getToDelete()
51
    {
52
        return $this->toDelete;
53
    }
54
55
    /**
56
     * @param array $toUpdate
57
     * @return $this
58
     */
59
    public function setToUpdate($toUpdate)
60
    {
61
        $this->toUpdate = $toUpdate;
62
        return $this;
63
    }
64
65
    /**
66
     * @param array $toDelete
67
     * @return $this
68
     */
69
    public function setToDelete($toDelete)
70
    {
71
        $this->toDelete = $toDelete;
72
        return $this;
73
    }
74
75
    /**
76
     * @param \SilverStripe\CMS\Model\SiteTree|null $original
77
     */
78
    public function onAfterPublishRecursive(&$original)
79
    {
80
        // if the site tree has been "reorganised" (ie: the parentID has changed)
81
        // then this is eht equivalent of an unpublish and publish as far as the
82
        // static publisher is concerned
83
        if ($original && (
84
                $original->ParentID !== $this->getOwner()->ParentID
85
                || $original->URLSegment !== $this->getOwner()->URLSegment
86
            )
87
        ) {
88
            $context = [
89
                'action' => 'unpublish',
90
            ];
91
            $original->collectChanges($context);
92
            $original->flushChanges();
93
        }
94
        $context = [
95
            'action' => 'publish',
96
        ];
97
        $this->collectChanges($context);
98
        $this->flushChanges();
99
    }
100
101
    public function onBeforeUnpublish()
102
    {
103
        $context = [
104
            'action' => 'unpublish',
105
        ];
106
        $this->collectChanges($context);
107
    }
108
109
    public function onAfterUnpublish()
110
    {
111
        $this->flushChanges();
112
    }
113
114
    /**
115
     * Collect all changes for the given context.
116
     *
117
     * @param array $context
118
     */
119
    public function collectChanges($context)
120
    {
121
        Environment::increaseMemoryLimitTo();
122
        Environment::increaseTimeLimitTo();
123
124
        if ($this->getOwner()->hasExtension(PublishableSiteTree::class)
125
            || $this->getOwner() instanceof StaticPublishingTrigger
126
        ) {
127
            $toUpdate = $this->getOwner()->objectsToUpdate($context);
128
            $this->setToUpdate($toUpdate);
129
130
            $toDelete = $this->getOwner()->objectsToDelete($context);
131
            $this->setToDelete($toDelete);
132
        }
133
    }
134
135
    /**
136
     * Execute URL deletions, enqueue URL updates.
137
     */
138
    public function flushChanges()
139
    {
140
        $queue = QueuedJobService::singleton();
141
        if (!empty($this->toUpdate)) {
142
            foreach ($this->toUpdate as $queueItem) {
143
                $job = Injector::inst()->create(GenerateStaticCacheJob::class);
144
145
                $jobData = new \stdClass();
146
                $urls = $queueItem->urlsToCache();
147
                ksort($urls);
148
                $jobData->URLsToProcess = $urls;
149
150
                $job->setJobData(0, 0, false, $jobData, [
151
                    'Building URLs: ' . var_export(array_keys($jobData->URLsToProcess), true),
152
                ]);
153
154
                $queue->queueJob($job);
155
            }
156
            $this->toUpdate = [];
157
        }
158
159
        if (!empty($this->toDelete)) {
160
            foreach ($this->toDelete as $queueItem) {
161
                $job = Injector::inst()->create(DeleteStaticCacheJob::class);
162
163
                $jobData = new \stdClass();
164
                $urls = $queueItem->urlsToCache();
165
                ksort($urls);
166
                $jobData->URLsToProcess = $urls;
167
168
                $job->setJobData(0, 0, false, $jobData, [
169
                    'Purging URLs: ' . var_export(array_keys($jobData->URLsToProcess), true),
170
                ]);
171
172
                $queue->queueJob($job);
173
            }
174
            $this->toDelete = [];
175
        }
176
    }
177
}
178