Test Failed
Push — master ( 87025b...f461b3 )
by Jean-Christophe
13:09
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 4
	protected static function getBulk($class, $operation = 'update') {
17 4
		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 4
		return self::$bulks [$operation] [$class];
22
	}
23
24 4
	protected static function toOperation($instance, string $operation): void {
25 4
		$class = \get_class ( $instance );
26 4
		self::getBulk ( $class, $operation )->addInstance ( $instance );
27 4
	}
28
29 3
	protected static function toOperations(array $instances, string $operation): void {
30 3
		$instance = \current ( $instances );
31 3
		if (isset ( $instance )) {
32 3
			$class = \get_class ( $instance );
33 3
			self::getBulk ( $class, $operation )->addInstances ( $instances );
34
		}
35 3
	}
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 3
	public static function toUpdate($instance): void {
74 3
		self::toOperation ( $instance, 'update' );
75 3
	}
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 2
	public static function toUpdates(array $instances): void {
84 2
		self::toOperations ( $instances, 'update' );
85 2
	}
86
87 1
	public static function updateGroups($count = 5) {
88 1
		$bulks = self::$bulks ['update'];
89 1
		foreach ( $bulks as $bulk ) {
90 1
			$bulk->groupOp ( $count );
91
		}
92 1
	}
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
	public static function deleteGroups($count = 5) {
102
		$bulks = self::$bulks ['delete'];
103
		foreach ( $bulks as $bulk ) {
104 1
			$bulk->groupOp ( $count );
105 1
		}
106 1
	}
107 1
108
	/**
109 1
	 * Executes all waiting update operations
110
	 */
111
	public static function flushUpdates(): void {
112
		$bulks = self::$bulks ['update'];
113
		foreach ( $bulks as $bulk ) {
114
			$bulk->flush ();
115
		}
116
	}
117 1
118 1
	/**
119 1
	 * Adds an instance in the bulk list of objects to delete.
120
	 * Call flush to commit the operation
121
	 *
122
	 * @param object $instance
123
	 */
124
	public static function toDelete($instance): void {
125
		self::toOperation ( $instance, 'delete' );
126
	}
127 1
128 1
	/**
129 1
	 * Adds an array of instances in the bulk list of objects to delete.
130
	 * Call flush to commit the operation
131
	 *
132
	 * @param array $instances
133
	 */
134 1
	public static function toDeletes(array $instances): void {
135 1
		self::toOperations ( $instances, 'delete' );
136 1
	}
137 1
138
	/**
139 1
	 * Executes all waiting delete operations
140
	 */
141
	public static function flushDeletes(): void {
142
		$bulks = self::$bulks ['delete'];
143
		foreach ( $bulks as $bulk ) {
144 1
			$bulk->flush ();
145 1
		}
146 1
	}
147 1
148
	/**
149
	 * Executes all waiting operations (inserts, updates, deletes)
150 1
	 */
151
	public static function flush(): void {
152
		foreach ( self::$bulks as $bulks ) {
153
			foreach ( $bulks as $bulk ) {
154
				$bulk->flush ();
155
			}
156
		}
157
	}
158
}
159
160