Completed
Push — actionrefactor ( 6e4bf0 )
by Andreas
04:36
created

AbstractAction::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 1
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace dokuwiki\Action;
4
5
use dokuwiki\Action\Exception\ActionDisabledException;
6
use dokuwiki\Action\Exception\ActionException;
7
use dokuwiki\Action\Exception\FatalException;
8
9
/**
10
 * Class AbstractAction
11
 *
12
 * Base class for all actions
13
 *
14
 * @package dokuwiki\Action
15
 */
16
abstract class AbstractAction {
17
18
    /** @var string holds the name of the action (lowercase class name, no namespace) */
19
    protected $actionname;
20
21
    /**
22
     * AbstractAction constructor.
23
     *
24
     * @param string $actionname the name of this action (see getActioName() for caveats)
25
     */
26
    public function __construct($actionname='') {
27
        if($actionname !== '') {
28
            $this->actionname = $actionname;
29
        } else {
30
            // http://stackoverflow.com/a/27457689/172068
31
            $this->actionname = strtolower(substr(strrchr(get_class($this), '\\'), 1));
32
        }
33
    }
34
35
    /**
36
     * Return the minimum permission needed
37
     *
38
     * This needs to return one of the AUTH_* constants. It will be checked against
39
     * the current user and page after checkPermissions() ran through. If it fails,
40
     * the user will be shown the Denied action.
41
     *
42
     * @return int
43
     */
44
    abstract function minimumPermission();
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
45
46
    /**
47
     * Check permissions are correct to run this action
48
     *
49
     * @throws ActionException
50
     * @return void
51
     */
52
    public function checkPermissions() {
53
        if(!actionOK($this->actionname)) throw new ActionDisabledException();
54
    }
55
56
    /**
57
     * Process data
58
     *
59
     * This runs before any output is sent to the browser.
60
     *
61
     * Throw an Exception if a different action should be run after this step.
62
     *
63
     * @throws ActionException
64
     * @return void
65
     */
66
    public function preProcess() {
67
    }
68
69
    /**
70
     * Output whatever content is wanted within tpl_content();
71
     *
72
     * @fixme we may want to return a Ui class here
73
     */
74
    public function tplContent() {
75
        throw new FatalException('No content for Action ' . $this->actionname);
76
    }
77
78
    /**
79
     * Returns the name of this action
80
     *
81
     * This is usually the lowercased class name, but may differ for some actions.
82
     * eg. the export_ modes or for the Plugin action.
83
     *
84
     * @return string
85
     */
86
    public function getActionName() {
87
        return $this->actionname;
88
    }
89
}
90