Issues (3627)

bundles/StageBundle/Event/StageBuilderEvent.php (3 issues)

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\StageBundle\Event;
13
14
use Symfony\Component\EventDispatcher\Event;
15
use Symfony\Component\Process\Exception\InvalidArgumentException;
16
use Symfony\Component\Translation\TranslatorInterface;
17
18
/**
19
 * Class StageBuilderEvent.
20
 */
21
class StageBuilderEvent extends Event
22
{
23
    /**
24
     * @var array
25
     */
26
    private $actions = [];
27
28
    /**
29
     * @var Translator
0 ignored issues
show
The type Mautic\StageBundle\Event\Translator was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
30
     */
31
    private $translator;
32
33
    /**
34
     * @param Translator $translator
35
     */
36
    public function __construct(TranslatorInterface $translator)
37
    {
38
        $this->translator = $translator;
0 ignored issues
show
Documentation Bug introduced by
It seems like $translator of type Symfony\Component\Translation\TranslatorInterface is incompatible with the declared type Mautic\StageBundle\Event\Translator of property $translator.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
39
    }
40
41
    /**
42
     * Adds an action to the list of available .
43
     *
44
     * @param string $key    - a unique identifier; it is recommended that it be namespaced i.e. lead.action
45
     * @param array  $action - can contain the following keys:
46
     *                       'label'           => (required) what to display in the list
47
     *                       'description'     => (optional) short description of event
48
     *                       'template'        => (optional) template to use for the action's HTML in the stage builder
49
     *                       i.e AcmeMyBundle:StageAction:theaction.html.php
50
     *                       'formType'        => (optional) name of the form type SERVICE for the action; will use a default form with stage change only
51
     *                       'formTypeOptions' => (optional) array of options to pass to formType
52
     *                       'callback'        => (optional) callback function that will be passed when the action is triggered; return true to
53
     *                       change the configured stages or false to ignore the action
54
     *                       The callback function can receive the following arguments by name (via ReflectionMethod::invokeArgs())
55
     *                       Mautic\LeadBundle\Entity\Lead $lead
56
     *                       $eventDetails - variable sent from firing function to call back function
57
     *                       array $action = array(
58
     *                       'id' => int
59
     *                       'type' => string
60
     *                       'name' => string
61
     *                       'properties' => array()
62
     *                       )
63
     *
64
     * @throws InvalidArgumentException
65
     */
66
    public function addAction($key, array $action)
67
    {
68
        if (array_key_exists($key, $this->actions)) {
69
            throw new InvalidArgumentException("The key, '$key' is already used by another action. Please use a different key.");
70
        }
71
72
        //check for required keys and that given functions are callable
73
        $this->verifyComponent(
74
            ['group', 'label'],
75
            ['callback'],
76
            $action
77
        );
78
79
        //translate the label and group
80
        $action['label'] = $this->translator->trans($action['label']);
81
        $action['group'] = $this->translator->trans($action['group']);
82
83
        $this->actions[$key] = $action;
84
    }
85
86
    /**
87
     * Get actions.
88
     *
89
     * @return array
90
     */
91
    public function getActions()
92
    {
93
        uasort($this->actions, function ($a, $b) {
94
            return strnatcasecmp(
95
                $a['label'], $b['label']);
96
        });
97
98
        return $this->actions;
99
    }
100
101
    /**
102
     * Gets a list of actions supported by the choice form field.
103
     *
104
     * @return array
105
     */
106
    public function getActionList()
107
    {
108
        $list    = [];
109
        $actions = $this->getActions();
110
        foreach ($actions as $k => $a) {
111
            $list[$k] = $a['label'];
112
        }
113
114
        return $list;
115
    }
116
117
    public function getActionChoices()
118
    {
119
        $choices = [];
120
        $actions = $this->getActions();
0 ignored issues
show
The assignment to $actions is dead and can be removed.
Loading history...
121
        foreach ($this->actions as $k => $c) {
122
            $choices[$c['group']][$k] = $c['label'];
123
        }
124
125
        return $choices;
126
    }
127
128
    /**
129
     * @throws InvalidArgumentException
130
     */
131
    private function verifyComponent(array $keys, array $methods, array $component)
132
    {
133
        foreach ($keys as $k) {
134
            if (!array_key_exists($k, $component)) {
135
                throw new InvalidArgumentException("The key, '$k' is missing.");
136
            }
137
        }
138
139
        foreach ($methods as $m) {
140
            if (isset($component[$m]) && !is_callable($component[$m], true)) {
141
                throw new InvalidArgumentException($component[$m].' is not callable.  Please ensure that it exists and that it is a fully qualified namespace.');
142
            }
143
        }
144
    }
145
}
146