Passed
Pull Request — master (#132)
by
unknown
02:58
created

onAfterPublishRecursive()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 21
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

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