Completed
Push — master ( fcca4d...4fbee8 )
by James
11s
created

WordPress_GitHub_Sync_Import::blob_to_post()   D

Complexity

Conditions 9
Paths 73

Size

Total Lines 45
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 25
CRAP Score 9

Importance

Changes 0
Metric Value
cc 9
eloc 26
nc 73
nop 1
dl 0
loc 45
ccs 25
cts 25
cp 1
crap 9
rs 4.909
c 0
b 0
f 0
1
<?php
2
/**
3
 * GitHub Import Manager
4
 *
5
 * @package WordPress_GitHub_Sync
6
 */
7
8
/**
9
 * Class WordPress_GitHub_Sync_Import
10
 */
11
class WordPress_GitHub_Sync_Import {
12
13
	/**
14
	 * Application container.
15
	 *
16
	 * @var WordPress_GitHub_Sync
17
	 */
18
	protected $app;
19
20
	/**
21
	 * Initializes a new import manager.
22
	 *
23
	 * @param WordPress_GitHub_Sync $app Application container.
24
	 */
25 10
	public function __construct( WordPress_GitHub_Sync $app ) {
26 10
		$this->app = $app;
27 10
	}
28
29
	/**
30
	 * Imports a payload.
31
	 *
32
	 * @param WordPress_GitHub_Sync_Payload $payload GitHub payload object.
33
	 *
34
	 * @return string|WP_Error
35
	 */
36 2
	public function payload( WordPress_GitHub_Sync_Payload $payload ) {
37
		/**
38
		 * Whether there's an error during import.
39
		 *
40
		 * @var false|WP_Error $error
41
		 */
42 2
		$error = false;
43
44 2
		$result = $this->commit( $this->app->api()->fetch()->commit( $payload->get_commit_id() ) );
0 ignored issues
show
Bug introduced by
It seems like $this->app->api()->fetch...yload->get_commit_id()) targeting WordPress_GitHub_Sync_Fetch_Client::commit() can also be of type object<WordPress_GitHub_Sync_Tree> or object<stdClass>; however, WordPress_GitHub_Sync_Import::commit() does only seem to accept object<WordPress_GitHub_...ommit>|object<WP_Error>, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
45
46 2
		if ( is_wp_error( $result ) ) {
47 1
			$error = $result;
48 1
		}
49
50 2
		$removed = array();
51 2
		foreach ( $payload->get_commits() as $commit ) {
52 2
			$removed = array_merge( $removed, $commit->removed );
53 2
		}
54 2
		foreach ( array_unique( $removed ) as $path ) {
55 2
			$result = $this->app->database()->delete_post_by_path( $path );
56
57 2
			if ( is_wp_error( $result ) ) {
58
				if ( $error ) {
59
					$error->add( $result->get_error_code(), $result->get_error_message() );
60
				} else {
61
					$error = $result;
62
				}
63
			}
64 2
		}
65
66 2
		if ( $error ) {
67 1
			return $error;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $error; (WordPress_GitHub_Sync_Co...Sync_Tree|boolean|array) is incompatible with the return type documented by WordPress_GitHub_Sync_Import::payload of type string|WP_Error.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
68
		}
69
70 1
		return __( 'Payload processed', 'wp-github-sync' );
71
	}
72
73
	/**
74
	 * Imports the latest commit on the master branch.
75
	 *
76
	 * @return string|WP_Error
77
	 */
78 8
	public function master() {
79 8
		return $this->commit( $this->app->api()->fetch()->master() );
0 ignored issues
show
Bug introduced by
It seems like $this->app->api()->fetch()->master() targeting WordPress_GitHub_Sync_Fetch_Client::master() can also be of type object<WordPress_GitHub_Sync_Tree> or object<stdClass>; however, WordPress_GitHub_Sync_Import::commit() does only seem to accept object<WordPress_GitHub_...ommit>|object<WP_Error>, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
80
	}
81
82
	/**
83
	 * Imports a provided commit into the database.
84
	 *
85
	 * @param WordPress_GitHub_Sync_Commit|WP_Error $commit Commit to import.
86
	 *
87
	 * @return string|WP_Error
88
	 */
