Passed
Push — master ( c52784...833dd8 )
by Aimeos
28:15 queued 16:05
created

CustomerRemoveLostUserDataFosuser::up()   A

Complexity

Conditions 6
Paths 5

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 9
nc 5
nop 0
dl 0
loc 18
rs 9.2222
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2018-2023
6
 */
7
8
9
 namespace Aimeos\Upscheme\Task;
10
11
12
/**
13
 * Removes address and list records without user entry
14
 */
15
class CustomerRemoveLostUserDataFosuser extends Base
16
{
17
	private $sql = [
18
		'fos_user_address' => [
19
			'fk_fosusad_pid' => 'DELETE FROM "fos_user_address" WHERE NOT EXISTS ( SELECT "id" FROM "fos_user" AS u WHERE "parentid"=u."id" )'
20
		],
21
		'fos_user_list' => [
22
			'fk_fosusli_pid' => 'DELETE FROM "fos_user_list" WHERE NOT EXISTS ( SELECT "id" FROM "fos_user" AS u WHERE "parentid"=u."id" )'
23
		],
24
		'fos_user_property' => [
25
			'fk_fosuspr_pid' => 'DELETE FROM "fos_user_property" WHERE NOT EXISTS ( SELECT "id" FROM "fos_user" AS u WHERE "parentid"=u."id" )'
26
		],
27
	];
28
29
30
	/**
31
	 * Returns the list of task names which depends on this task.
32
	 *
33
	 * @return string[] List of task names
34
	 */
35
	public function before() : array
36
	{
37
		return ['Customer'];
38
	}
39
40
41
	/**
42
	 * Migrate database schema
43
	 */
44
	public function up()
45
	{
46
		$db = $this->db( 'db-customer' );
47
48
		if( !$db->hasTable( 'fos_user' ) ) {
49
			return;
50
		}
51
52
		$this->info( 'Remove left over FosUser references', 'vv' );
53
54
		foreach( $this->sql as $table => $map )
55
		{
56
			foreach( $map as $constraint => $sql )
57
			{
58
				if( $db->hasTable( $table ) && !$db->hasForeign( $table, $constraint ) )
59
				{
60
					$this->info( sprintf( 'Remove records from %1$s', $table ), 'vv', 1 );
61
					$db->exec( $sql );
62
				}
63
			}
64
		}
65
	}
66
}
67