Skip to content

Commit f0d3240

Browse files
authored
Add a helper for posting an event when something is copied (#23664)
1 parent bbb95a9 commit f0d3240

File tree

1 file changed

+54
-2
lines changed

1 file changed

+54
-2
lines changed

plugins/CoreHome/EntityDuplicator/DuplicateRequestResponse.php

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@
1111

1212
namespace Piwik\Plugins\CoreHome\EntityDuplicator;
1313

14+
use Piwik\Piwik;
15+
1416
/**
15-
*
17+
* The object for building a consistent response to the duplication of an entity.
1618
*/
1719
class DuplicateRequestResponse
1820
{
@@ -36,6 +38,15 @@ class DuplicateRequestResponse
3638
*/
3739
protected $additionalData;
3840

41+
/**
42+
* @var array Optional array containing the data required for the event to be posted on success. If set, the event
43+
* will be triggered when the getResponseArray method is called.
44+
*
45+
* @see self::setRequestDataForEvent()
46+
* @see self::getResponseArray()
47+
*/
48+
protected $eventDataToPost;
49+
3950
/**
4051
* Get an instance of the object and store it's initial state for comparison later
4152
*/
@@ -135,6 +146,11 @@ public function getResponseArray(): array
135146
throw new \Exception('No duplicate request response properties were set.');
136147
}
137148

149+
// If the flag is set to post the event and the request was successful, post the event for the duplication
150+
if ($this->success && $this->eventDataToPost !== null) {
151+
Piwik::postEvent('EntityDuplicator.DuplicationSuccessful', $this->eventDataToPost);
152+
}
153+
138154
return $responseArray;
139155
}
140156

@@ -145,9 +161,45 @@ private function getCurrentState(): array
145161
{
146162
// Get an array of all the property values
147163
$state = get_object_vars($this);
148-
// Exclude the state property
164+
// Exclude the state property and eventDataToPost
149165
unset($state['initialState']);
166+
unset($state['eventDataToPost']);
150167

151168
return $state;
152169
}
170+
171+
/**
172+
* Set the arguments to be used while posting the event when the response array is built. This is used by plugins
173+
* which use this class while generating the response to a duplication request.
174+
*
175+
* @param string $entityTypeTranslation Translation key for the name of the type of entity. E.g. Goals_Goal,
176+
* Heatmaps_Heatmap, etc.
177+
* @param string $entityName The name of the entity being copied. E.g. 'Goal that does thing' or'Home page heatmap'.
178+
* @param int|null $idEntity The ID of the entity being copied. E.g. 2 or 900. It's optional since some entities
179+
* might only have a string identifier which should be provided as the entityName.
180+
* @param int|null $idSite ID of the source site. It's optional in case the entity being copied is not site scoped,
181+
* like a system-wide configuration.
182+
* @param array|null $idDestinationSites IDs of the destination sites. This is optional for the same reason as
183+
* idSite but also since it doesn't need to be provided if the only destination site is the source site (idSite).
184+
* @param array|null $additionalData Optional array of additional data relating to the entity being copied.
185+
*
186+
* @return void
187+
*/
188+
public function setRequestDataForEvent(
189+
string $entityTypeTranslation,
190+
string $entityName,
191+
?int $idEntity = null,
192+
?int $idSite = null,
193+
?array $idDestinationSites = null,
194+
?array $additionalData = null
195+
): void {
196+
$this->eventDataToPost = [
197+
$entityTypeTranslation,
198+
$entityName,
199+
$idEntity,
200+
$idSite,
201+
$idDestinationSites,
202+
$additionalData,
203+
];
204+
}
153205
}

0 commit comments

Comments
 (0)