Completed
Pull Request — master (#210)
by korelstar
35:08
created

PageController::index()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 27
rs 9.488
c 0
b 0
f 0
cc 2
nc 2
nop 0
1
<?php
2
/**
3
 * Nextcloud - Notes
4
 *
5
 * This file is licensed under the Affero General Public License version 3 or
6
 * later. See the COPYING file.
7
 *
8
 * @author Bernhard Posselt <[email protected]>
9
 * @copyright Bernhard Posselt 2012, 2014
10
 */
11
12
namespace OCA\Notes\Controller;
13
14
use OCP\AppFramework\Controller;
15
use OCP\AppFramework\Http\TemplateResponse;
16
use OCP\AppFramework\Http\ContentSecurityPolicy;
17
use OC\Encryption\Exceptions\DecryptionFailedException;
18
use OCP\IRequest;
19
use OCP\IConfig;
20
use OCP\IL10N;
21
use OCA\Notes\Service\NotesService;
22
use OCA\Notes\Service\NoteDoesNotExistException;
23
24
/**
25
 * Class PageController
26
 *
27
 * @package OCA\Notes\Controller
28
 */
29
class PageController extends Controller {
30
31
    /** @var NotesService */
32
    private $notesService;
33
    /** @var IConfig */
34
    private $settings;
35
    /** @var string */
36
    private $userId;
37
    /** @var string */
38
    private $l10n;
39
    /**
40
     * @param string $AppName
41
     * @param IRequest $request
42
     * @param NotesService $notesService
43
     * @param IConfig $settings
44
     * @param string $UserId
45
     * @param IL10N $l10n
46
     */
47
    public function __construct($AppName, IRequest $request, $UserId,
48
                                NotesService $notesService, IConfig $settings,  IL10N $l10n){
49
        parent::__construct($AppName, $request);
50
        $this->notesService = $notesService;
51
        $this->userId = $UserId;
52
        $this->settings = $settings;
53
        $this->l10n = $l10n;
0 ignored issues
show
Documentation Bug introduced by
It seems like $l10n of type object<OCP\IL10N> is incompatible with the declared type string of property $l10n.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
54
    }
55
56
57
    /**
58
     * @NoAdminRequired
59
     * @NoCSRFRequired
60
     *
61
     * @return TemplateResponse
62
     */
63
    public function index() {
64
        $lastViewedNote = (int) $this->settings->getUserValue($this->userId,
65
            $this->appName, 'notesLastViewedNote');
66
        // check if note exists
67
        try {
68
            $this->notesService->get($lastViewedNote, $this->userId);
69
            $errorMessage=null;
70
        } catch(\Exception $ex) {
71
            $lastViewedNote = 0;
72
            $errorMessage=$this->l10n->t('The last viewed note cannot be accessed. ').$ex->getMessage();
0 ignored issues
show
Bug introduced by
The method t cannot be called on $this->l10n (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
73
        }
74
75
        $response = new TemplateResponse(
76
            $this->appName,
77
            'main',
78
            [
79
                 'lastViewedNote'=>$lastViewedNote,
80
                 'errorMessage'=>$errorMessage
81
            ]
82
        );
83
84
        $csp = new ContentSecurityPolicy();
85
        $csp->addAllowedImageDomain('*');
86
        $response->setContentSecurityPolicy($csp);
87
88
        return $response;
89
    }
90
91
92
}