Passed
Push — master ( 382ef3...ddad46 )
by Thierry
03:13 queued 01:02
created

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

35
        $this->/** @scrutinizer ignore-call */ 
36
               set(ViewRenderer::class, function($c) {
Loading history...
36
            $xViewRenderer = new ViewRenderer($c->g(Container::class));
37
            // Add the default view renderer
38
            $xViewRenderer->addRenderer('jaxon', function($di) {
39
                return new TemplateView($di->g(TemplateEngine::class));
40
            });
41
            $sTemplateDir = rtrim(trim($c->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...
42
            $sPaginationDir = $sTemplateDir . DIRECTORY_SEPARATOR . 'pagination';
43
            // By default, render pagination templates with Jaxon.
44
            $xViewRenderer->addNamespace('jaxon', $sTemplateDir, '.php', 'jaxon');
45
            $xViewRenderer->addNamespace('pagination', $sPaginationDir, '.php', 'jaxon');
46
            return $xViewRenderer;
47
        });
48
49
        // Pagination Paginator
50
        $this->set(Paginator::class, function($c) {
51
            return new Paginator($c->g(PaginationRenderer::class));
52
        });
53
        // Pagination Renderer
54
        $this->set(PaginationRenderer::class, function($c) {
55
            return new PaginationRenderer($c->g(ViewRenderer::class));
56
        });
57
58
        // Dialog library manager
59
        $this->set(DialogLibraryManager::class, function($c) {
60
            return new DialogLibraryManager($c->g(Container::class), $c->g(ConfigManager::class), $c->g(Translator::class));
61
        });
62
        $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

62
        $this->/** @scrutinizer ignore-call */ 
63
               val(AlertLibrary::class, new AlertLibrary());
Loading history...
63
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
64
65
    /**
66
     * Register a javascript dialog library adapter.
67
     *
68
     * @param string $sClass
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
69
     * @param string $sLibraryName
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
70
     *
71
     * @return void
72
     */
73
    public function registerDialogLibrary(string $sClass, string $sLibraryName)
74
    {
75
        $this->set($sClass, function($c) use($sClass) {
76
            // Set the protected attributes of the library
77
            $cSetter = function() use($c) {
78
                $this->xHelper = new DialogLibraryHelper($this, $c->g(ConfigManager::class), $c->g(TemplateEngine::class));
0 ignored issues
show
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

78
                $this->xHelper = new DialogLibraryHelper(/** @scrutinizer ignore-type */ $this, $c->g(ConfigManager::class), $c->g(TemplateEngine::class));
Loading history...
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...
79
            };
80
            // Can now access protected attributes
81
            $xLibrary = $c->make($sClass);
82
            call_user_func($cSetter->bindTo($xLibrary, $xLibrary));
83
            return $xLibrary;
84
        });
85
        // Set the alias, so the libraries can be found by their names.
86
        $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

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

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

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