Completed
Push — main ( 2daa48...b5d932 )
by
unknown
08:38
created

ImageRotator   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
dl 0
loc 40
c 0
b 0
f 0
wmc 5
lcom 0
cbo 0
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A rotate() 0 30 5
1
<?php
2
3
namespace Addwiki\Mediawiki\Api\Service;
4
5
use Addwiki\Mediawiki\Api\Client\SimpleRequest;
6
use Addwiki\Mediawiki\Api\Client\UsageException;
7
use Addwiki\Mediawiki\DataModel\File;
8
9
/**
10
 * @access private
11
 *
12
 * @author Addshore
13
 */
14
class ImageRotator extends Service {
15
16
	/**
17
	 * NOTE: This service has not been fully tested
18
	 *
19
	 * @param int $rotation Degrees to rotate image clockwise, One value: 90, 180, 270
20
	 * @throws UsageException
21
	 */
22
	public function rotate( File $file, int $rotation ): bool {
23
		$params = [
24
			'rotation' => $rotation,
25
			'token' => $this->api->getToken(),
26
		];
27
28
		if ( $file->getPageIdentifier()->getTitle() !== null ) {
29
			$params['titles'] = $file->getPageIdentifier()->getTitle()->getText();
30
		} else {
31
			$params['pageids'] = $file->getPageIdentifier()->getId();
32
		}
33
34
		$result = $this->api->postRequest( new SimpleRequest( 'imagerotate', $params ) );
35
36
		// This module sometimes gives odd errors so deal with them..
37
		if ( array_key_exists( 'imagerotate', $result ) ) {
38
			$imageRotate = array_pop( $result['imagerotate'] );
39
			if ( array_key_exists( 'result', $imageRotate ) &&
40
				$imageRotate['result'] == 'Failure'
41
			) {
42
				throw new UsageException(
43
					'imagerotate-Failure',
44
					$imageRotate['errormessage'],
45
					$result
46
				);
47
			}
48
		}
49
50
		return true;
51
	}
52
53
}
54