File: DuplicateRequestResponse.php

package info (click to toggle)
matomo 5.8.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 95,068 kB
  • sloc: php: 289,425; xml: 127,249; javascript: 112,130; python: 202; sh: 178; makefile: 20; sql: 10
file content (186 lines) | stat: -rw-r--r-- 5,553 bytes parent folder | download
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
<?php

/**
 * Matomo - free/libre analytics platform
 *
 * @link    https://matomo.org
 * @license https://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
 */

declare(strict_types=1);

namespace Piwik\Plugins\CoreHome\EntityDuplicator;

use Piwik\Piwik;

/**
 * The object for building a consistent response to the duplication of an entity.
 */
class DuplicateRequestResponse
{
    /**
     * @var array
     */
    private $initialState;

    /**
     * @var bool|null
     */
    protected $success;

    /**
     * @var string|null
     */
    protected $message;

    /**
     * @var array|null
     */
    protected $additionalData;

    /**
     * @var array Optional array containing the data required for the event to be posted on success. If set, the event
     *   will be triggered when the getResponseArray method is called.
     *
     * @see self::setRequestDataForEvent()
     * @see self::getResponseArray()
     */
    protected $eventDataToPost;

    /**
     * Get an instance of the object and store it's initial state for comparison later
     */
    public function __construct()
    {
        // Save the initial state of the object
        $this->initialState = $this->getCurrentState();
    }

    public function isSuccess(): bool
    {
        return $this->success ?? false;
    }

    public function setSuccess(bool $success): void
    {
        $this->success = $success;
    }

    public function getMessage(): string
    {
        return $this->message ?? '';
    }

    public function setMessage(string $message): void
    {
        $this->message = $message;
    }

    /**
     * @return array
     */
    public function getAdditionalData(): array
    {
        return $this->additionalData ?? [];
    }

    /**
     * @param array $additionalData
     */
    public function setAdditionalData(array $additionalData): void
    {
        $this->additionalData = $additionalData;
    }

    public function hasResponseBeenModified(): bool
    {
        return $this->initialState !== $this->getCurrentState();
    }

    /**
     * Checks which property values have changed from the initial state and only includes them in the JSON string.
     *
     * @return string JSON of the response object
     * @throws \Exception If none of the properties have been set
     */
    public function getJsonResponse(): string
    {
        return json_encode($this->getResponseArray());
    }

    /**
     * Checks which property values have changed from the initial state and only includes them in the array.
     *
     * @return array response object properties
     * @throws \Exception If none of the properties have been set
     */
    public function getResponseArray(): array
    {
        $responseArray = [];
        $currentState = $this->getCurrentState();
        foreach ($this->initialState as $propertyName => $value) {
            if ($currentState[$propertyName] !== $value) {
                $responseArray[$propertyName] = $currentState[$propertyName];
            }
        }

        if (count($responseArray) === 0) {
            throw new \Exception('No duplicate request response properties were set.');
        }

        // If the flag is set to post the event and the request was successful, post the event for the duplication
        if ($this->success && $this->eventDataToPost !== null) {
            Piwik::postEvent('EntityDuplicator.DuplicationSuccessful', $this->eventDataToPost);
        }

        return $responseArray;
    }

    /**
     * @return array
     */
    private function getCurrentState(): array
    {
        // Get an array of all the property values
        $state = get_object_vars($this);
        // Exclude the state property and eventDataToPost
        unset($state['initialState']);
        unset($state['eventDataToPost']);

        return $state;
    }

    /**
     * Set the arguments to be used while posting the event when the response array is built. This is used by plugins
     *   which use this class while generating the response to a duplication request.
     *
     * @param string $entityTypeTranslation Translation key for the name of the type of entity. E.g. Goals_Goal,
     *   Heatmaps_Heatmap, etc.
     * @param string $entityName The name of the entity being copied. E.g. 'Goal that does thing' or'Home page heatmap'.
     * @param int|null $idEntity The ID of the entity being copied. E.g. 2 or 900. It's optional since some entities
     *   might only have a string identifier which should be provided as the entityName.
     * @param int|null $idSite ID of the source site. It's optional in case the entity being copied is not site scoped,
     *   like a system-wide configuration.
     * @param array|null $idDestinationSites IDs of the destination sites. This is optional for the same reason as
     *   idSite but also since it doesn't need to be provided if the only destination site is the source site (idSite).
     * @param array|null $additionalData Optional array of additional data relating to the entity being copied.
     *
     */
    public function setRequestDataForEvent(
        string $entityTypeTranslation,
        string $entityName,
        ?int $idEntity = null,
        ?int $idSite = null,
        ?array $idDestinationSites = null,
        ?array $additionalData = null
    ): void {
        $this->eventDataToPost = [
            $entityTypeTranslation,
            $entityName,
            $idEntity,
            $idSite,
            $idDestinationSites,
            $additionalData,
        ];
    }
}