Completed
Pull Request — master (#111)
by Luca
02:07
created

AttachmentPathFinder   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 81
rs 10
c 0
b 0
f 0
wmc 16
lcom 1
cbo 1

4 Methods

Rating   Name   Duplication   Size   Complexity  
A filter_attachment_url() 0 13 3
B filter_srcset() 0 23 5
B has_import_data() 0 20 5
A get_source_post() 0 11 3
1
<?php
2
3
namespace lloc\Msls\ContentImport;
4
5
use lloc\Msls\MslsRegistryInstance;
6
7
class AttachmentPathFinder extends MslsRegistryInstance {
8
9
	const LINKED = '_msls_linked';
10
11
	public function filter_srcset( array $sources, $sizeArray, $imageSrc, $imageMeta, $attachmentId ) {
0 ignored issues
show
Unused Code introduced by
The parameter $sizeArray is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $imageMeta is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
12
		if ( ! $msls_imported = $this->has_import_data( $attachmentId ) ) {
13
			return $sources;
14
		}
15
16
		$source_post = get_blog_post( $msls_imported['blog'], $msls_imported['post'] );
17
18
		if ( false === $source_post ) {
19
			return $sources;
20
		}
21
22
		$extension           = '.' . pathinfo( $source_post->guid, PATHINFO_EXTENSION );
23
		$pattern             = '/(-[\\d]+x[\\d]+)*' . preg_quote( $extension, '/' ) . '$/';
24
		$srcWithoutExtension = preg_replace( $pattern, '', $imageSrc );
25
26
		foreach ( $sources as $key => &$value ) {
27
			preg_match( $pattern, $value['url'], $matches );
28
			$w_and_h      = ! empty( $matches[1] ) ? $matches[1] : '';
29
			$value['url'] = $srcWithoutExtension . $w_and_h . $extension;
30
		}
31
32
		return $sources;
33
	}
34
35
	protected function has_import_data( $attachment_id ) {
36
		if ( empty( $attachment_id ) ) {
37
			return false;
38
		}
39
40
		$msls_imported = get_post_meta( $attachment_id, self::LINKED, true );
41
42
		if ( ! (
43
			is_array( $msls_imported )
44
			&& array_key_exists( 'blog', $msls_imported )
45
			&& array_key_exists( 'post', $msls_imported )
46
		)
47
		) {
48
			delete_post_meta( $attachment_id, self::LINKED );
49
50
			return false;
51
		}
52
53
		return $msls_imported;
54
	}
55
56
	public function filter_attachment_url( $url, $attachment_id ) {
57
		if ( ! $msls_imported = $this->has_import_data( $attachment_id ) ) {
58
			return $url;
59
		}
60
61
		$source_post = $this->get_source_post( $attachment_id, $msls_imported );
62
63
		if ( false === $source_post ) {
64
			return $url;
65
		}
66
67
		return $source_post->guid;
68
	}
69
70
	/**
71
	 * @param int $attachment_id
72
	 * @param array $msls_imported
73
	 *
74
	 * @return \WP_Post|false
75
	 */
76
	protected function get_source_post( $attachment_id, $msls_imported ) {
77
		$source_post = get_blog_post( $msls_imported['blog'], $msls_imported['post'] );
78
79
		if ( empty( $source_post ) || ! $source_post instanceof \WP_Post ) {
0 ignored issues
show
Bug introduced by
The class WP_Post does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
80
			delete_post_meta( $attachment_id, self::LINKED );
81
82
			return false;
83
		}
84
85
		return $source_post;
86
	}
87
}