Completed
Push — try/code-signature-diff ( 9196c0...4ac422 )
by
unknown
183:18 queued 174:43
created

PersistentList   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
dl 0
loc 33
rs 10
c 0
b 0
f 0
wmc 6
lcom 1
cbo 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A get() 0 3 1
A add() 0 3 1
A print() 0 3 1
A save() 0 10 2
1
<?php
2
3
namespace Automattic\Jetpack\Analyzer;
4
5
/**
6
 * Handy class for persisting a list of objects that support the to_csv_array method
7
 */
8
class PersistentList {
9
	private $items;
10
11
	function __construct() {
12
		$this->items = array();
13
	}
14
15
	public function get() {
16
		return $this->items;
17
	}
18
19
	public function add( $item ) {
20
		$this->items[] = $item;
21
	}
22
23
	public function print() {
24
		echo $this->save( 'php://memory' );
25
	}
26
27
	/**
28
	 * Saves the items to a file and returns the file contents
29
	 */
30
	public function save( $file_path ) {
31
		$handle = fopen( $file_path, 'r+');
32
		foreach ( $this->items as $item ) {
33
			fputcsv( $handle, $item->to_csv_array() );
34
		}
35
		rewind( $handle );
36
		$contents = stream_get_contents( $handle );
37
		fclose( $handle );
38
		return $contents;
39
	}
40
}