PluginTrait   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 170
Duplicated Lines 0 %

Importance

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

20 Methods

Rating   Name   Duplication   Size   Complexity  
registerPlugins() 0 42 ?
A hp$0 ➔ getCodeGenerator() 0 3 1
A hp$0 ➔ getAssetManager() 0 3 1
getAssetManager() 0 3 ?
getPackageConfigKey() 0 3 ?
registerPackage() 0 26 ?
getPluginManager() 0 3 ?
getPackageManager() 0 3 ?
getScriptPlugin() 0 3 ?
A hp$0 ➔ registerPackage() 0 26 2
A hp$0 ➔ getPackageConfigKey() 0 3 1
getCodeGenerator() 0 3 ?
getDialogPlugin() 0 3 ?
A hp$0 ➔ registerPlugins() 0 42 1
A hp$0 ➔ getDialogPlugin() 0 3 1
A hp$0 ➔ getPackageConfig() 0 3 1
A hp$0 ➔ getPackageManager() 0 3 1
getPackageConfig() 0 3 ?
A hp$0 ➔ getScriptPlugin() 0 3 1
A hp$0 ➔ getPluginManager() 0 3 1
1
<?php
2
3
namespace Jaxon\Di\Traits;
4
5
use Jaxon\Jaxon;
6
use Jaxon\App\Config\ConfigManager;
7
use Jaxon\App\I18n\Translator;
8
use Jaxon\App\View\ViewRenderer;
9
use Jaxon\Di\Container;
10
use Jaxon\Exception\SetupException;
11
use Jaxon\Script\Factory\CallFactory;
12
use Jaxon\Plugin\Code\AssetManager;
13
use Jaxon\Plugin\Code\CodeGenerator;
14
use Jaxon\Plugin\Code\MinifierInterface;
15
use Jaxon\Plugin\Manager\PackageManager;
16
use Jaxon\Plugin\Manager\PluginManager;
17
use Jaxon\Plugin\Request\CallableClass\CallableRegistry;
18
use Jaxon\Plugin\Response\DataBag\DataBagPlugin;
19
use Jaxon\Plugin\Response\Dialog\DialogCommand;
20
use Jaxon\Plugin\Response\Dialog\DialogPlugin;
21
use Jaxon\Plugin\Response\Dialog\DialogManager;
22
use Jaxon\Plugin\Response\Script\ScriptPlugin;
23
use Jaxon\Request\Handler\CallbackManager;
24
use Jaxon\Request\Handler\ParameterReader;
25
use Jaxon\Utils\Config\Config;
26
use Jaxon\Utils\File\FileMinifier;
27
use Jaxon\Utils\Template\TemplateEngine;
28
29
use function call_user_func;
30
31
trait PluginTrait
32
{
33
    /**
34
     * Register the values into the container
35
     *
36
     * @return void
37
     */
38
    private function registerPlugins()
39
    {
40
        // Plugin manager
41
        $this->set(PluginManager::class, function($di) {
0 ignored issues
show
Bug introduced by
It seems like set() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

41
        $this->/** @scrutinizer ignore-call */ 
42
               set(PluginManager::class, function($di) {
Loading history...
42
            $xPluginManager = new PluginManager($di->g(Container::class),
43
                $di->g(CodeGenerator::class), $di->g(Translator::class));
44
            // Register the Jaxon request and response plugins
45
            $xPluginManager->registerPlugins();
46
            return $xPluginManager;
47
        });
48
        // Package manager
49
        $this->set(PackageManager::class, function($di) {
50
            return new PackageManager($di->g(Container::class), $di->g(Translator::class),
51
                $di->g(PluginManager::class), $di->g(ConfigManager::class),
52
                $di->g(CodeGenerator::class), $di->g(ViewRenderer::class),
53
                $di->g(CallbackManager::class), $di->g(CallableRegistry::class));
54
        });
55
        // Code Generation
56
        $this->set(MinifierInterface::class, function() {
57
            return new class extends FileMinifier implements MinifierInterface
58
            {};
59
        });
60
        $this->set(AssetManager::class, function($di) {
61
            return new AssetManager($di->g(ConfigManager::class), $di->g(ParameterReader::class),
62
                $di->g(MinifierInterface::class));
63
        });
64
        $this->set(CodeGenerator::class, function($di) {
65
            return new CodeGenerator(Jaxon::VERSION, $di->g(Container::class),
66
            $di->g(Translator::class), $di->g(TemplateEngine::class));
67
        });
68
69
        // Script response plugin
70
        $this->set(ScriptPlugin::class, function($di) {
71
            return new ScriptPlugin($di->g(CallFactory::class));
72
        });
73
        // DataBag response plugin
74
        $this->set(DataBagPlugin::class, function($di) {
75
            return new DataBagPlugin($di->g(Container::class));
76
        });
77
        // Dialog response plugin
78
        $this->set(DialogPlugin::class, function($di) {
79
            return new DialogPlugin($di->g(DialogCommand::class), $di->g(DialogManager::class));
80
        });
81
    }
82
83
    /**
84
     * Get the plugin manager
85
     *
86
     * @return PluginManager
87
     */
88
    public function getPluginManager(): PluginManager
89
    {
90
        return $this->g(PluginManager::class);
0 ignored issues
show
Bug introduced by
It seems like g() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

90
        return $this->/** @scrutinizer ignore-call */ g(PluginManager::class);
Loading history...
91
    }
92
93
    /**
94
     * Get the package manager
95
     *
96
     * @return PackageManager
97
     */
98
    public function getPackageManager(): PackageManager
99
    {
100
        return $this->g(PackageManager::class);
101
    }
102
103
    /**
104
     * Get the code generator
105
     *
106
     * @return CodeGenerator
107
     */
108
    public function getCodeGenerator(): CodeGenerator
109
    {
110
        return $this->g(CodeGenerator::class);
111
    }
112
113
    /**
114
     * Get the asset manager
115
     *
116
     * @return AssetManager
117
     */
118
    public function getAssetManager(): AssetManager
119
    {
120
        return $this->g(AssetManager::class);
121
    }
122
123
    /**
124
     * Get the jQuery plugin
125
     *
126
     * @return ScriptPlugin
127
     */
128
    public function getScriptPlugin(): ScriptPlugin
129
    {
130
        return $this->g(ScriptPlugin::class);
131
    }
132
133
    /**
134
     * Get the dialog plugin
135
     *
136
     * @return DialogPlugin
137
     */
138
    public function getDialogPlugin(): DialogPlugin
139
    {
140
        return $this->g(DialogPlugin::class);
141
    }
142
143
    /**
144
     * @param string $sClassName    The package class name
145
     *
146
     * @return string
147
     */
148
    private function getPackageConfigKey(string $sClassName): string
149
    {
150
        return $sClassName . '_PackageConfig';
151
    }
152
153
    /**
154
     * Register a package
155
     *
156
     * @param string $sClassName    The package class name
157
     * @param Config $xPkgConfig    The user provided package options
158
     *
159
     * @return void
160
     * @throws SetupException
161
     */
162
    public function registerPackage(string $sClassName, Config $xPkgConfig)
163
    {
164
        // Register the user class, but only if the user didn't already.
165
        if(!$this->h($sClassName))
0 ignored issues
show
Bug introduced by
It seems like h() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

165
        if(!$this->/** @scrutinizer ignore-call */ h($sClassName))
Loading history...
166
        {
167
            $this->set($sClassName, function() use($sClassName) {
168
                return $this->make($sClassName);
0 ignored issues
show
Bug introduced by
It seems like make() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

168
                return $this->/** @scrutinizer ignore-call */ make($sClassName);
Loading history...
169
            });
170
        }
171
172
        // Save the package config in the container.
173
        $this->val($this->getPackageConfigKey($sClassName), $xPkgConfig);
0 ignored issues
show
Bug introduced by
It seems like val() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

173
        $this->/** @scrutinizer ignore-call */ 
174
               val($this->getPackageConfigKey($sClassName), $xPkgConfig);
Loading history...
174
175
        // Initialize the package instance.
176
        $this->xLibContainer->extend($sClassName, function($xPackage) use($sClassName) {
177
            $xPkgConfig = $this->getPackageConfig($sClassName);
178
            $xViewRenderer = $this->g(ViewRenderer::class);
179
            $cSetter = function() use($xPkgConfig, $xViewRenderer) {
180
                // Set the protected attributes of the Package instance.
181
                $this->xPkgConfig = $xPkgConfig;
0 ignored issues
show
Bug Best Practice introduced by
The property xPkgConfig does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
182
                $this->xRenderer = $xViewRenderer;
0 ignored issues
show
Bug Best Practice introduced by
The property xRenderer does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
183
                $this->init();
0 ignored issues
show
Bug introduced by
It seems like init() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

183
                $this->/** @scrutinizer ignore-call */ 
184
                       init();
Loading history...
184
            };
185
            // Can now access protected attributes
186
            call_user_func($cSetter->bindTo($xPackage, $xPackage));
187
            return $xPackage;
188
        });
189
    }
190
191
    /**
192
     * Get the config of a package
193
     *
194
     * @param string $sClassName    The package class name
195
     *
196
     * @return Config
197
     */
198
    public function getPackageConfig(string $sClassName): Config
199
    {
200
        return $this->g($this->getPackageConfigKey($sClassName));
201
    }
202
}
203