Completed
Pull Request — master (#167)
by
unknown
05:55
created

WordPress_GitHub_Sync_Payload::get_commit()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * GitHub Webhook payload.
4
 * @package WordPress_GitHub_Sync
5
 */
6
7
/**
8
 * Class WordPress_GitHub_Sync_Payload
9
 */
10
class WordPress_GitHub_Sync_Payload {
11
12
	/**
13
	 * Application container.
14
	 *
15
	 * @var WordPress_GitHub_Sync
16
	 */
17
	protected $app;
18
19
	/**
20
	 * Payload data.
21
	 *
22
	 * @var stdClass
23
	 */
24
	protected $data;
25
26
	/**
27
	 * WordPress_GitHub_Sync_Payload constructor.
28
	 *
29
	 * @param WordPress_GitHub_Sync $app      Application container.
30
	 * @param string                $raw_data Raw request data.
31
	 */
32 5
	public function __construct( WordPress_GitHub_Sync $app, $raw_data ) {
33 5
		$this->app  = $app;
34 5
		$this->data = json_decode( $raw_data );
35 5
	}
36
37
	/**
38
	 * Returns whether payload should be imported.
39
	 *
40
	 * @return bool
41
	 */
42 4
	public function should_import() {
43
		// @todo how do we get this without importing the whole api object just for this?
44 4
		if ( strtolower( $this->data->repository->full_name ) !== strtolower( $this->app->api()->fetch()->repository() ) ) {
45 1
			return false;
46
		}
47
48
		// The last term in the ref is the payload_branch name.
49 3
		$refs   = explode( '/', $this->data->ref );
50 3
		$payload_branch = array_pop( $refs );
51 3
		$sync_branch = apply_filters( 'wpghs_sync_branch', 'master' );
52
53 3
		if ( ! $sync_branch ) {
54
			throw new Exception( __( 'Sync branch not set. Filter `wpghs_sync_branch` misconfigured.', 'wp-github-sync' ) );
55
		}
56
57 3
		if ( $sync_branch !== $payload_branch ) {
58 1
			return false;
59
		}
60
61
		// We add a tag to commits we push out, so we shouldn't pull them in again.
62 2
		$tag = apply_filters( 'wpghs_commit_msg_tag', 'wpghs' );
63
64 2
		if ( ! $tag ) {
65
			throw new Exception( __( 'Commit message tag not set. Filter `wpghs_commit_msg_tag` misconfigured.', 'wp-github-sync' ) );
66
		}
67
68 2
		if ( $tag === substr( $this->message(), -1 * strlen( $tag ) ) ) {
69 1
			return false;
70
		}
71
72 1
		if ( ! $this->get_commit_id() ) {
73
			return false;
74
		}
75
76 1
		return true;
77
	}
78
79
	/**
80
	 * Returns the head commit.
81
	 *
82
	 * @return string
83
	 */
84
	public function get_commit() {
85
		return $this->data->head_commit;
86
	}
87
88
	/**
89
	 * Returns the sha of the head commit.
90
	 *
91
	 * @return string
92
	 */
93 2
	public function get_commit_id() {
94 2
		return $this->data->head_commit ? $this->data->head_commit->id : null;
95
	}
96
97
	/**
98
	 * Returns the email address for the commit author.
99
	 *
100
	 * @return string
101
	 */
102 1
	public function get_author_email() {
103 1
		return $this->data->head_commit->author->email;
104
	}
105
106
	/**
107
	 * Returns array commits for the payload.
108
	 *
109
	 * @return array
110
	 */
111 1
	public function get_commits() {
112 1
		return $this->data->commits;
113
	}
114
115
	/**
116
	 * Returns the repository's full name.
117
	 *
118
	 * @return string
119
	 */
120
	public function get_repository_name() {
121
		return $this->data->repository->full_name;
122
	}
123
124
	/**
125
	 * Returns the payload's commit message.
126
	 *
127
	 * @return string
128
	 */
129 2
	protected function message() {
130 2
		return $this->data->head_commit->message;
131
	}
132
}
133