Passed
Push — master ( f83aa9...a7daae )
by Alex
03:39
created

ActionBuilder   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 106
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 24
c 1
b 0
f 1
dl 0
loc 106
rs 10
wmc 11

5 Methods

Rating   Name   Duplication   Size   Complexity  
A resetLayout() 0 4 1
A ignoreKey() 0 3 1
A getActionBuilderMethod() 0 9 3
A constructOtherView() 0 20 4
A constructOverrideHandler() 0 9 2
1
<?php
2
namespace Mezon\Application;
3
4
use Mezon\HtmlTemplate\HtmlTemplate;
5
6
class ActionBuilder
7
{
8
9
    /**
10
     * Ignore config keyu
11
     *
12
     * @return callable factory method
13
     */
14
    public static function ignoreKey(): callable
15
    {
16
        return function (): void {
17
            // do nothind
18
        };
19
    }
20
21
    /**
22
     * Method resets layout
23
     *
24
     * @param $template template
0 ignored issues
show
Bug introduced by
The type Mezon\Application\template 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...
25
     * @param string $value
26
     *            new layout
27
     * @return callable factory method
28
     */
29
    public static function resetLayout(HtmlTemplate $template, string $value): callable
30
    {
31
        return function () use ($template, $value) {
32
            $template->resetLayout($value);
33
        };
34
    }
35
36
    /**
37
     * Overriding defined config
38
     *
39
     * @param string $path
40
     *            path to the current config
41
     * @param array $config
42
     *            config itself
43
     */
44
    public static function constructOverrideHandler(string $path, array &$config): void
45
    {
46
        if (isset($config['override'])) {
47
48
            $path = pathinfo($path, PATHINFO_DIRNAME);
49
50
            $baseConfig = json_decode(file_get_contents($path . '/' . $config['override']), true);
0 ignored issues
show
Bug introduced by
Are you sure $path of type array|string can be used in concatenation? ( Ignorable by Annotation )

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

50
            $baseConfig = json_decode(file_get_contents(/** @scrutinizer ignore-type */ $path . '/' . $config['override']), true);
Loading history...
51
52
            $config = array_merge($baseConfig, $config);
53
        }
54
    }
55
56
    /**
57
     * Method returns fabric method for action processing
58
     *
59
     * @param CommonApplication $app
60
     *            application
61
     * @param string $key
62
     *            config key name
63
     * @param mixed $value
64
     *            config key value
65
     * @return callable|NULL callback
66
     */
67
    public static function getActionBuilderMethod(CommonApplication $app, string $key, $value): ?callable
68
    {
69
        if ($key === 'override') {
70
            return ActionBuilder::ignoreKey();
71
        } elseif ($key === 'layout') {
72
            return ActionBuilder::resetLayout($app->getTemplate(), $value);
73
        }
74
75
        return null;
76
    }
77
78
    /**
79
     * Constructing view
80
     *
81
     * @param CommonApplication $app
82
     *            application
83
     * @param array $result
84
     *            compiled result
85
     * @param string $key
86
     *            config key
87
     * @param mixed $value
88
     *            config value
89
     * @param array $views
90
     *            list of views
91
     */
92
    public static function constructOtherView(CommonApplication $app, array &$result, string $key, $value, array &$views): void
93
    {
94
        // any other view
95
        if (isset($value['name'])) {
96
            $views[$key] = new $value['class']($app->getTemplate(), $value['name']);
97
        } else {
98
            $views[$key] = new $value['class']($app->getTemplate());
99
        }
100
101
        foreach ($value as $configKey => $configValue) {
102
            if (! in_array($configKey, [
103
                'class',
104
                'name',
105
                'placeholder'
106
            ])) {
107
                $views[$key]->setViewParameter($configKey, $configValue, true);
108
            }
109
        }
110
111
        $result[$value['placeholder']] = $views[$key];
112
    }
113
}
114