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