Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
24 / 24
100.00% covered (success)
100.00%
7 / 7
CRAP
100.00% covered (success)
100.00%
1 / 1
AbstractController
100.00% covered (success)
100.00%
24 / 24
100.00% covered (success)
100.00%
7 / 7
15
100.00% covered (success)
100.00%
1 / 1
 setDefaultAction
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getDefaultAction
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setMaintenanceAction
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getMaintenanceAction
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setBypassMaintenance
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 bypassMaintenance
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 dispatch
100.00% covered (success)
100.00%
15 / 15
100.00% covered (success)
100.00%
1 / 1
9
1<?php
2/**
3 * Pop PHP Framework (https://www.popphp.org/)
4 *
5 * @link       https://github.com/popphp/popphp-framework
6 * @author     Nick Sagona, III <dev@noladev.com>
7 * @copyright  Copyright (c) 2009-2026 NOLA Interactive, LLC.
8 * @license    https://www.popphp.org/license     New BSD License
9 */
10
11/**
12 * @namespace
13 */
14namespace Pop\Controller;
15
16use Pop\App;
17
18/**
19 * Pop abstract controller class
20 *
21 * @category   Pop
22 * @package    Pop\Controller
23 * @author     Nick Sagona, III <dev@noladev.com>
24 * @copyright  Copyright (c) 2009-2026 NOLA Interactive, LLC.
25 * @license    https://www.popphp.org/license     New BSD License
26 * @version    4.4.0
27 */
28abstract class AbstractController implements ControllerInterface
29{
30
31    /**
32     * Default action
33     * @var string
34     */
35    protected string $defaultAction = 'error';
36
37    /**
38     * Maintenance action
39     * @var string
40     */
41    protected string $maintenanceAction = 'maintenance';
42
43    /**
44     * Bypass maintenance false
45     * @var bool
46     */
47    protected bool $bypassMaintenance = false;
48
49    /**
50     * Set the default action
51     *
52     * @param  string $default
53     * @return AbstractController
54     */
55    public function setDefaultAction(string $default): AbstractController
56    {
57        $this->defaultAction = $default;
58        return $this;
59    }
60
61    /**
62     * Get the default action
63     *
64     * @return string
65     */
66    public function getDefaultAction(): string
67    {
68        return $this->defaultAction;
69    }
70
71    /**
72     * Set the maintenance action
73     *
74     * @param  string $maintenance
75     * @return AbstractController
76     */
77    public function setMaintenanceAction(string $maintenance): AbstractController
78    {
79        $this->maintenanceAction = $maintenance;
80        return $this;
81    }
82
83    /**
84     * Get the maintenance action
85     *
86     * @return string
87     */
88    public function getMaintenanceAction(): string
89    {
90        return $this->maintenanceAction;
91    }
92
93    /**
94     * Check the bypass maintenance check
95     *
96     * @param  bool $bypass
97     * @return static
98     */
99    public function setBypassMaintenance(bool $bypass = true): static
100    {
101        $this->bypassMaintenance = $bypass;
102        return $this;
103    }
104
105    /**
106     * Check the bypass maintenance check
107     *
108     * @return bool
109     */
110    public function bypassMaintenance(): bool
111    {
112        return $this->bypassMaintenance;
113    }
114
115    /**
116     * Dispatch the controller based on the action
117     *
118     * @param  ?string $action
119     * @param  ?array  $params
120     * @throws Exception
121     * @return void
122     */
123    public function dispatch(?string $action = null, ?array $params = null): void
124    {
125        // Handle maintenance mode
126        if ((App::isDown()) && (!App::isSecretRequest()) && (!$this->bypassMaintenance)) {
127            if (method_exists($this, $this->maintenanceAction)) {
128                $action = $this->maintenanceAction;
129                $this->$action();
130            } else {
131                throw new Exception(
132                    "The application is currently in maintenance mode. The maintenance action is not defined in the controller."
133                );
134            }
135        // Else, dispatch route action
136        } else if (($action !== null) && method_exists($this, $action)) {
137            if ($params !== null) {
138                call_user_func_array([$this, $action], array_values($params));
139            } else {
140                $this->$action();
141            }
142        // Else, fallback to default route action
143        } else if (method_exists($this, $this->defaultAction)) {
144            $action = $this->defaultAction;
145            $this->$action();
146        } else {
147            throw new Exception("The action to handle the route is not defined in the controller.");
148        }
149    }
150
151}