89 10
	protected function commit( $commit ) {
90 10
		if ( is_wp_error( $commit ) ) {
91 2
			return $commit;
92
		}
93
94 8
		if ( $commit->already_synced() ) {
95 1
			return new WP_Error( 'commit_synced', __( 'Already synced this commit.', 'wp-github-sync' ) );
96
		}
97
98 7
		$posts = array();
99 7
		$new   = array();
100
101 7
		foreach ( $commit->tree()->blobs() as $blob ) {
102 7
			if ( ! $this->importable_blob( $blob ) ) {
103
				continue;
104
			}
105
106 7
			$posts[] = $post = $this->blob_to_post( $blob );
107
108 7
			if ( $post->is_new() ) {
109 6
				$new[] = $post;
110 6
			}
111 7
		}
112
113 7
		$result = $this->app->database()->save_posts( $posts, $commit->author_email() );
114
115 7
		if ( is_wp_error( $result ) ) {
116
			return $result;
117
		}
118
119 7
		if ( $new ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $new of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
120 6
			$result = $this->app->export()->new_posts( $new );
121
122 6
			if ( is_wp_error( $result ) ) {
123
				return $result;
124
			}
125 6
		}
126
127 7
		return $posts;
128
	}
129
130
	/**
131
	 * Checks whether the provided blob should be imported.
132
	 *
133
	 * @param WordPress_GitHub_Sync_Blob $blob Blob to validate.
134
	 *
135
	 * @return bool
136
	 */
137 7
	protected function importable_blob( WordPress_GitHub_Sync_Blob $blob ) {
138 7
		global $wpdb;
139
140
		// Skip the repo's readme.
141 7
		if ( 'readme' === strtolower( substr( $blob->path(), 0, 6 ) ) ) {
142
			return false;
143
		}
144
145
		// If the blob sha already matches a post, then move on.
146 7
		if ( ! is_wp_error( $this->app->database()->fetch_by_sha( $blob->sha() ) ) ) {
147
			return false;
148
		}
149
150 7
		if ( ! $blob->has_frontmatter() ) {
151
			return false;
152
		}
153
154 7
		return true;
155
	}
156
157
	/**
158
	 * Imports a single blob content into matching post.
159
	 *
160
	 * @param WordPress_GitHub_Sync_Blob $blob Blob to transform into a Post.
161
	 *
162
	 * @return WordPress_GitHub_Sync_Post
163
	 */
164 7
	protected function blob_to_post( WordPress_GitHub_Sync_Blob $blob ) {
165 7
		$args = array( 'post_content' => $blob->content_import() );
166 7
		$meta = $blob->meta();
167
168 7
		if ( $meta ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $meta of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
169 5
			if ( array_key_exists( 'layout', $meta ) ) {
170 1
				$args['post_type'] = $meta['layout'];
171 1
				unset( $meta['layout'] );
172 1
			}
173
174 5
			if ( array_key_exists( 'published', $meta ) ) {
175 1
				$args['post_status'] = true === $meta['published'] ? 'publish' : 'draft';
176 1
				unset( $meta['published'] );
177 1
			}
178
179 5
			if ( array_key_exists( 'post_title', $meta ) ) {
180 1
				$args['post_title'] = $meta['post_title'];
181 1
				unset( $meta['post_title'] );
182 1
			}
183
184 5
			if ( array_key_exists( 'ID', $meta ) ) {
185 1
				$args['ID'] = $meta['ID'];
186 1
				unset( $meta['ID'] );
187 1
			}
188 5
189
			if ( array_key_exists( 'post_date', $meta ) ) {
190 7
191
				if ( empty( $meta['post_date'] ) ) {
192 7
					$meta['post_date'] = current_time( 'mysql' );
193 7
				}
194
195 7
				$args['post_date'] = $meta['post_date'];
196
197
				$args['post_date_gmt'] = get_gmt_from_date( $meta['post_date'] );
198
				unset( $meta['post_date'] );
199
			}
200
		}
201
202
		$meta['_sha'] = $blob->sha();
203
204
		$post = new WordPress_GitHub_Sync_Post( $args, $this->app->api() );
205
		$post->set_meta( $meta );
206
207
		return $post;
208
	}
209
}
210