Passed
Push — master ( 7de9ff...9a7a6b )
by Paul
07:10 queued 03:33
created

Upgrader   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 25
dl 0
loc 55
ccs 32
cts 32
cp 1
rs 10
c 0
b 0
f 0
wmc 9

4 Methods

Rating   Name   Duplication   Size   Complexity  
A updateVersion() 0 6 2
A setReviewCounts() 0 6 2
A run() 0 19 4
A currentVersion() 0 3 1
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 7
	public function run()
15
	{
16 7
		$filenames = [];
17 7
		$iterator = new DirectoryIterator( dirname( __FILE__ ).'/Upgrader' );
18 7
		foreach( $iterator as $fileinfo ) {
19 7
			if( !$fileinfo->isFile() )continue;
20 7
			$filenames[] = $fileinfo->getFilename();
21
		}
22 7
		natsort( $filenames );
23 7
		array_walk( $filenames, function( $file ) {
24 7
			$className = str_replace( '.php', '', $file );
25 7
			$version = str_replace( ['Upgrade_', '_'], ['', '.'], $className );
26 7
			$versionSuffix = preg_replace( '/[\d.]+(.+)?/', '${1}', glsr()->version ); // allow alpha/beta versions
27 7
			if( version_compare( $this->currentVersion(), $version.$versionSuffix, '>=' ))return;
28 6
			glsr( 'Modules\\Upgrader\\'.$className );
29 6
			glsr_log()->info( 'Completed Upgrade for v'.$version.$versionSuffix );
30 7
		});
31 7
		$this->setReviewCounts();
32 7
		$this->updateVersion();
33 7
	}
34
35
	/**
36
	 * @return string
37
	 */
38 7
	public function currentVersion()
39
	{
40 7
		return glsr( OptionManager::class )->get( 'version', '0.0.0' );
41
	}
42
43
	/**
44
	 * @return void
45
	 */
46 7
	public function setReviewCounts()
47
	{
48 7
		if( !empty( glsr( OptionManager::class )->get( 'last_review_count' )))return;
49 6
		add_action( 'admin_init', function() {
50 1
			glsr( AdminController::class )->routerCountReviews( false );
51 1
			glsr_log()->info( __( 'Calculated rating counts.', 'site-reviews' ));
52 6
		});
53 6
	}
54
55
	/**
56
	 * @return void
57
	 */
58 7
	public function updateVersion()
59
	{
60 7
		$currentVersion = $this->currentVersion();
61 7
		if( $currentVersion !== glsr()->version ) {
62 6
			glsr( OptionManager::class )->set( 'version', glsr()->version );
63 6
			glsr( OptionManager::class )->set( 'version_upgraded_from', $currentVersion );
64
		}
65 7
	}
66
}
67