StyleChooserController::updateActionPost()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 17

Duplication

Lines 4
Ratio 23.53 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 4
loc 17
ccs 0
cts 12
cp 0
rs 9.7
c 0
b 0
f 0
cc 3
nc 3
nop 0
crap 12
1
<?php
2
3
namespace Anax\StyleChooser;
4
5
use Anax\Commons\ContainerInjectableInterface;
6
use Anax\Commons\ContainerInjectableTrait;
7
8
/**
9
 * Style chooser controller loads available stylesheets from a directory and
10
 * lets the user choose the stylesheet to use.
11
 */
12
class StyleChooserController implements ContainerInjectableInterface
13
{
14
    use ContainerInjectableTrait;
15
16
17
18
    /**
19
     * @var string $cssUrl The baseurl to where the css files are.
20
     * @var string $cssDir The path to the directory storing css files.
21
     * @var array  $styles The styles available in the style directory.
22
     * @var string $key    The session key used to store the active style.
23
     */
24
    private $cssUrl = "css";
25
    private $cssDir = ANAX_INSTALL_PATH . "/htdocs/css";
26
    private $styles = [];
27
    private static $key = "AnaxStyleChooser";
28
29
30
31
    /**
32
     * Get the session key to use to retrieve the active stylesheet.
33
     *
34
     * @return string
35
     */
36
    public static function getSessionKey() : string
37
    {
38
        return self::$key;
39
    }
40
41
42
43
    /**
44
     * The initialize method is optional and will always be called before the
45
     * target method/action. This is a convienient method where you could
46
     * setup internal properties that are commonly used by several methods.
47
     *
48
     * @return void
49
     */
50
    public function initialize() : void
51
    {
52
        foreach (glob("{$this->cssDir}/*.css") as $file) {
53
            $filename = basename($file);
54
            $url = "{$this->cssUrl}/$filename";
55
            $content = file_get_contents($file);
56
            $comment = strstr($content, "*/", true);
57
            $comment = preg_replace(["#\/\*!#", "#\*#"], "", $comment);
58
            $comment = preg_replace("#@#", "<br>@", $comment);
59
            $first = strpos($comment, ".");
60
            $short = substr($comment, 0, $first + 1);
61
            $long = substr($comment, $first + 1);
62
            $this->styles[$url] = [
63
                "shortDescription" => $short,
64
                "longDescription" => $long,
65
            ];
66
        }
67
68
        foreach ($this->styles as $key => $value) {
69
            $isMinified = strstr($key, ".min.css", true);
70
            if ($isMinified) {
71
                unset($this->styles["$isMinified.css"]);
72
            }
73
        }
74
    }
75
76
77
78
    /**
79
     * Display the stylechooser with details on current selected style.
80
     *
81
     * @return object
82
     */
83
    public function indexAction() : object
84
    {
85
        $title = "Stylechooser";
86
87
        $page = $this->di->get("page");
88
        $session = $this->di->get("session");
89
90
        $active = $session->get(self::$key, null);
91
92
        $page->add("anax/v2/stylechooser/default", [
93
            "styles" => $this->styles,
94
            "activeStyle" => $active,
95
            "activeShortDescription" => $this->styles[$active]["shortDescription"] ?? null,
96
            "activeLongDescription" => $this->styles[$active]["longDescription"] ?? null,
97
        ]);
98
99
        return $page->render([
100
            "title" => $title,
101
        ]);
102
    }
103
104
105
106
    /**
107
     * Update current selected style.
108
     *
109
     * @return object
110
     */
111
    public function updateActionPost() : object
112
    {
113
        $response = $this->di->get("response");
114
        $request = $this->di->get("request");
115
        $session = $this->di->get("session");
116
        $key = $request->getPost("stylechooser");
117
118
        if ($key === "none") {
119
            $session->set("flashmessage", "Unsetting the style and using deafult style.");
120
            $session->set(self::$key, null);
121 View Code Duplication
        } elseif (array_key_exists($key, $this->styles)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
122
            $session->set("flashmessage", "Using the style '$key'.");
123
            $session->set(self::$key, $key);
124
        }
125
126
        return $response->redirect("style");
127
    }
128
129
130
131
    /**
132
     * Update current selected style using a GET url and redirect to last
133
     * page visited.
134
     *
135
     * @param string $style the key to the style to use.
136
     *
137
     * @return object
138
     */
139
    public function updateActionGet($style) : object
140
    {
141
        $response = $this->di->get("response");
142
        $session = $this->di->get("session");
143
144
        $key = $this->cssUrl . "/" . $style . ".css";
145
        $keyMin = $this->cssUrl . "/" . $style . ".min.css";
146
147
        if ($style === "none") {
148
            $session->set("flashmessage", "Unsetting the style and using the default style.");
149
            $session->set(self::$key, null);
150 View Code Duplication
        } elseif (array_key_exists($keyMin, $this->styles)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
151
            $session->set("flashmessage", "Now using the style '$keyMin'.");
152
            $session->set(self::$key, $keyMin);
153
        } elseif (array_key_exists($key, $this->styles)) {
154
            $session->set("flashmessage", "Now using the style '$key'.");
155
            $session->set(self::$key, $key);
156
        }
157
158
        $url = $session->getOnce("redirect", "style");
159
        return $response->redirect($url);
160
    }
161
}
162