Test Failed
Push — hotfix/fix-counts ( 1fe4ce...872cd6 )
by Paul
03:14
created

Upgrader::finish()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

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