Passed
Push — master ( a6c179...989fd8 )
by Jean-Christophe
10:02
created

DAOBulkUpdatesTrait::toUpdate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 1
c 2
b 0
f 0
dl 0
loc 2
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Ubiquity\orm\traits;
4
5
/**
6
 * Ubiquity\orm\traits$DAOBulkUpdatesTrait
7
 * This class is part of Ubiquity
8
 *
9
 * @author jcheron <[email protected]>
10
 * @version 1.0.3
11
 *
12
 */
13
trait DAOBulkUpdatesTrait {
14
	protected static $bulks = [ 'insert' => [ ],'update' => [ ],'delete' => [ ] ];
15
16 3
	protected static function getBulk($class, $operation = 'update') {
17 3
		if (! isset ( self::$bulks [$operation] [$class] )) {
18 2
			$bulkClass = '\\Ubiquity\\orm\\bulk\\Bulk' . \ucfirst ( $operation ) . 's';
19 2
			self::$bulks [$operation] [$class] = new $bulkClass ( $class );
20
		}
21 3
		return self::$bulks [$operation] [$class];
22
	}
23
24 3
	protected static function toOperation($instance, string $operation): void {
25 3
		$class = \get_class ( $instance );
26 3
		self::getBulk ( $class, $operation )->addInstance ( $instance );
27 3
	}
28
29 2
	protected static function toOperations(array $instances, string $operation): void {
30 2
		$instance = \current ( $instances );
31 2
		if (isset ( $instance )) {
32 2
			$class = \get_class ( $instance );
33 2
			self::getBulk ( $class, $operation )->addInstances ( $instances );
34
		}
35 2
	}
36
37
	/**
38
	 * Adds an instance in the bulk list of objects to insert.
39
	 * Call flush to commit the operation
40
	 *
41
	 * @param object $instance
42
	 */
43 2
	public static function toInsert($instance): void {
44 2
		self::toOperation ( $instance, 'insert' );
45 2
	}
46
47
	/**
48
	 * Adds an array of instances in the bulk list of objects to insert.
49
	 * Call flush to commit the operation
50
	 *
51
	 * @param array $instances
52
	 */
53 1
	public static function toInserts(array $instances): void {
54 1
		self::toOperations ( $instances, 'insert' );
55 1
	}
56
57
	/**
58
	 * Executes all waiting insert operations
59
	 */
60 1
	public static function flushInserts(): void {
61 1
		$bulks = self::$bulks ['insert'];
62 1
		foreach ( $bulks as $bulk ) {
63 1
			$bulk->flush ();
64
		}
65 1
	}
66
67
	/**
68
	 * Adds an instance in the bulk list of objects to update.
69
	 * Call flush to commit the operation
70
	 *
71
	 * @param object $instance
72
	 */
73 2
	public static function toUpdate($instance): void {
74 2
		self::toOperation ( $instance, 'update' );
75 2
	}
76
77
	/**
78
	 * Adds an array of instances in the bulk list of objects to update.
79
	 * Call flush to commit the operation
80
	 *
81
	 * @param array $instances
82
	 */
83 1
	public static function toUpdates(array $instances): void {
84 1
		self::toOperations ( $instances, 'update' );
85 1
	}
86
87
	public static function updateGroups($count = 5) {
88
		$bulks = self::$bulks ['update'];
89
		foreach ( $bulks as $bulk ) {
90
			$bulk->groupOp ( $count );
91
		}
92
	}
93
94
	public static function insertGroups($count = 5) {
95
		$bulks = self::$bulks ['insert'];
96
		foreach ( $bulks as $bulk ) {
97
			$bulk->groupOp ( $count );
98
		}
99
	}
100
101
	/**
102
	 * Executes all waiting update operations
103
	 */
104 1
	public static function flushUpdates(): void {
105 1
		$bulks = self::$bulks ['update'];
106 1
		foreach ( $bulks as $bulk ) {
107 1
			$bulk->flush ();
108
		}
109 1
	}
110
111
	/**
112
	 * Adds an instance in the bulk list of objects to delete.
113
	 * Call flush to commit the operation
114
	 *
115
	 * @param object $instance
116
	 */
117 1
	public static function toDelete($instance): void {
118 1
		self::toOperation ( $instance, 'delete' );
119 1
	}
120
121
	/**
122
	 * Adds an array of instances in the bulk list of objects to delete.
123
	 * Call flush to commit the operation
124
	 *
125
	 * @param array $instances
126
	 */
127 1
	public static function toDeletes(array $instances): void {
128 1
		self::toOperations ( $instances, 'delete' );
129 1
	}
130
131
	/**
132
	 * Executes all waiting delete operations
133
	 */
134 1
	public static function flushDeletes(): void {
135 1
		$bulks = self::$bulks ['delete'];
136 1
		foreach ( $bulks as $bulk ) {
137 1
			$bulk->flush ();
138
		}
139 1
	}
140
141
	/**
142
	 * Executes all waiting operations (inserts, updates, deletes)
143
	 */
144 1
	public static function flush(): void {
145 1
		foreach ( self::$bulks as $bulks ) {
146 1
			foreach ( $bulks as $bulk ) {
147 1
				$bulk->flush ();
148
			}
149
		}
150 1
	}
151
}
152
153