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

PersistentList   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
dl 0
loc 36
rs 10
c 0
b 0
f 0
wmc 7
lcom 1
cbo 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A get() 0 3 1
A add() 0 6 2
A output() 0 3 1
A save() 0 10 2
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