Content   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 90%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 0
dl 0
loc 74
ccs 18
cts 20
cp 0.9
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getModel() 0 3 1
A getHash() 0 14 4
A hasChanged() 0 3 1
A getData() 0 3 1
1
<?php
2
3
namespace Mediawiki\DataModel;
4
5
use LogicException;
6
7
/**
8
 * Class Representing the content of a revision
9
 *
10
 * @author Addshore
11
 */
12
class Content {
13
14
	/**
15
	 * @var string sha1 hash of the object content upon creation
16
	 */
17
	private $initialHash;
18
19
	/**
20
	 * @var mixed
21
	 */
22
	private $data;
23
24
	/**
25
	 * @var string|null
26
	 */
27
	private $model;
28
29
	/**
30
	 * Should always be called AFTER overriding constructors so a hash can be created
31
	 *
32
	 * @param mixed $data
33
	 * @param string|null $model
34 3
	 */
35 3
	public function __construct( $data, $model = null ) {
36 3
		$this->data = $data;
37 3
		$this->model = $model;
38 3
		$this->initialHash = $this->getHash();
39
	}
40
41
	/**
42
	 * @return string
43 3
	 */
44 3
	public function getModel() {
45
		return $this->model;
46
	}
47
48
	/**
49
	 * Returns a sha1 hash of the content
50
	 *
51
	 * @throws LogicException
52
	 * @return string
53 3
	 */
54 3
	public function getHash() {
55 3
		$data = $this->getData();
56 1
		if ( is_object( $data ) ) {
57
			if ( method_exists( $data, 'getHash' ) ) {
58
				return $data->getHash();
59 1
			} else {
60
				return sha1( serialize( $data ) );
61
			}
62 2
		}
63 2
		if ( is_string( $data ) ) {
64
			return sha1( $data );
65
		}
66
		throw new LogicException( "Cant get hash for data of type: " . gettype( $data ) );
67
	}
68
69
	/**
70
	 * Has the content been changed since object construction (this shouldn't happen!)
71
	 *
72 3
	 * @return bool
73 3
	 */
74
	public function hasChanged() {
75
		return $this->initialHash !== $this->getHash();
76
	}
77
78
	/**
79 3
	 * @return mixed
80 3
	 */
81
	public function getData() {
82
		return $this->data;
83
	}
84
85
}
86