Issues (3627)

CampaignBundle/Event/CampaignBuilderEvent.php (1 issue)

1
<?php
2
3
/*
4
 * @copyright   2014 Mautic Contributors. All rights reserved
5
 * @author      Mautic
6
 *
7
 * @link        http://mautic.org
8
 *
9
 * @license     GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
10
 */
11
12
namespace Mautic\CampaignBundle\Event;
13
14
use Mautic\CampaignBundle\Event\Exception\KeyAlreadyRegisteredException;
15
use Mautic\CoreBundle\Event\ComponentValidationTrait;
16
use Symfony\Component\EventDispatcher\Event;
17
use Symfony\Component\Translation\TranslatorInterface;
18
19
class CampaignBuilderEvent extends Event
20
{
21
    use ComponentValidationTrait;
22
23
    /**
24
     * @var array
25
     */
26
    private $decisions = [];
27
28
    /**
29
     * @var array
30
     */
31
    private $conditions = [];
32
33
    /**
34
     * @var array
35
     */
36
    private $actions = [];
37
38
    /**
39
     * @var \Symfony\Bundle\FrameworkBundle\Translation\Translator
40
     */
41
    private $translator;
42
43
    /**
44
     * Holds info if some property has been already sorted or not.
45
     *
46
     * @var array
47
     */
48
    private $sortCache = [];
49
50
    /**
51
     * CampaignBuilderEvent constructor.
52
     */
53
    public function __construct(TranslatorInterface $translator)
54
    {
55
        $this->translator = $translator;
0 ignored issues
show
Documentation Bug introduced by
$translator is of type Symfony\Component\Translation\TranslatorInterface, but the property $translator was declared to be of type Symfony\Bundle\Framework...\Translation\Translator. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
56
    }
57
58
    /**
59
     * Add an lead decision to the list of available .
60
     *
61
     * @param string $key      a unique identifier; it is recommended that it be namespaced i.e. lead.mytrigger
62
     * @param array  $decision can contain the following keys:
63
     *                         $decision = [
64
     *                         'label'                   => (required) what to display in the list
65
     *                         'eventName'               => (required) The event name to fire when this event is triggered.
66
     *                         'description'             => (optional) short description of event
67
     *                         'formType'                => (optional) name of the form type SERVICE for the action
68
     *                         'formTypeOptions'         => (optional) array of options to pass to the formType service
69
     *                         'formTheme'               => (optional) form theme
70
     *                         'connectionRestrictions'  => (optional) Array of events to restrict this event to. Implicit events
71
     *                         [
72
     *                         'anchor' => [], // array of anchors this event should _not_ be allowed to connect to in the format of eventType.anchorName, e.g. decision.no
73
     *                         'source' => ['action' => [], 'decision' => [], 'condition' => []], // array of event keys allowed to connect into this event
74
     *                         'target' => ['action' => [], 'decision' => [], 'condition' => []], // array of event keys allowed to flow from this event
75
     *                         ]
76
     *                         ]
77
     */
78
    public function addDecision($key, array $decision)
79
    {
80
        if (array_key_exists($key, $this->decisions)) {
81
            throw new KeyAlreadyRegisteredException("The key, '$key' is already used by another contact action. Please use a different key.");
82
        }
83
84
        //check for required keys and that given functions are callable
85
        $this->verifyComponent(
86
            ['label', ['eventName', 'callback']],
87
            $decision,
88
            ['callback']
89
        );
90
91
        $decision['label']       = $this->translator->trans($decision['label']);
92
        $decision['description'] = (isset($decision['description'])) ? $this->translator->trans($decision['description']) : '';
93
94
        $this->decisions[$key] = $decision;
95
    }
96
97
    /**
98
     * @return mixed
99
     */
100
    public function getDecisions()
101
    {
102
        return $this->sort('decisions');
103
    }
104
105
    /**
106
     * Add an lead condition to the list of available conditions.
107
     *
108
     * @param string $key   a unique identifier; it is recommended that it be namespaced i.e. lead.mytrigger
109
     * @param array  $event can contain the following keys:
110
     *                      $condition = [
111
     *                      'label'                   => (required) what to display in the list
112
     *                      'eventName'               => (required) The event name to fire when this event is triggered.
113
     *                      'description'             => (optional) short description of event
114
     *                      'formType'                => (optional) name of the form type SERVICE for the action
115
     *                      'formTypeOptions'         => (optional) array of options to pass to the formType service
116
     *                      'formTheme'               => (optional) form theme
117
     *                      'connectionRestrictions'  => (optional) Array of events to restrict this event to. Implicit events
118
     *                      [
119
     *                      'anchor' => [], // array of anchors this event should _not_ be allowed to connect to in the format of eventType.anchorName, e.g. decision.no
120
     *                      'source' => ['action' => [], 'decision' => [], 'condition' => []], // array of event keys allowed to connect into this event
121
     *                      'target' => ['action' => [], 'decision' => [], 'condition' => []], // array of event keys allowed to flow from this event
122
     *                      ]
123
     *                      ]
124
     */
125
    public function addCondition($key, array $event)
126
    {
127
        if (array_key_exists($key, $this->conditions)) {
128
            throw new KeyAlreadyRegisteredException("The key, '$key' is already used by another contact action. Please use a different key.");
129
        }
130
131
        //check for required keys and that given functions are callable
132
        $this->verifyComponent(
133
            ['label', ['eventName', 'callback']],
134
            $event,
135
            ['callback']
136
        );
137
138
        $event['label']       = $this->translator->trans($event['label']);
139
        $event['description'] = (isset($event['description'])) ? $this->translator->trans($event['description']) : '';
140
141
        $this->conditions[$key] = $event;
142
    }
143
144
    /**
145
     * Get lead conditions.
146
     *
147
     * @return array
148
     */
149
    public function getConditions()
150
    {
151
        return $this->sort('conditions');
152
    }
153
154
    /**
155
     * Add an action to the list of available .
156
     *
157
     * @param string $key    a unique identifier; it is recommended that it be namespaced i.e. lead.action
158
     * @param array  $action can contain the following keys:
159
     *                       $action = [
160
     *                       'label'               => (required) what to display in the list
161
     *                       'eventName'           => (required) The event to fire when this event is triggered.
162
     *                       'description'         => (optional) short description of event
163
     *                       'formType'            => (optional) name of the form type SERVICE for the action
164
     *                       'formTypeOptions'     => (optional) array of options to pass to the formType service
165
     *                       'formTheme'           => (optional) form theme
166
     *                       'timelineTemplate'    => (optional) custom template for the lead timeline
167
     *                       'connectionRestrictions'  => (optional) Array of events to restrict this event to. Implicit events
168
     *                       [
169
     *                       'anchor' => [], // array of anchors this event should _not_ be allowed to connect to in the format of eventType.anchorName, e.g. decision.no
170
     *                       'source' => ['action' => [], 'decision' => [], 'condition' => []], // array of event keys allowed to connect into this event
171
     *                       'target' => ['action' => [], 'decision' => [], 'condition' => []], // array of event keys allowed to flow from this event
172
     *                       ]
173
     *                       ]
174
     */
175
    public function addAction($key, array $action)
176
    {
177
        if (array_key_exists($key, $this->actions)) {
178
            throw new KeyAlreadyRegisteredException("The key, '$key' is already used by another action. Please use a different key.");
179
        }
180
181
        //check for required keys and that given functions are callable
182
        $this->verifyComponent(
183
            ['label', ['batchEventName', 'eventName', 'callback']],
184
            $action,
185
            ['callback']
186
        );
187
188
        //translate the group
189
        $action['label']       = $this->translator->trans($action['label']);
190
        $action['description'] = (isset($action['description'])) ? $this->translator->trans($action['description']) : '';
191
192
        $this->actions[$key] = $action;
193
    }
194
195
    /**
196
     * Get actions.
197
     *
198
     * @return array
199
     */
200
    public function getActions()
201
    {
202
        return $this->sort('actions');
203
    }
204
205
    /**
206
     * Sort internal actions, decisions and conditions arrays.
207
     *
208
     * @param string $property name
209
     *
210
     * @return array
211
     */
212
    protected function sort($property)
213
    {
214
        if (empty($this->sortCache[$property])) {
215
            uasort(
216
                $this->{$property},
217
                function ($a, $b) {
218
                    return strnatcasecmp(
219
                        $a['label'],
220
                        $b['label']
221
                    );
222
                }
223
            );
224
            $this->sortCache[$property] = true;
225
        }
226
227
        return $this->{$property};
228
    }
229
}
230