BaseController   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
eloc 28
c 1
b 0
f 0
dl 0
loc 50
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A render() 0 34 5
A url() 0 2 1
A redirect() 0 3 1
1
<?php
2
3
namespace Lepton\Controller;
4
5
use Lepton\Authenticator\UserAuthenticator;
6
use Lepton\Core\Application;
7
use Liquid\{Liquid, Template};
8
use Lepton\Boson\QuerySet;
0 ignored issues
show
Bug introduced by
The type Lepton\Boson\QuerySet 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...
9
use Lepton\Http\Response\{SuccessResponse, RedirectResponse};
10
11
abstract class BaseController
12
{
13
    protected array $default_parameters;
14
    protected array $custom_filters;
15
    public string $baseLink;
16
17
    public function url($string){
18
        return Application::getDir()."/".$string;
19
    }
20
21
    public function render(string $view, array $parameters = array(), $headers = array())
22
    {
23
        Liquid::set('INCLUDE_SUFFIX', 'html');
24
        Liquid::set('INCLUDE_PREFIX', '');
25
        $path = "app/Views";
26
        $template  = new Template($path);
27
        $template->registerFilter(new SiteFilter());
0 ignored issues
show
Bug introduced by
new Lepton\Controller\SiteFilter() of type Lepton\Controller\SiteFilter is incompatible with the type string expected by parameter $filter of Liquid\Template::registerFilter(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

27
        $template->registerFilter(/** @scrutinizer ignore-type */ new SiteFilter());
Loading history...
28
29
        if(isset($this->custom_filters)){
30
            foreach($this->custom_filters as $filter){
31
                $template->registerFilter(new $filter());
0 ignored issues
show
Bug introduced by
new $filter() of type object is incompatible with the type string expected by parameter $filter of Liquid\Template::registerFilter(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

31
                $template->registerFilter(/** @scrutinizer ignore-type */ new $filter());
Loading history...
32
            }
33
        }
34
        $template->parseFile($view);
35
        $parameters = array_map(function ($x) {
36
            if ($x instanceof QuerySet) {
37
                return $x->do();
38
            } else {
39
                return $x;
40
            }
41
        }, $parameters);
42
43
44
        $authenticator = new UserAuthenticator();
45
46
47
        $parameters["page"] = $_SERVER["REQUEST_URI"];
48
        $parameters["logged_user"] = $authenticator->getLoggedUser();
49
        $parameters["base_link"] = $this->baseLink;
50
51
        if (isset($this->default_parameters)) {
52
            $parameters = array_merge($parameters, $this->default_parameters);
53
        }
54
        return new SuccessResponse($template->render($parameters), headers: $headers);
55
    }
56
57
58
    public function redirect($url, $htmx = false, $parse = true)
59
    {
60
        return new RedirectResponse($url, $htmx, $parse);
61
    }
62
}
63