Passed
Push — master ( c0a3a7...3b84a4 )
by Jeroen
58:51
created

engine/classes/Elgg/Database/Seeder.php (1 issue)

1
<?php
2
3
namespace Elgg\Database;
4
5
use Elgg\Database\Seeds\Seed;
6
use Elgg\PluginHooksService;
7
8
/**
9
 * Seeder class
10
 *
11
 * Populates the database with rows for testing
12
 *
13
 * @access private
14
 */
15
class Seeder {
16
17
	/**
18
	 * @var PluginHooksService
19
	 */
20
	protected $hooks;
21
22
	/**
23
	 * Seeder constructor.
24
	 *
25
	 * @param PluginHooksService $hooks Hooks registration service
26
	 */
27 1
	public function __construct(PluginHooksService $hooks) {
28 1
		$this->hooks = $hooks;
29 1
	}
30
31
	/**
32
	 * Load seed scripts
33
	 *
34
	 * @param int $limit the max number of entities to seed
35
	 *
36
	 * @return void
37
	 */
38
	public function seed($limit = null) {
39
		if (!$limit) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $limit of type null|integer is loosely compared to false; this is ambiguous if the integer can be 0. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
40
			$limit = max(elgg_get_config('default_limit'), 20);
41
		}
42
43
		$ia = elgg_set_ignore_access(true);
44
45
		$ha = access_get_show_hidden_status();
46
		access_show_hidden_entities(true);
47
48
		$seeds = $this->hooks->trigger('seeds', 'database', null, []);
49
50
		foreach ($seeds as $seed) {
51
			if (!class_exists($seed)) {
52
				elgg_log("Seeding class $seed not found", 'ERROR');
53
				continue;
54
			}
55
			if (!is_subclass_of($seed, Seed::class)) {
56
				elgg_log("Seeding class $seed does not extend " . Seed::class, 'ERROR');
57
				continue;
58
			}
59
			$seeder = new $seed($limit);
60
			elgg_log('Starting seeding with ' . get_class($seeder));
61
			$seeder->seed();
62
			elgg_log('Finished seeding with ' . get_class($seeder));
63
		}
64
65
		access_show_hidden_entities($ha);
66
		elgg_set_ignore_access($ia);
67
	}
68
69
	/**
70
	 * Remove all seeded entities
71
	 * @return void
72
	 */
73
	public function unseed() {
74
75
		$ia = elgg_set_ignore_access(true);
76
77
		$ha = access_get_show_hidden_status();
78
		access_show_hidden_entities(true);
79
80
		$seeds = $this->hooks->trigger('seeds', 'database', null, []);
81
82
		foreach ($seeds as $seed) {
83
			if (!class_exists($seed)) {
84
				elgg_log("Seeding class $seed not found", 'ERROR');
85
				continue;
86
			}
87
			if (!is_subclass_of($seed, Seed::class)) {
88
				elgg_log("Seeding class $seed does not extend " . Seed::class, 'ERROR');
89
				continue;
90
			}
91
			$seeder = new $seed();
92
			elgg_log('Starting unseeding with ' . get_class($seeder));
93
			$seeder->unseed();
94
			elgg_log('Finished unseeding with ' . get_class($seeder));
95
		}
96
97
		access_show_hidden_entities($ha);
98
		elgg_set_ignore_access($ia);
99
	}
100
}