Completed
Push — master ( adedfa...b6430b )
by Julius
07:05
created

SettingsController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
ccs 6
cts 6
cp 1
rs 9.4285
cc 1
eloc 5
nc 1
nop 5
crap 1
1
<?php
2
3
namespace OCA\AppOrder\Controller;
4
5
use \OCP\AppFramework\Controller;
6
use \OCP\AppFramework\Http\TemplateResponse;
7
use \OCP\IRequest;
8
use \OCP\INavigationManager;
9
use \OCA\AppOrder\Service\ConfigService;
10
11
class SettingsController extends Controller {
12
13
    private $userId;
14
    private $l10n;
0 ignored issues
show
Unused Code introduced by
The property $l10n is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
15
    private $appConfig;
16
    private $navigationManager;
17
18 7
    public function __construct($appName, IRequest $request, ConfigService $appConfig, INavigationManager $navigationManager, $userId) {
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 80 characters; contains 136 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
19 7
        parent::__construct($appName, $request);
20 7
        $this->userId = $userId;
21 7
        $this->appConfig = $appConfig;
22 7
        $this->navigationManager = $navigationManager;
23 7
    }
24
25 1
    public function adminIndex() {
26 1
        $navigation = $this->navigationManager->getAll();
27 1
        $order = json_decode($this->appConfig->getAppValue('order'));
28 1
        $nav = $this->matchOrder($navigation, $order);
29 1
        return new TemplateResponse(
30 1
            $this->appName, 
31 1
            'admin',
32 1
            ["nav" => $nav],
33 1
            'blank'
34
        );
35
    }
36
37 3
    public function getAppOrder() {
38 3
        $order_user = $this->appConfig->getUserValue('order', $this->userId);
39 3
        $order_default = $this->appConfig->getAppValue('order');
40 3
        if ($order_user !== null && $order_user !== "")
41 2
            $order = $order_user;
42
        else
43 1
            $order = $order_default;
44 3
        return $order;
45
    }
46
47 2
    public function matchOrder($nav, $order) {
48 2
        $nav_tmp = array();
49 2
        $result = array();
50 2
        foreach ($nav as $app)
51 2
            $nav_tmp[$app['href']] = $app;
52 2
        foreach ($order as $app)
53 2
            $result[$app] = $nav_tmp[$app];
54 2
        foreach ($nav as $app)
55 2
            if (!array_key_exists($app['href'], $result))
56 2
                $result[$app['href']] = $app;
57 2
        return $result;
58
    }
59
60
    /**
61
     * @NoAdminRequired
62
     */
63 1
    public function getOrder() {
64 1
        $order = $this->getAppOrder();
65 1
        return array('status' => 'success', 'order' => $order);
66
    }
67
68
    /**
69
     * @NoAdminRequired
70
     */
71 1
    public function savePersonal($order) {
72 1
        $this->appConfig->setUserValue('order', $this->userId, $order);
73
        $response = array(
74 1
            'status' => 'success',
75
            'data' => array('message'=> 'User order saved successfully.'),
76 1
            'order' => $order
77
        );
78 1
        return $response;
79
    }
80
81 1
    public function saveDefaultOrder($order) {
82 1
        if (!is_null($order)) {
83 1
            $this->appConfig->setAppValue('order', $order);
84
        }
85 1
        return array('status' => 'success', 'order' => $order);
86
    }
87
}
88