Passed
Push — v5.x ( 3c34c5...4cf44b )
by Thierry
02:14
created

ViewTrait::getDialogManager()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
0 ignored issues
show
Coding Style introduced by
Missing file doc comment
Loading history...
3
namespace Jaxon\Di\Traits;
4
5
use Jaxon\App\Config\ConfigManager;
6
use Jaxon\App\Dialog\DialogManager;
7
use Jaxon\App\Dialog\Library\AlertLibrary;
8
use Jaxon\App\Dialog\Library\DialogLibraryHelper;
9
use Jaxon\App\Dialog\Library\DialogLibraryManager;
10
use Jaxon\App\Dialog\LibraryInterface;
11
use Jaxon\App\Dialog\MessageInterface;
12
use Jaxon\App\Dialog\ModalInterface;
13
use Jaxon\App\Dialog\QuestionInterface;
14
use Jaxon\App\I18n\Translator;
15
use Jaxon\App\View\TemplateView;
16
use Jaxon\App\View\ViewRenderer;
17
use Jaxon\Di\Container;
18
use Jaxon\Utils\Template\TemplateEngine;
19
20
use function call_user_func;
21
use function rtrim;
22
use function trim;
23
24
trait ViewTrait
0 ignored issues
show
Coding Style introduced by
Missing doc comment for trait ViewTrait
Loading history...
25
{
26
    /**
27
     * Register the values into the container
28
     *
29
     * @return void
30
     */
31
    private function registerViews()
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines before function; 0 found
Loading history...
32
    {
33
        // Jaxon template view
34
        $this->set(TemplateView::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

34
        $this->/** @scrutinizer ignore-call */ 
35
               set(TemplateView::class, function($di) {
Loading history...
35
            return new TemplateView($di->g(TemplateEngine::class));
36
        });
37
        // View Renderer
38
        $this->set(ViewRenderer::class, function($di) {
39
            $xViewRenderer = new ViewRenderer($di->g(Container::class));
40
            // Add the default view renderer
41
            $xViewRenderer->addRenderer('jaxon', function($di) {
42
                return $di->g(TemplateView::class);
43
            });
44
            $sTemplateDir = rtrim(trim($di->g('jaxon.core.dir.template')), '/\\');
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 3 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
45
            $sPaginationDir = $sTemplateDir . DIRECTORY_SEPARATOR . 'pagination';
46
            // By default, render pagination templates with Jaxon.
47
            $xViewRenderer->addNamespace('jaxon', $sTemplateDir, '.php', 'jaxon');
48
            $xViewRenderer->addNamespace('pagination', $sPaginationDir, '.php', 'jaxon');
49
            return $xViewRenderer;
50
        });
51
52
        // Dialog library manager
53
        $this->set(DialogLibraryManager::class, function($di) {
54
            return new DialogLibraryManager($di->g(Container::class), $di->g(ConfigManager::class), $di->g(Translator::class));
55
        });
56
        $this->set(DialogManager::class, function($di) {
57
            return new DialogManager($di->g(DialogLibraryManager::class));
58
        });
59
        $this->val(AlertLibrary::class, new AlertLibrary());
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

59
        $this->/** @scrutinizer ignore-call */ 
60
               val(AlertLibrary::class, new AlertLibrary());
Loading history...
60
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
61
62
    /**
63
     * Register a javascript dialog library adapter.
64
     *
65
     * @param string $sClass
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
66
     * @param string $sLibraryName
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
67
     *
68
     * @return void
69
     */
70
    public function registerDialogLibrary(string $sClass, string $sLibraryName)
71
    {
72
        $this->set($sClass, function($di) use($sClass) {
73
            // Set the protected attributes of the library
74
            $cSetter = function() use($di) {
75
                $this->xHelper = new DialogLibraryHelper($this, $di->g(ConfigManager::class), $di->g(TemplateEngine::class));
0 ignored issues
show
Bug Best Practice introduced by
The property xHelper does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
Bug introduced by
$this of type Jaxon\Di\Traits\ViewTrait is incompatible with the type Jaxon\App\Dialog\LibraryInterface expected by parameter $xDialogLibrary of Jaxon\App\Dialog\Library...ryHelper::__construct(). ( Ignorable by Annotation )

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

75
                $this->xHelper = new DialogLibraryHelper(/** @scrutinizer ignore-type */ $this, $di->g(ConfigManager::class), $di->g(TemplateEngine::class));
Loading history...
76
            };
77
            // Can now access protected attributes
78
            $xLibrary = $di->make($sClass);
79
            call_user_func($cSetter->bindTo($xLibrary, $xLibrary));
80
            return $xLibrary;
81
        });
82
        // Set the alias, so the libraries can be found by their names.
83
        $this->alias("dialog_library_$sLibraryName", $sClass);
0 ignored issues
show
Bug introduced by
It seems like alias() 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

83
        $this->/** @scrutinizer ignore-call */ 
84
               alias("dialog_library_$sLibraryName", $sClass);
Loading history...
84
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
85
86
    /**
87
     * Get a dialog library
88
     *
89
     * @param string $sLibraryName
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
90
     *
91
     * @return LibraryInterface
92
     */
93
    public function getDialogLibrary(string $sLibraryName): LibraryInterface
94
    {
95
        return $this->g("dialog_library_$sLibraryName");
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

95
        return $this->/** @scrutinizer ignore-call */ g("dialog_library_$sLibraryName");
Loading history...
96
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
97
98
    /**
99
     * Get the QuestionInterface library
100
     *
101
     * @param string $sLibraryName
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
102
     *
103
     * @return QuestionInterface
104
     */
105
    public function getQuestionLibrary(string $sLibraryName): QuestionInterface
106
    {
107
        $sKey = "dialog_library_$sLibraryName";
108
        return $this->h($sKey) ? $this->g($sKey) : $this->g(AlertLibrary::class);
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

108
        return $this->/** @scrutinizer ignore-call */ h($sKey) ? $this->g($sKey) : $this->g(AlertLibrary::class);
Loading history...
109
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
110
111
    /**
112
     * Get the MessageInterface library
113
     *
114
     * @param string $sLibraryName
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
115
     *
116
     * @return MessageInterface
117
     */
118
    public function getMessageLibrary(string $sLibraryName): MessageInterface
119
    {
120
        $sKey = "dialog_library_$sLibraryName";
121
        return $this->h($sKey) ? $this->g($sKey) : $this->g(AlertLibrary::class);
122
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
123
124
    /**
125
     * Get the ModalInterface library
126
     *
127
     * @param string $sLibraryName
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
128
     *
129
     * @return ModalInterface|null
130
     */
131
    public function getModalLibrary(string $sLibraryName): ?ModalInterface
132
    {
133
        $sKey = "dialog_library_$sLibraryName";
134
        return $this->h($sKey) ? $this->g($sKey) : null;
135
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
136
137
    /**
138
     * Get the dialog library manager
139
     *
140
     * @return DialogLibraryManager
141
     */
142
    public function getDialogLibraryManager(): DialogLibraryManager
143
    {
144
        return $this->g(DialogLibraryManager::class);
145
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
146
147
    /**
148
     * Get the dialog manager
149
     *
150
     * @return DialogManager
151
     */
152
    public function getDialogManager(): DialogManager
153
    {
154
        return $this->g(DialogManager::class);
155
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
156
157
    /**
158
     * Get the view renderer
159
     *
160
     * @return ViewRenderer
161
     */
162
    public function getViewRenderer(): ViewRenderer
163
    {
164
        return $this->g(ViewRenderer::class);
165
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 0 found
Loading history...
166
}
167