-
Notifications
You must be signed in to change notification settings - Fork 1
/
RemoteEntityBase.php
160 lines (135 loc) · 3.91 KB
/
RemoteEntityBase.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
<?php
declare(strict_types=1);
namespace Drupal\helfi_api_base\Entity;
use Drupal\Core\Entity\ContentEntityBase;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\StringTranslation\TranslatableMarkup;
/**
* Base class for remote entities.
*/
abstract class RemoteEntityBase extends ContentEntityBase {
/**
* The maximum sync attempts.
*
* This determines how many times we attempt to sync the
* given entity before deleting it.
*
* @see \Drupal\helfi_api_base\EventSubscriber\MigrationSubscriber::onPostImport().
*
* @var int
*/
public const MAX_SYNC_ATTEMPTS = 2;
/**
* Whether to reset sync attempts.
*
* @var bool
*/
protected bool $resetSyncAttempts = TRUE;
/**
* Gets the migration name.
*
* @return string|null
* The migration name.
*/
public static function getMigration() : ? string {
return NULL;
}
/**
* {@inheritdoc}
*/
public function preSave(EntityStorageInterface $storage) {
parent::preSave($storage);
// Make sure entity id is set manually before saving.
if (!$this->id()) {
throw new \InvalidArgumentException('ID must be set before saving the entity.');
}
}
/**
* {@inheritdoc}
*/
public function save() {
if ($this->resetSyncAttempts) {
$this->resetSyncAttempts();
}
return parent::save();
}
/**
* {@inheritdoc}
*/
public function delete(bool $forceDelete = FALSE) : void {
$hasDeleteForm = $this->hasLinkTemplate('delete-form');
// Disable deleting entities to prevent accidental automatic deletions if
// entity type does not define delete form.
if (!$hasDeleteForm && !$forceDelete) {
\Drupal::logger('helfi_api_base')
->notice('Prevented deleting entity @type with ID @id. Deleting Remote entities without "delete-form" is disabled. This can be overridden by calling ::delete() with forceDelete = TRUE.',
[
'@id' => $this->id(),
'@type' => $this->getEntityTypeId(),
]);
return;
}
parent::delete();
}
/**
* Increments sync attempts counter.
*
* @param int $increment
* Amount to increment.
*
* @return $this
* The self.
*/
public function incrementSyncAttempts(int $increment = 1) : self {
// Never reset sync attempts on save if we increment sync attempts.
$this->resetSyncAttempts = FALSE;
$this->set('sync_attempts', $this->getSyncAttempts() + $increment);
return $this;
}
/**
* Resets the sync attempts counter.
*
* @return $this
* The self.
*/
public function resetSyncAttempts() : self {
$this->set('sync_attempts', 0);
return $this;
}
/**
* Gets the sync attempts counter.
*
* @return int
* The sync attempts.
*/
public function getSyncAttempts() : int {
return (int) $this->get('sync_attempts')->value ?? 0;
}
/**
* {@inheritdoc}
*/
public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
$fields = parent::baseFieldDefinitions($entity_type);
// We use external id as entity id.
$fields[$entity_type->getKey('id')] = BaseFieldDefinition::create('string')
->setLabel(new TranslatableMarkup('ID'))
->setSettings([
'is_ascii' => TRUE,
])
->setReadOnly(TRUE);
$fields['sync_attempts'] = BaseFieldDefinition::create('integer')
->setLabel(new TranslatableMarkup('Sync attempts'))
->setReadOnly(TRUE);
$fields['created'] = BaseFieldDefinition::create('created')
->setLabel(new TranslatableMarkup('Authored on'))
->setTranslatable(TRUE)
->setDisplayConfigurable('form', TRUE)
->setDisplayConfigurable('view', TRUE);
$fields['changed'] = BaseFieldDefinition::create('changed')
->setLabel(new TranslatableMarkup('Changed'))
->setTranslatable(TRUE);
return $fields;
}
}