Completed
Push — master ( 3f5086...28f5b7 )
by mw
02:06
created

ApprovedRevsHandler::doChangeFile()   A

Complexity

Conditions 5
Paths 9

Size

Total Lines 27

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 5.009

Importance

Changes 0
Metric Value
dl 0
loc 27
ccs 13
cts 14
cp 0.9286
rs 9.1768
c 0
b 0
f 0
cc 5
nc 9
nop 2
crap 5.009
1
<?php
2
3
namespace SMW\ApprovedRevs;
4
5
use Title;
6
use Revision;
7
use RepoGroup;
8
use OldLocalFile;
9
use File;
10
11
/**
12
 * @license GNU GPL v2+
13
 * @since 1.0
14
 *
15
 * @author mwjames
16
 */
17
class ApprovedRevsHandler {
18
19
	/**
20
	 * @var ApprovedRevsFacade
21
	 */
22
	private $approvedRevsFacade;
23
24
	/**
25
	 * @var RepoGroup
26
	 */
27
	private $repoGroup;
28
29
	/**
30
	 * @since 1.0
31
	 *
32
	 * @param ApprovedRevsFacade $approvedRevsFacade
33
	 * @param RepoGroup|null $repoGroup
34
	 */
35 9
	public function __construct( ApprovedRevsFacade $approvedRevsFacade, RepoGroup $repoGroup = null ) {
36 9
		$this->approvedRevsFacade = $approvedRevsFacade;
37 9
		$this->repoGroup = $repoGroup;
38 9
	}
39
40
	/**
41
	 * @since  1.0
42
	 *
43
	 * @param Title $title
44
	 * @param integer $latestRevID
45
	 *
46
	 * @return boolean
47
	 */
48 4
	public function isApprovedUpdate( Title $title, $latestRevID ) {
49
50 4
		if ( !$this->approvedRevsFacade->hasApprovedRevision( $title ) ) {
51 1
			return true;
52
		}
53
54 3
		if ( ( $approvedRevID = $this->approvedRevsFacade->getApprovedRevID( $title ) ) !== null ) {
55 2
			return $approvedRevID == $latestRevID;
56
		}
57
58 1
		return true;
59
	}
60
61
	/**
62
	 * @since  1.0
63
	 *
64
	 * @param Title $title
65
	 * @param Revision|null &$revision
66
	 */
67 1
	public function doChangeRevision( Title $title, &$revision ) {
68
69
		// Forcibly change the revision to match what ApprovedRevs sees as
70
		// approved
71 1
		if ( ( $approvedRevID = $this->approvedRevsFacade->getApprovedRevID( $title ) ) !== null ) {
72 1
			$approvedRev = Revision::newFromId( $approvedRevID );
73
74 1
			if ( $approvedRev instanceof Revision ) {
0 ignored issues
show
Bug introduced by
The class Revision does not exist. Is this class maybe located in a folder that is not analyzed, or in a newer version of your dependencies than listed in your composer.lock/composer.json?
Loading history...
75
				$revision = $approvedRev;
76
			}
77
		}
78 1
	}
79
80
	/**
81
	 * @since  1.0
82
	 *
83
	 * @param Title $title
84
	 * @param integer &$revisionID
85
	 */
86 1
	public function doChangeRevisionID( Title $title, &$revisionID ) {
87
88 1
		if ( ( $approvedRevID = $this->approvedRevsFacade->getApprovedRevID( $title ) ) !== null ) {
89 1
			$revisionID = $approvedRevID;
90
		}
91 1
	}
92
93
	/**
94
	 * @since  1.0
95
	 *
96
	 * @param Title $title
97
	 * @param File &$file
98
	 */
99 2
	public function doChangeFile( Title $title, &$file ) {
100
101 2
		list( $timestamp, $file_sha1 ) = $this->approvedRevsFacade->getApprovedFileInfo( $title );
102
103 2
		if ( $file_sha1 === false ) {
104 1
			return true;
105
		}
106
107 1
		if ( $this->repoGroup === null ) {
108
			$this->repoGroup = RepoGroup::singleton();
109
		}
110
111 1
		$localRepo = $this->repoGroup->getLocalRepo();
112
113
		// Retrievalable from the archive?
114 1
		$file = OldLocalFile::newFromKey( $file_sha1, $localRepo, $timestamp );
115
116
		// Try the local repo!
117 1
		if ( $file === false ) {
118 1
			$files = $localRepo->findBySha1( $file_sha1 );
119 1
			$file = end( $files );
120
		}
121
122 1
		if ( $file instanceof File ) {
0 ignored issues
show
Bug introduced by
The class File does not exist. Is this class maybe located in a folder that is not analyzed, or in a newer version of your dependencies than listed in your composer.lock/composer.json?
Loading history...
123 1
			$file->file_sha1 = $file_sha1;
124
		}
125 1
	}
126
127
}
128