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

StaticCacheFullBuildJob::process()   D

Complexity

Conditions 9
Paths 12

Size

Total Lines 30
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
eloc 21
nc 12
nop 0
dl 0
loc 30
rs 4.909
c 0
b 0
f 0
1
<?php
2
3
namespace SilverStripe\StaticPublishQueue\Job;
4
5
use SilverStripe\CMS\Model\SiteTree;
6
use SilverStripe\StaticPublishQueue\Contract\StaticallyPublishable;
7
use SilverStripe\StaticPublishQueue\Extension\Publishable\PublishableSiteTree;
8
use SilverStripe\StaticPublishQueue\Job;
9
use SilverStripe\StaticPublishQueue\Publisher;
10
use SilverStripe\Versioned\Versioned;
11
12
/**
13
 * Adds all pages to the queue for caching. Best implemented on a cron via StaticCacheFullBuildTask.
14
 */
15
class StaticCacheFullBuildJob extends Job
16
{
17
    /**
18
     * @return string
19
     */
20
    public function getTitle()
21
    {
22
        return 'Generate a static pages for all URLs';
23
    }
24
25
    /**
26
     * @return string
27
     */
28
    public function getSignature()
29
    {
30
        return md5(static::class);
31
    }
32
33
    public function setup()
34
    {
35
        parent::setup();
36
        $this->URLsToProcess = $this->getAllLivePageURLs();
0 ignored issues
show
Bug Best Practice introduced by
The property URLsToProcess does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
37
        $this->URLsToCleanUp = [];
0 ignored issues
show
Bug Best Practice introduced by
The property URLsToCleanUp does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
38
        $this->totalSteps = ceil(count($this->URLsToProcess) / self::config()->get('chunk_size'));
39
        $this->addMessage(sprintf('Building %s URLS', count($this->URLsToProcess)));
40
        $this->addMessage(var_export(array_keys($this->URLsToProcess), true));
41
    }
42
43
    /**
44
     * Do some processing yourself!
45
     */
46
    public function process()
47
    {
48
        $chunkSize = self::config()->get('chunk_size');
49
        $count = 0;
50
        foreach ($this->jobData->URLsToProcess as $url => $priority) {
51
            if (++$count > $chunkSize) {
52
                break;
53
            }
54
            $meta = Publisher::singleton()->publishURL($url, true);
55
            if (!empty($meta['success'])) {
56
                $this->jobData->ProcessedURLs[$url] = $url;
57
                unset($this->jobData->URLsToProcess[$url]);
58
            }
59
        }
60
        if (empty($this->jobData->URLsToProcess)) {
61
            $trimSlashes = function ($value) {
62
                return trim($value, '/');
63
            };
64
            $this->jobData->publishedURLs = array_map($trimSlashes, Publisher::singleton()->getPublishedURLs());
65
            $this->jobData->ProcessedURLs = array_map($trimSlashes, $this->jobData->ProcessedURLs);
66
            $this->jobData->URLsToCleanUp = array_diff($this->jobData->publishedURLs, $this->jobData->ProcessedURLs);
67
            foreach ($this->jobData->URLsToCleanUp as $staleURL) {
68
                $staleURL = $staleURL == '' ? '/' : $staleURL;
69
                $purgeMeta = Publisher::singleton()->purgeURL($staleURL);
70
                if (!empty($purgeMeta['success'])) {
71
                    unset($this->jobData->URLsToCleanUp[$staleURL]);
72
                }
73
            }
74
        };
75
        $this->isComplete = empty($this->jobData->URLsToProcess) && empty($this->jobData->URLsToCleanUp);
76
    }
77
78
    /**
79
     *
80
     * @return array
81
     */
82
    protected function getAllLivePageURLs()
83
    {
84
        $urls = [];
85
        $this->extend('beforeGetAllLivePageURLs', $urls);
86
        $livePages = Versioned::get_by_stage(SiteTree::class, Versioned::LIVE);
87
        foreach ($livePages as $page) {
88
            if ($page->hasExtension(PublishableSiteTree::class) || $page instanceof StaticallyPublishable) {
89
                $urls = array_merge($urls, $page->urlsToCache());
90
            }
91
        }
92
93
        $this->extend('afterGetAllLivePageURLs', $urls);
94
        // @TODO look here when integrating subsites
95
        // if (class_exists(Subsite::class)) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
54% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
96
        //     Subsite::disable_subsite_filter(true);
0 ignored issues
show
Unused Code Comprehensibility introduced by
63% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
97
        // }
98
        return $urls;
99
    }
100
}
101