Dispatcher::__construct()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 4
Bugs 0 Features 0
Metric Value
dl 0
loc 14
ccs 0
cts 14
cp 0
rs 9.4285
c 4
b 0
f 0
cc 2
eloc 12
nc 2
nop 1
crap 6
1
<?php
2
/**
3
 * @author Thomas Tanghus
4
 * @copyright 2013-2014 Thomas Tanghus ([email protected])
5
 *
6
 * This file is licensed under the Affero General Public License version 3 or
7
 * later.
8
 * See the COPYING-README file.
9
 */
10
11
namespace OCA\Contacts;
12
13
use OCP\AppFramework\App as MainApp;
14
use OCP\AppFramework\Http;
15
use OCP\AppFramework\IAppContainer;
16
use OCA\Contacts\Middleware\Http as HttpMiddleware;
17
use OCA\Contacts\Controller\PageController;
18
use OCA\Contacts\Controller\AddressBookController;
19
use OCA\Contacts\Controller\BackendController;
20
use OCA\Contacts\Controller\GroupController;
21
use OCA\Contacts\Controller\ContactController;
22
use OCA\Contacts\Controller\ContactPhotoController;
23
use OCA\Contacts\Controller\SettingsController;
24
use OCA\Contacts\Controller\ImportController;
25
use OCA\Contacts\Controller\ExportController;
26
27
/**
28
 * This class manages our app actions
29
 *
30
 * TODO: Build app properly on basis of AppFramework
31
 */
32
class Dispatcher extends MainApp {
33
34
	/**
35
	 * @var string
36
	 */
37
	protected $appName;
38
39
	/**
40
	* @var \OCA\Contacts\App
41
	*/
42
	protected $app;
43
44
	/**
45
	* @var \OCP\IServerContainer
46
	*/
47
	protected $server;
48
49
	/**
50
	* @var \OCP\AppFramework\IAppContainer
51
	*/
52
	protected $container;
53
54
	public function __construct($params) {
55
		$this->appName = 'contacts';
56
		parent::__construct($this->appName, $params);
57
		$this->container = $this->getContainer();
58
		$this->server = $this->container->getServer();
59
		$user = \OC::$server->getUserSession()->getUser();
60
		if (is_null($user)) {
61
			\OC_Util::redirectToDefaultPage();
62
		}
63
		$userId = $user->getUID();
64
		$this->app = new App($userId);
65
		$this->registerServices();
66
		$this->container->registerMiddleware('HttpMiddleware');
67
	}
68
69
	public function registerServices() {
70
		$app = $this->app;
71
		$appName = $this->appName;
72
73
		$this->container->registerService('HttpMiddleware', function($container) {
0 ignored issues
show
Unused Code introduced by
The parameter $container 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...
74
			return new HttpMiddleware();
75
		});
76
77
		$this->container->registerService('PageController', function(IAppContainer $container) use($app, $appName) {
78
			$request = $container->query('Request');
79
			return new PageController($appName, $request);
80
		});
81
		$this->container->registerService('AddressBookController', function(IAppContainer $container) use($app, $appName) {
82
			$request = $container->query('Request');
83
			$userId = \OC::$server->getUserSession()->getUser()->getUID();
84
			return new AddressBookController($appName, $request, $app, $userId);
85
		});
86
		$this->container->registerService('BackendController', function(IAppContainer $container) use($app, $appName) {
87
			$request = $container->query('Request');
88
			return new BackendController($container, $request, $app);
89
		});
90 View Code Duplication
		$this->container->registerService('GroupController', function(IAppContainer $container) use($app, $appName) {
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...
91
			$request = $container->query('Request');
92
			$tags = $container->getServer()->getTagManager()->load('contact', array(), true);
93
			return new GroupController($appName, $request, $app, $tags);
94
		});
95
		$this->container->registerService('ContactController', function(IAppContainer $container) use($app, $appName) {
96
			$request = $container->query('Request');
97
			return new ContactController($appName, $request, $app);
98
		});
99 View Code Duplication
		$this->container->registerService('ContactPhotoController', function(IAppContainer $container) use($app, $appName) {
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...
100
			$request = $container->query('Request');
101
			$cache = $container->getServer()->getCache();
102
			return new ContactPhotoController($appName, $request, $app, $cache);
103
		});
104
		$this->container->registerService('SettingsController', function(IAppContainer $container) use($app, $appName) {
105
			$request = $container->query('Request');
106
			return new SettingsController($appName, $request, $app);
107
		});
108
		$this->container->registerService('ImportController', function(IAppContainer $container) use($app, $appName) {
109
			$request = $container->query('Request');
110
			$cache = $container->getServer()->getCache();
111
			$tags = $container->getServer()->getTagManager()->load('contact');
112
			return new ImportController($appName, $request, $app, $cache, $tags);
113
		});
114
		$this->container->registerService('ExportController', function(IAppContainer $container) use($app, $appName) {
115
			$request = $container->query('Request');
116
			return new ExportController($appName, $request, $app);
117
		});
118
	}
119
120
}
121