Completed
Push — master ( 0c53d4...debbec )
by David
09:08 queued 10s
created

Wordlift_Reference_Rebuild_Service   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 118
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
dl 0
loc 118
rs 10
c 0
b 0
f 0
wmc 8
lcom 1
cbo 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
B rebuild() 0 56 6
A find() 0 14 1
1
<?php
2
3
class Wordlift_Reference_Rebuild_Service extends Wordlift_Rebuild_Service {
4
5
	/**
6
	 * @since  3.18.0
7
	 * @access private
8
	 * @var \Wordlift_Linked_Data_Service $log A {@link Wordlift_Linked_Data_Service} instance.
9
	 */
10
	private $linked_data_service;
11
12
	/**
13
	 * The {@link Wordlift_Entity_Service} instance.
14
	 *
15
	 * @since  3.18.0
16
	 * @access private
17
	 * @var \Wordlift_Entity_Service $entity_service The {@link Wordlift_Entity_Service} instance.
18
	 */
19
	private $entity_service;
20
21
	/**
22
	 * A {@link Wordlift_Log_Service} instance.
23
	 *
24
	 * @since  3.18.0
25
	 * @access private
26
	 * @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance.
27
	 */
28
	private $log;
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
29
30
31
	/**
32
	 * Wordlift_Reference_Rebuild_Service constructor.
33
	 *
34
	 * @param \Wordlift_Entity_Service       $entity_service       The {@link Wordlift_Entity_Service} instance.
35
	 * @param \Wordlift_Linked_Data_Service  $linked_data_service  The {@link Wordlift_Linked_Data_Service} instance.
36
	 */
37
	public function __construct( $linked_data_service, $entity_service, $relation_service ) {
0 ignored issues
show
Unused Code introduced by
The parameter $relation_service 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...
38
39
		$this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Reference_Rebuild_Service' );
40
41
		$this->linked_data_service = $linked_data_service;
42
		$this->entity_service      = $entity_service;
43
	}
44
45
	public function rebuild() {
0 ignored issues
show
Coding Style introduced by
rebuild uses the super-global variable $_GET which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

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

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
46
		set_time_limit( 21600 ); // 6 hours
47
48
		// Send textual output.
49
		header( 'Content-type: text/plain; charset=utf-8' );
50
51
		// We start at 0 by default and get to max.
52
		$offset = $_GET['offset'] ?: 0;
53
		$limit  = $_GET['limit'] ?: 1;
54
		$max    = $offset + $limit;
55
56
		$this->log->debug( 'Processing references...' );
57
58
		// Go through the list of published entities and posts and call the (legacy)
59
		// `wl_linked_data_save_post` function for each one. We're using the `process`
60
		// function which is provided by the parent `Wordlift_Listable` abstract class
61
		// and will cycle through all the posts w/ a very small memory footprint
62
		// in order to avoid memory errors.
63
64
		$count               = 0;
65
		$log                 = $this->log;
66
		$entity_service      = $this->entity_service;
67
		$linked_data_service = $this->linked_data_service;
68
69
		$this->process(
70
			function ( $post_id ) use ( &$count, $log, $entity_service, $linked_data_service ) {
71
				$count ++;
72
73
				if ( $entity_service->is_entity( $post_id ) ) {
74
					$log->trace( "Post $post_id is an entity, skipping..." );
75
					return;
76
				}
77
78
				$log->trace( "Going to save post $count, ID $post_id..." );
79
				$linked_data_service->push( $post_id );
80
			},
81
			array(),
82
			$offset,
83
			$max
84
		);
85
86
		// Redirect to the next chunk.
87
		if ( $count == $limit ) {
88
			$log->trace( 'Redirecting to post #' . ( $offset + 1 ) . '...' );
89
			$url = admin_url( 'admin-ajax.php?action=wl_rebuild_references&offset=' . ( $offset + $limit ) . '&limit=' . $limit );
90
			$this->redirect( $url );
91
		}
92
93
		$this->log->info( "Rebuild complete" );
94
		echo( "Rebuild complete" );
95
96
		// If we're being called as AJAX, die here.
97
		if ( DOING_AJAX ) {
98
			wp_die();
99
		}
100
	}
101
102
	/**
103
	 * @inheritdoc
104
	 */
105
	function find( $offset = 0, $limit = 10, $args = array() ) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
106
		global $wpdb;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
107
108
		return $wpdb->get_col( $wpdb->prepare(
109
			"
110
			SELECT DISTINCT subject_id AS id
111
			FROM {$wpdb->prefix}wl_relation_instances
112
			LIMIT %d OFFSET %d
113
			",
114
			$limit,
115
			$offset
116
		) );
117
118
	}
119
120
}
121