-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathMissingBehaviorTrait.php
116 lines (102 loc) · 3.04 KB
/
MissingBehaviorTrait.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
<?php
namespace Drupal\dgi_migrate\Plugin\migrate\process;
use Drupal\migrate\MigrateException;
use Drupal\migrate\MigrateSkipProcessException;
use Drupal\migrate\MigrateSkipRowException;
/**
* Helper for when a specified migration property is missing.
*/
trait MissingBehaviorTrait {
/**
* The configured missing behavior.
*
* @var string
*/
protected string $missingBehavior;
/**
* The class to which $missingBehavior maps in ::getMissingClassMap().
*
* @var string
*/
protected string $missingClass;
/**
* A bit of initialization.
*
* Grab our config and lookup the exception to use.
*
* @throws \Drupal\migrate\MigrateException
*/
protected function missingBehaviorInit() : void {
$this->missingBehavior = $this->configuration[static::getMissingConfigKey()] ?? $this->getDefaultMissingBehavior();
// XXX: More just for validation, to check that the class exists.
$this->getMissingClass();
}
/**
* Get the default missing behavior.
*
* @return string
* One of the keys returned by ::getMissingClassMap().
*/
protected function getDefaultMissingBehavior() : string {
return 'abort';
}
/**
* Get the config key used.
*
* Ideally, would be a constant; however, traits do not support defining
* constants.
*
* @return string
* The key used to hold what missing behavior should be employed.
*/
final protected static function getMissingConfigKey() : string {
return 'missing_behavior';
}
/**
* Get the name of the exception class to use when the index is missing.
*
* @return string
* The name of the class to use.
*
* @throws \Drupal\migrate\MigrateException
* If the indicated behavior does not appear to be valid.
*/
protected function getMissingClass() : string {
if (!isset($this->missingClass)) {
if (!isset(static::getMissingClassMap()[$this->missingBehavior])) {
throw new MigrateException(strtr('Unrecognized "missing_behavior" :input; expecting one of: :valid', [
':input' => $this->missingBehavior,
':valid' => implode(', ', array_keys(static::getMissingClassMap())),
]));
}
$this->missingClass = static::getMissingClassMap()[$this->missingBehavior];
}
return $this->missingClass;
}
/**
* Instantiate and return the exception.
*
* @return \Exception
* The appropriate exception.
*
* @throws \Drupal\migrate\MigrateException
* Propagating from ::getMissingClasse().
*/
protected function getMissingException($message) : \Exception {
$class = $this->getMissingClass();
return new $class($message);
}
/**
* Map tokens to be used in config to classes to instantiate.
*
* @return array
* An associative array mapping machine names to exception class names.
*/
protected static function getMissingClassMap() : array {
return [
'abort' => MigrateException::class,
'skip_process' => MigrateSkipProcessException::class,
'skip_row' => MigrateSkipRowException::class,
];
}
}