WordPress_GitHub_Sync_Commit   A
last analyzed

Complexity

Total Complexity 28

Size/Duplication

Total Lines 272
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 272
ccs 65
cts 65
cp 1
rs 10
c 0
b 0
f 0
wmc 28
lcom 1
cbo 1

18 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A sha() 0 3 1
A url() 0 3 1
A author() 0 3 1
A author_email() 0 7 2
A set_author() 0 7 1
A committer() 0 3 1
A set_committer() 0 7 1
A message() 0 3 1
A set_message() 0 7 1
A parents() 0 3 1
A tree_sha() 0 11 3
A tree() 0 3 1
A set_tree() 0 5 1
A already_synced() 0 3 1
A to_body() 0 10 1
B interpret_data() 0 8 7
A set_to_parent() 0 6 2
1
<?php
2
/**
3
 * API commit model.
4
 * @package WordPress_GitHub_Sync
5
 */
6
7
/**
8
 * Class WordPress_GitHub_Sync_Commit
9
 */
10
class WordPress_GitHub_Sync_Commit {
11
12
	/**
13
	 * Raw commit data.
14
	 *
15
	 * @var stdClass
16
	 */
17
	protected $data;
18
19
	/**
20
	 * Commit sha.
21
	 *
22
	 * @var string
23
	 */
24
	protected $sha;
25
26
	/**
27
	 * Commit message.
28
	 *
29
	 * @var string
30
	 */
31
	protected $message = '';
32
33
	/**
34
	 * Commit tree.
35
	 *
36
	 * @var WordPress_GitHub_Sync_Tree
37
	 */
38
	protected $tree;
39
40
	/**
41
	 * Commit api url.
42
	 *
43
	 * @var string
44
	 */
45
	protected $url;
46
47
	/**
48
	 * Commit author.
49
	 *
50
	 * @var stdClass|false
51
	 */
52
	protected $author;
53
54
	/**
55
	 * Commit committer.
56
	 *
57
	 * @var stdClass|false
58
	 */
59
	protected $committer;
60
61
	/**
62
	 * Commit parents.
63
	 *
64
	 * @var string[]
65
	 */
66
	protected $parents;
67
68
	/**
69
	 * Instantiates a new Commit object.
70
	 *
71
	 * @param stdClass $data Raw commit data.
72
	 */
73 13
	public function __construct( stdClass $data ) {
74 13
		$this->data = $data;
75
76 13
		$this->interpret_data();
77 13
	}
78
79
	/**
80
	 * Returns the commit sha.
81
	 *
82
	 * @return string
83
	 */
84 3
	public function sha() {
85 3
		return $this->sha;
86
	}
87
88
	/**
89
	 * Return the commit's API url.
90
	 *
91
	 * @return string
92
	 */
93 2
	public function url() {
94 2
		return $this->url;
95
	}
96
97
	/**
98
	 * Return the commit author.
99
	 *
100
	 * @return stdClass|false
101
	 */
102 3
	public function author() {
103 3
		return $this->author;
104
	}
105
106
	/**
107
	 * Return's the commit author's email.
108
	 *
109
	 * @return string
110
	 */
111 2
	public function author_email() {
112 2
		if ( isset( $this->author->email ) ) {
113 1
			return $this->author->email;
114
		}
115
116 1
		return '';
117
	}
118
119
	/**
120
	 * Set's the commit author.
121
	 *
122
	 * @param stdClass $author Commit author data.
123
	 *
124
	 * @return $this
125
	 */
126 1
	public function set_author( stdClass $author ) {
127 1
		$this->author = $author;
128
129 1
		$this->set_to_parent();
130
131 1
		return $this;
132
	}
133
134
	/**
135
	 * Return the commit committer.
136
	 *
137
	 * @return stdClass|false
138
	 */
139 3
	public function committer() {
140 3
		return $this->committer;
141
	}
142
143
	/**
144
	 * Set's the commit committer.
145
	 *
146
	 * @param stdClass $committer Committer data.
147
	 *
148
	 * @return $this
149
	 */
150 1
	public function set_committer( stdClass $committer ) {
151 1
		$this->committer = $committer;
152
153 1
		$this->set_to_parent();
154
155 1
		return $this;
156
	}
157
158
	/**
159
	 * Returns the commit message, if set.
160
	 *
161
	 * @return string
162
	 */
163 3
	public function message() {
164 3
		return $this->message;
165
	}
166
167
	/**
168
	 * Set's the commit message;
169
	 *
170
	 * @param string $message Commit message.
171
	 *
172
	 * @return $this
173
	 */
174 3
	public function set_message( $message ) {
175 3
		$this->message = (string) $message;
176
177 3
		$this->set_to_parent();
178
179 3
		return $this;
180
	}
181
182
	/**
183
	 * Return the commit parents.
184
	 *
185
	 * @return string[]
186
	 */
187 6
	public function parents() {
188 6
		return $this->parents;
189
	}
190
191
	/**
192
	 * Returns the commit's tree's sha.
193
	 *
194
	 * @return string
195
	 */
196 8
	public function tree_sha() {
197 8
		if ( $this->tree ) {
198 1
			return $this->tree->sha();
199
		}
200
201 7
		if ( isset( $this->data->tree ) ) {
202 6
			return $this->data->tree->sha;
203
		}
204
205 1
		return '';
206
	}
207
208
	/**
209
	 * Return's the commit's tree.
210
	 *
211
	 * @return WordPress_GitHub_Sync_Tree
212
	 */
213 5
	public function tree() {
214 5
		return $this->tree;
215
	}
216
217
	/**
218
	 * Set the commit's tree.
219
	 *
220
	 * @param WordPress_GitHub_Sync_Tree $tree New tree for commit.
221
	 *
222
	 * @return $this
223
	 */
224 5
	public function set_tree( WordPress_GitHub_Sync_Tree $tree ) {
225 5
		$this->tree = $tree;
226
227 5
		return $this;
228
	}
229
230
	/**
231
	 * Returns whether the commit is currently synced.
232
	 *
233
	 * The commit message of every commit that's exported
234
	 * by WPGHS ends with '- wpghs', so we don't sync those
235
	 * commits down.
236
	 *
237
	 * @return bool
238
	 */
239 2
	public function already_synced() {
240 2
		return 'wpghs' === substr( $this->message, - 5 );
241
	}
242
243
	/**
244
	 * Transforms the commit into the API
245
	 * body required to create a new commit.
246
	 *
247
	 * @return array
248
	 */
249 1
	public function to_body() {
250
		$body = array(
251 1
			'tree' => $this->tree_sha(),
252 1
			'message' => $this->message(),
253 1
			'parents' => $this->parents(),
254 1
		);
255
256
		// @todo set author here
257 1
		return $body;
258
	}
259
260
	/**
261
	 * Interprets the raw data object into commit properties.
262
	 */
263 13
	protected function interpret_data() {
264 13
		$this->sha       = isset( $this->data->sha ) ? $this->data->sha : '';
265 13
		$this->url       = isset( $this->data->url ) ? $this->data->url : '';
266 13
		$this->author    = isset( $this->data->author ) ? $this->data->author : false;
267 13
		$this->committer = isset( $this->data->committer ) ? $this->data->committer : false;
268 13
		$this->message   = isset( $this->data->message ) ? $this->data->message : '';
269 13
		$this->parents   = isset( $this->data->parents ) ? $this->data->parents : array();
270 13
	}
271
272
	/**
273
	 * Assigns the current sha to be its parent.
274
	 */
275 5
	protected function set_to_parent() {
276 5
		if ( $this->sha ) {
277 3
			$this->parents = array( $this->sha );
278 3
			$this->sha     = '';
279 3
		}
280 5
	}
281
}
282