Passed
Pull Request — master (#133)
by
unknown
02:20
created

SiteTreePublishingEngine::onAfterPublish()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 21
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 11
nc 2
nop 1
dl 0
loc 21
rs 9.9
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)
0 ignored issues
show
Unused Code introduced by
The parameter $original is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

78
    public function onAfterPublishRecursive(/** @scrutinizer ignore-unused */ &$original)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
79
    {
80
        return;
81
        // if the site tree has been "reorganised" (ie: the parentID has changed)
82
        // then this is eht equivalent of an unpublish and publish as far as the
83
        // static publisher is concerned
84
        if ($original && (
0 ignored issues
show
Unused Code introduced by
IfNode is not reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
85
                $original->ParentID !== $this->getOwner()->ParentID
86
                || $original->URLSegment !== $this->getOwner()->URLSegment
87
            )
88
        ) {
89
            $context = [
90
                'action' => 'unpublish',
91
            ];
92
            $original->collectChanges($context);
93
            $original->flushChanges();
94
        }
95
        $context = [
96
            'action' => 'publish',
97
        ];
98
        $this->collectChanges($context);
99
        $this->flushChanges();
100
    }
101
102
    public function onBeforeUnpublish()
103
    {
104
        return;
105
        $context = [
0 ignored issues
show
Unused Code introduced by
$context = array('action' => 'unpublish') is not reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
106
            'action' => 'unpublish',
107
        ];
108
        $this->collectChanges($context);
109
    }
110
111
    public function onAfterUnpublish()
112
    {
113
        return;
114
        $this->flushChanges();
0 ignored issues
show
Unused Code introduced by
$this->flushChanges() is not reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
115
    }
116
117
    /**
118
     * Collect all changes for the given context.
119
     *
120
     * @param array $context
121
     */
122
    public function collectChanges($context)
123
    {
124
        Environment::increaseMemoryLimitTo();
125
        Environment::increaseTimeLimitTo();
126
127
        if ($this->getOwner()->hasExtension(PublishableSiteTree::class)
128
            || $this->getOwner() instanceof StaticPublishingTrigger
129
        ) {
130
            $toUpdate = $this->getOwner()->objectsToUpdate($context);
131
            $this->setToUpdate($toUpdate);
132
133
            $toDelete = $this->getOwner()->objectsToDelete($context);
134
            $this->setToDelete($toDelete);
135
        }
136
    }
137
138
    /**
139
     * Execute URL deletions, enqueue URL updates.
140
     */
141
    public function flushChanges()
142
    {
143
        $queue = QueuedJobService::singleton();
144
        if (!empty($this->toUpdate)) {
145
            foreach ($this->toUpdate as $queueItem) {
146
                $job = Injector::inst()->create(GenerateStaticCacheJob::class);
147
148
                $jobData = new \stdClass();
149
                $urls = $queueItem->urlsToCache();
150
                ksort($urls);
151
                $jobData->URLsToProcess = $urls;
152
153
                $job->setJobData(0, 0, false, $jobData, [
154
                    'Building URLs: ' . var_export(array_keys($jobData->URLsToProcess), true),
155
                ]);
156
157
                $queue->queueJob($job);
158
            }
159
            $this->toUpdate = [];
160
        }
161
162
        if (!empty($this->toDelete)) {
163
            foreach ($this->toDelete as $queueItem) {
164
                $job = Injector::inst()->create(DeleteStaticCacheJob::class);
165
166
                $jobData = new \stdClass();
167
                $urls = $queueItem->urlsToCache();
168
                ksort($urls);
169
                $jobData->URLsToProcess = $urls;
170
171
                $job->setJobData(0, 0, false, $jobData, [
172
                    'Purging URLs: ' . var_export(array_keys($jobData->URLsToProcess), true),
173
                ]);
174
175
                $queue->queueJob($job);
176
            }
177
            $this->toDelete = [];
178
        }
179
    }
180
}
181