ActionController::createAction()   C
last analyzed

Complexity

Conditions 8
Paths 10

Size

Total Lines 48
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 24
nc 10
nop 4
dl 0
loc 48
rs 5.9322
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of Jitamin.
5
 *
6
 * Copyright (C) Jitamin Team
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Jitamin\Http\Controllers\Api;
13
14
use Jitamin\Policy\ActionPolicy;
15
use Jitamin\Policy\ProjectPolicy;
16
17
/**
18
 * Action API controller.
19
 */
20
class ActionController extends Controller
21
{
22
    /**
23
     * Get available automatic actions.
24
     *
25
     * @return array
26
     */
27
    public function getAvailableActions()
28
    {
29
        return $this->actionManager->getAvailableActions();
0 ignored issues
show
Documentation introduced by
The property actionManager does not exist on object<Jitamin\Http\Cont...s\Api\ActionController>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
30
    }
31
32
    /**
33
     * Get the list of events and description that can be used from the user interface.
34
     *
35
     * @return array
36
     */
37
    public function getAvailableActionEvents()
38
    {
39
        return $this->eventManager->getAll();
0 ignored issues
show
Documentation introduced by
The property eventManager does not exist on object<Jitamin\Http\Cont...s\Api\ActionController>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
40
    }
41
42
    /**
43
     * Get list of compatible events for a given action.
44
     *
45
     * @param string $name
0 ignored issues
show
Bug introduced by
There is no parameter named $name. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
46
     *
47
     * @return array
48
     */
49
    public function getCompatibleActionEvents($action_name)
50
    {
51
        return $this->actionManager->getCompatibleEvents($action_name);
0 ignored issues
show
Documentation introduced by
The property actionManager does not exist on object<Jitamin\Http\Cont...s\Api\ActionController>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
52
    }
53
54
    /**
55
     * Remove an action.
56
     *
57
     * @param int $action_id
58
     *
59
     * @return bool
60
     */
61
    public function removeAction($action_id)
62
    {
63
        ActionPolicy::getInstance($this->container)->check($this->getClassName(), 'removeAction', $action_id);
64
65
        return $this->actionModel->remove($action_id);
0 ignored issues
show
Documentation introduced by
The property actionModel does not exist on object<Jitamin\Http\Cont...s\Api\ActionController>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
66
    }
67
68
    /**
69
     * Return actions and parameters for a given project.
70
     *
71
     * @param int $project_id
72
     *
73
     * @return array
74
     */
75
    public function getActions($project_id)
76
    {
77
        ProjectPolicy::getInstance($this->container)->check($this->getClassName(), 'getActions', $project_id);
78
79
        return $this->actionModel->getAllByProject($project_id);
0 ignored issues
show
Documentation introduced by
The property actionModel does not exist on object<Jitamin\Http\Cont...s\Api\ActionController>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
80
    }
81
82
    /**
83
     * Create an action.
84
     *
85
     * @param int    $project_id
86
     * @param string $event_name
87
     * @param string $action_name
88
     * @param array  $params
89
     *
90
     * @return bool|int
91
     */
92
    public function createAction($project_id, $event_name, $action_name, array $params)
93
    {
94
        ProjectPolicy::getInstance($this->container)->check($this->getClassName(), 'createAction', $project_id);
95
        $values = [
96
            'project_id'  => $project_id,
97
            'event_name'  => $event_name,
98
            'action_name' => $action_name,
99
            'params'      => $params,
100
        ];
101
102
        list($valid) = $this->actionValidator->validateCreation($values);
0 ignored issues
show
Documentation introduced by
The property actionValidator does not exist on object<Jitamin\Http\Cont...s\Api\ActionController>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
103
104
        if (!$valid) {
105
            return false;
106
        }
107
108
        // Check if the action exists
109
        $actions = $this->actionManager->getAvailableActions();
0 ignored issues
show
Documentation introduced by
The property actionManager does not exist on object<Jitamin\Http\Cont...s\Api\ActionController>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
110
111
        if (!isset($actions[$action_name])) {
112
            return false;
113
        }
114
115
        // Check the event
116
        $action = $this->actionManager->getAction($action_name);
0 ignored issues
show
Documentation introduced by
The property actionManager does not exist on object<Jitamin\Http\Cont...s\Api\ActionController>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
117
118
        if (!in_array($event_name, $action->getEvents())) {
119
            return false;
120
        }
121
122
        $required_params = $action->getActionRequiredParameters();
123
124
        // Check missing parameters
125
        foreach ($required_params as $param => $value) {
126
            if (!isset($params[$param])) {
127
                return false;
128
            }
129
        }
130
131
        // Check extra parameters
132
        foreach ($params as $param => $value) {
133
            if (!isset($required_params[$param])) {
134
                return false;
135
            }
136
        }
137
138
        return $this->actionModel->create($values);
0 ignored issues
show
Documentation introduced by
The property actionModel does not exist on object<Jitamin\Http\Cont...s\Api\ActionController>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
139
    }
140
}
141