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

PersistentList::get()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
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
}