ViewTrait   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 147
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 12
eloc 35
c 1
b 0
f 0
dl 0
loc 147
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A getDialogLibraryManager() 0 3 1
A getQuestionLibrary() 0 4 2
A getDialogLibrary() 0 3 1
A getPaginator() 0 3 1
A getViewRenderer() 0 3 1
A getModalLibrary() 0 4 2
A getMessageLibrary() 0 4 2
A registerDialogLibrary() 0 14 1
A registerViews() 0 35 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
        // Jaxon template view
35
        $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

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

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

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

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

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

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