Passed
Push — feature/rebusify ( 495106...67b08f )
by Paul
06:10 queued 02:03
created

Upgrader::updateVersionFrom()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace GeminiLabs\SiteReviews\Modules;
4
5
use DirectoryIterator;
6
use GeminiLabs\SiteReviews\Database\OptionManager;
7
8
class Upgrader
9
{
10
    /**
11
     * @return string
12
     */
13
    public $currentVersion;
14
15
    /**
16
     * @return void
17
     */
18 7
    public function run()
19
    {
20 7
        $filenames = [];
21 7
        $iterator = new DirectoryIterator(dirname(__FILE__).'/Upgrader');
22 7
        foreach ($iterator as $fileinfo) {
23 7
            if ($fileinfo->isFile()) {
24 7
                $filenames[] = $fileinfo->getFilename();
25
            }
26
        }
27 7
        natsort($filenames);
28 7
        $this->currentVersion = $this->currentVersion();
29 7
        array_walk($filenames, function ($file) {
30 7
            $className = str_replace('.php', '', $file);
31 7
            $upgradeFromVersion = str_replace(['Upgrade_', '_'], ['', '.'], $className);
32 7
            $suffix = preg_replace('/[\d.]+(.+)?/', '${1}', glsr()->version); // allow alpha/beta versions
33 7
            if ('0.0.0' == $this->currentVersion
34 7
                || version_compare($this->currentVersion, $upgradeFromVersion.$suffix, '>=')) {
35 7
                return;
36
            }
37
            glsr('Modules\\Upgrader\\'.$className);
38
            glsr_log()->notice('Completed Upgrade for v'.$upgradeFromVersion.$suffix);
39 7
        });
40 7
        $this->finish();
41 7
    }
42
43
    /**
44
     * @return void
45
     */
46 7
    public function finish()
47
    {
48 7
        if ($this->currentVersion !== glsr()->version) {
49 6
            $this->setReviewCounts();
50 6
            $this->updateVersionFrom($this->currentVersion);
51 1
        } elseif (!glsr(OptionManager::class)->get('last_review_count', false)) {
52 1
            $this->setReviewCounts();
53
        }
54 7
    }
55
56
    /**
57
     * @return string
58
     */
59 7
    protected function currentVersion()
60
    {
61 7
        $fallback = '0.0.0';
62 7
        $majorVersions = [4, 3, 2];
63 7
        foreach ($majorVersions as $majorVersion) {
64 7
            $settings = get_option(OptionManager::databaseKey($majorVersion));
65 7
            $version = glsr_get($settings, 'version', $fallback);
66 7
            if (version_compare($version, $fallback, '>')) {
67 7
                return $version;
68
            }
69
        }
70 6
        return $fallback;
71
    }
72
73
    /**
74
     * @return void
75
     */
76 7
    protected function setReviewCounts()
77
    {
78 7
        add_action('admin_init', 'glsr_calculate_ratings');
79 7
    }
80
81
    /**
82
     * @param string $previousVersion
83
     * @return void
84
     */
85 6
    protected function updateVersionFrom($previousVersion)
86
    {
87 6
        glsr(OptionManager::class)->set('version', glsr()->version);
88 6
        glsr(OptionManager::class)->set('version_upgraded_from', $previousVersion);
89 6
    }
90
}
91