WordPress_GitHub_Sync_Persist_Client   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 110
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 110
ccs 42
cts 42
cp 1
rs 10
c 0
b 0
f 0
wmc 12
lcom 1
cbo 3

5 Methods

Rating   Name   Duplication   Size   Complexity  
B commit() 0 32 5
A create_tree() 0 3 1
A create_commit() 0 9 2
A set_ref() 0 3 1
A export_user() 0 23 3
1
<?php
2
/**
3
 * API Persist client.
4
 * @package WordPress_GitHub_Sync
5
 */
6
7
/**
8
 * Class WordPress_GitHub_Sync_Persist_Client
9
 */
10
class WordPress_GitHub_Sync_Persist_Client extends WordPress_GitHub_Sync_Base_Client {
11
12
	/**
13
	 * Add a new commit to the master branch.
14
	 *
15
	 * @param WordPress_GitHub_Sync_Commit $commit Commit to create.
16
	 *
17
	 * @return bool|mixed|WordPress_GitHub_Sync_Commit|WP_Error
18
	 */
19 6
	public function commit( WordPress_GitHub_Sync_Commit $commit ) {
20 6
		if ( ! $commit->tree()->is_changed() ) {
21 1
			return new WP_Error(
22 1
				'no_commit',
23 1
				__(
24 1
					'There were no changes, so no additional commit was added.',
25
					'wp-github-sync'
26 1
				)
27 1
			);
28
		}
29
30 5
		$tree = $this->create_tree( $commit->tree() );
31
32 5
		if ( is_wp_error( $tree ) ) {
33 1
			return $tree;
34
		}
35
36 4
		$commit->tree()->set_sha( $tree->sha );
37 4
		$commit = $this->create_commit( $commit );
38
39 4
		if ( is_wp_error( $commit ) ) {
40 1
			return $commit;
41
		}
42
43 3
		$ref = $this->set_ref( $commit->sha );
44
45 3
		if ( is_wp_error( $ref ) ) {
46 1
			return $ref;
47
		}
48
49 2
		return true;
50
	}
51
52
	/**
53
	 * Create the tree by a set of blob ids.
54
	 *
55
	 * @param WordPress_GitHub_Sync_Tree $tree Tree to create.
56
	 *
57
	 * @return stdClass|WP_Error
58
	 */
59 5
	protected function create_tree( WordPress_GitHub_Sync_Tree $tree ) {
60 5
		return $this->call( 'POST', $this->tree_endpoint(), $tree->to_body() );
61
	}
62
63
	/**
64
	 * Create the commit from tree sha.
65
	 *
66
	 * @param WordPress_GitHub_Sync_Commit $commit Commit to create.
67
	 *
68
	 * @return mixed
69
	 */
70 4
	protected function create_commit( WordPress_GitHub_Sync_Commit $commit ) {
71 4
		$body = $commit->to_body();
72
73 4
		if ( $author = $this->export_user() ) {
74 4
			$body['author'] = $author;
75 4
		}
76
77 4
		return $this->call( 'POST', $this->commit_endpoint(), $body );
78
	}
79
80
	/**
81
	 * Updates the master branch to point to the new commit
82
	 *
83
	 * @param string $sha Sha for the commit for the master branch.
84
	 *
85
	 * @return mixed
86
	 */
87 3
	protected function set_ref( $sha ) {
88 3
		return $this->call( 'PATCH', $this->reference_endpoint(), array( 'sha' => $sha ) );
89
	}
90
91
	/**
92
	 * Get the data for the current user.
93
	 *
94
	 * @return array
95
	 */
96 4
	protected function export_user() {
97
		// @todo constant/abstract out?
98 4
		if ( $user_id = (int) get_option( '_wpghs_export_user_id' ) ) {
99 1
			delete_option( '_wpghs_export_user_id' );
100 1
		} else {
101 3
			$user_id = get_current_user_id();
102
		}
103
104 4
		$user = get_userdata( $user_id );
105
106 4
		if ( ! $user ) {
107
			// @todo is this what we want to include here?
108
			return array(
109 3
				'name'  => 'Anonymous',
110 3
				'email' => '[email protected]',
111 3
			);
112
		}
113
114
		return array(
115 1
			'name'  => $user->display_name,
116 1
			'email' => $user->user_email,
117 1
		);
118
	}
119
}
120