Completed
Push — try/code-signature-diff ( b0566b...70fa0d )
by
unknown
23:17 queued 09:18
created

PersistentList::output()   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
use Automattic\Jetpack\Analyzer\PersistentList\Item as PersistentListItem;
6
7
/**
8
 * Handy class for persisting a list of objects that support the to_csv_array method
9
 */
10
class PersistentList {
11
	private $items;
12
13
	function __construct() {
14
		$this->items = array();
15
	}
16
17
	public function get() {
18
		return $this->items;
19
	}
20
21
	public function add( $item ) {
22
		if ( ! is_subclass_of( $item, 'Automattic\Jetpack\Analyzer\PersistentList\Item' ) ) {
23
			throw new \Exception( 'item must extend Automattic\Jetpack\Analyzer\PersistentList\Item' );
24
		}
25
		$this->items[] = $item;
26
	}
27
28
	public function output() {
29
		echo $this->save( 'php://memory' );
30
	}
31
32
	/**
33
	 * Saves the items to a file and returns the file contents
34
	 */
35
	public function save( $file_path ) {
36
		$handle = fopen( $file_path, 'w+' );
37
		foreach ( $this->items as $item ) {
38
			fputcsv( $handle, $item->to_csv_array() );
39
		}
40
		rewind( $handle );
41
		$contents = stream_get_contents( $handle );
42
		fclose( $handle );
43
		return $contents;
44
	}
45
}
46