Completed
Pull Request — master (#116)
by Janis
09:43
created

Application::__construct()   B

Complexity

Conditions 2
Paths 1

Size

Total Lines 168
Code Lines 84

Duplication

Lines 28
Ratio 16.67 %

Code Coverage

Tests 96
CRAP Score 2.0004

Importance

Changes 0
Metric Value
dl 28
loc 168
ccs 96
cts 101
cp 0.9505
rs 8.2857
c 0
b 0
f 0
cc 2
eloc 84
nc 1
nop 1
crap 2.0004

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * Nextcloud - OCR
5
 * This file is licensed under the Affero General Public License version 3 or
6
 * later.
7
 * See the COPYING file.
8
 * 
9
 * @author Janis Koehr <[email protected]>
10
 * @copyright Janis Koehr 2017
11
 */
12
namespace OCA\Ocr\AppInfo;
13
14
use OC\Files\View;
15
use OCA\Ocr\Controller\JobController;
16
use OCA\Ocr\Controller\PersonalSettingsController;
17
use OCA\Ocr\Db\FileMapper;
18
use OCA\Ocr\Db\OcrJobMapper;
19
use OCA\Ocr\Db\ShareMapper;
20
use OCA\Ocr\Service\JobService;
21
use OCP\AppFramework\App;
22
use OCP\AppFramework\IAppContainer;
23
use OCP\IContainer;
24
use OCA\Ocr\Controller\StatusController;
25
use OCA\Ocr\Service\StatusService;
26
use OCA\Ocr\Service\FileService;
27
use OCA\Ocr\Service\RedisService;
28
use OCA\Ocr\Service\AppConfigService;
29
use OCA\Ocr\Util\PHPUtil;
30
use OCA\Ocr\Util\FileUtil;
31
use OCA\Ocr\Util\RedisUtil;
32
use OCA\Ocr\Hooks\UserHooks;
33
34
35
/**
36
 * Class Application
37
 * 
38
 * @package OCA\Ocr\AppInfo
39
 */
40
class Application extends App {
41
42
    /**
43
     * Application constructor.
44
     * 
45
     * @param array $urlParams            
46
     */
47 16
    public function __construct(array $urlParams = array()) {
48 16
        parent::__construct('ocr', $urlParams);
49 16
        $container = $this->getContainer();
50
        /**
51
         * Add the js and style if OCA\Files app is loaded
52
         */
53 16
        $eventDispatcher = \OC::$server->getEventDispatcher();
54 16
        $eventDispatcher->addListener('OCA\Files::loadAdditionalScripts', 
55 16
                function () {
56
                    script('ocr', 'dist/ocrapp');
57
                    style('ocr', 'ocrstyle');
58
                    // if not loaded before - load select2 for multi select languages
59
                    vendor_script('select2/select2');
60
                    vendor_style('select2/select2');
61 16
                });
62
        /**
63
         * Register core services
64
         */
65 16
        $container->registerService('CurrentUID', 
66 16
                function (IContainer $c) {
67
                    /** @var \OC\Server $server */
68 5
                    $server = $c->query('ServerContainer');
69 5
                    $user = $server->getUserSession()
70 5
                        ->getUser();
71 5
                    return ($user) ? $user->getUID() : '';
72 16
                });
73
        // Allow automatic DI for the View, until they migrated to Nodes API
74 16
        $container->registerService(View::class, function () {
75
            return new View('');
76 16
        }, false);
77
        /**
78
         * Register the PHPUtil
79
         */
80 16
        $container->registerService('PHPUtil', 
81 16
                function (IContainer $c) {
82
                    /** @var \OC\Server $server */
83 5
                    $server = $c->query('ServerContainer');
84 5
                    return new PHPUtil($server->getL10N('ocr'));
85 16
                });
86
        /**
87
         * Register the RedisUtil
88
         */
89 16
        $container->registerService('RedisUtil', 
90 16
                function (IContainer $c) {
91
                    /** @var \OC\Server $server */
92 7
                    $server = $c->query('ServerContainer');
93 7
                    return new RedisUtil($server->getL10N('ocr'), $server->getLogger(), $server->getConfig());
94 16
                });
95
        /**
96
         * Register the FileUtil
97
         */
98 16
        $container->registerService('FileUtil', 
99 16
                function (IContainer $c) {
0 ignored issues
show
Unused Code introduced by
The parameter $c is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
100 7
                    return new FileUtil();
101 16
                });
102
        /**
103
         * Register the Ocr Job mapper
104
         */
105 16
        $container->registerService('OcrJobMapper', 
106 16
                function (IContainer $c) {
107
                    /** @var \OC\Server $server */
108 7
                    $server = $c->query('ServerContainer');
109 7
                    return new OcrJobMapper($server->getDatabaseConnection());
110 16
                });
111
        /**
112
         * Register the File mapper
113
         */
114 16
        $container->registerService('FileMapper', 
115 16
                function (IContainer $c) {
116
                    /** @var \OC\Server $server */
117 6
                    $server = $c->query('ServerContainer');
118 6
                    return new FileMapper($server->getDatabaseConnection());
119 16
                });
120
        /**
121
         * Register the Share mapper
122
         */
123 16
        $container->registerService('ShareMapper', 
124 16
                function (IContainer $c) {
125
                    /** @var \OC\Server $server */
126 6
                    $server = $c->query('ServerContainer');
127 6
                    return new ShareMapper($server->getDatabaseConnection());
128 16
                });
129
        /**
130
         * Register the App Config Service
131
         */
132 16
        $container->registerService('AppConfigService', 
133 16
                function (IAppContainer $c) {
134
                    /** @var \OC\Server $server */
135 5
                    $server = $c->query('ServerContainer');
136 5
                    return new AppConfigService($server->getConfig(), $server->getL10N('ocr'), $c->query('RedisUtil'), 
137 5
                            $server->getLogger());
138 16
                });
139
        /**
140
         * Register the Job Service
141
         */
142 16
        $container->registerService('FileService', 
143 16 View Code Duplication
                function (IAppContainer $c) {
144
                    /** @var \OC\Server $server */
145 5
                    $server = $c->query('ServerContainer');
146 5
                    return new FileService($server->getL10N('ocr'), $server->getLogger(), $c->query('CurrentUID'), 
147 5
                            $c->query('FileMapper'), $c->query('ShareMapper'), $c->query('FileUtil'));
148 16
                });
149
        /**
150
         * Register the Redis Service
151
         */
152 16
        $container->registerService('RedisService', 
153 16 View Code Duplication
                function (IAppContainer $c) {
154
                    /** @var \OC\Server $server */
155 5
                    $server = $c->query('ServerContainer');
156 5
                    return new RedisService($c->query('OcrJobMapper'), $c->query('FileUtil'), $c->query('RedisUtil'), 
157 5
                            $server->getL10N('ocr'), $server->getLogger());
158 16
                });
159
        /**
160
         * Register the Job Service
161
         */
162 16
        $container->registerService('JobService', 
163 16
                function (IAppContainer $c) {
164
                    /** @var \OC\Server $server */
165 4
                    $server = $c->query('ServerContainer');
166 4
                    return new JobService($server->getL10N('ocr'), $server->getLogger(), $c->query('CurrentUID'), 
167 4
                            new View('/' . $c->query('CurrentUID') . '/files'), $server->getTempManager(), 
168 4
                            $c->query('RedisService'), $c->query('OcrJobMapper'), $c->query('FileService'), 
169 4
                            $c->query('AppConfigService'), $c->query('PHPUtil'), $c->query('FileUtil'));
170 16
                });
171
        /**
172
         * Register the Status Service
173
         */
174 16
        $container->registerService('StatusService', 
175 16 View Code Duplication
                function (IAppContainer $c) {
176
                    /** @var \OC\Server $server */
177 2
                    $server = $c->query('ServerContainer');
178 2
                    return new StatusService($server->getL10N('ocr'), $server->getLogger(), $c->query('CurrentUID'), 
179 2
                            $c->query('OcrJobMapper'), $c->query('JobService'));
180 16
                });
181
        /**
182
         * Controller
183
         */
184 16
        $container->registerService('StatusController', 
185 16 View Code Duplication
                function (IAppContainer $c) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
186
                    /** @var \OC\Server $server */
187 1
                    $server = $c->query('ServerContainer');
188 1
                    return new StatusController($c->getAppName(), $server->getRequest(), $c->query('StatusService'));
189 16
                });
190
        /**
191
         * Controller
192
         */
193 16
        $container->registerService('JobController', 
194 16 View Code Duplication
                function (IAppContainer $c) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
195
                    /** @var \OC\Server $server */
196 1
                    $server = $c->query('ServerContainer');
197 1
                    return new JobController($c->getAppName(), $server->getRequest(), $c->query('JobService'), 
198 1
                            $c->query('CurrentUID'));
199 16
                });
200
        /**
201
         * Controller
202
         */
203 16
        $container->registerService('UserHooks', 
204 16
                function (IAppContainer $c) {
205
                    /** @var \OC\Server $server */
206 1
                    $server = $c->query('ServerContainer');
207 1
                    return new UserHooks($c->query('ServerContainer')
208 1
                        ->getUserManager(), $c->query('OcrJobMapper'), $server->getLogger());
209 16
                });
210
        /**
211
         * Controller
212
         */
213 16
        $container->registerAlias('PersonalSettingsController', PersonalSettingsController::class);
214 16
    }
215
216
    /**
217
     * Registers the Personal Settings Page for deletion of status objects and such things.
218
     * @codeCoverageIgnore
219
     */
220
    public function registerPersonal() {
221
        \OCP\App::registerPersonal($this->getContainer()
222
            ->getAppName(), 'personal');
223
    }
224
225
    /**
226
     * Registers the User Hooks.
227
     * @codeCoverageIgnore
228
     */
229
    public function registerHooks() {
230
        $this->getContainer()
231
            ->query('UserHooks')
232
            ->register();
233
    }
234
}