FolderApiController::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 10
nc 1
nop 5
1
<?php
2
/**
3
 * ownCloud - News
4
 *
5
 * This file is licensed under the Affero General Public License version 3 or
6
 * later. See the COPYING file.
7
 *
8
 * @author Alessandro Cosentino <[email protected]>
9
 * @author Bernhard Posselt <[email protected]>
10
 * @copyright Alessandro Cosentino 2012
11
 * @copyright Bernhard Posselt 2012, 2014
12
 */
13
14
namespace OCA\News\Controller;
15
16
use \OCP\IRequest;
17
use \OCP\AppFramework\ApiController;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, OCA\News\Controller\ApiController.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
18
use \OCP\AppFramework\Http;
19
20
use \OCA\News\Service\FolderService;
21
use \OCA\News\Service\ItemService;
22
use \OCA\News\Service\ServiceNotFoundException;
23
use \OCA\News\Service\ServiceConflictException;
24
use \OCA\News\Service\ServiceValidationException;
25
26
27
class FolderApiController extends ApiController {
28
29
    use JSONHttpError;
30
31
    private $folderService;
32
    private $itemService;
33
    private $userId;
34
    private $serializer;
35
36
    public function __construct($AppName,
37
                                IRequest $request,
38
                                FolderService $folderService,
39
                                ItemService $itemService,
40
                                $UserId){
41
        parent::__construct($AppName, $request);
42
        $this->folderService = $folderService;
43
        $this->itemService = $itemService;
44
        $this->userId = $UserId;
45
        $this->serializer = new EntityApiSerializer('folders');
46
    }
47
48
49
    /**
50
     * @NoAdminRequired
51
     * @NoCSRFRequired
52
     * @CORS
53
     */
54
    public function index() {
55
        return $this->serializer->serialize(
56
            $this->folderService->findAll($this->userId)
57
        );
58
    }
59
60
61
    /**
62
     * @NoAdminRequired
63
     * @NoCSRFRequired
64
     * @CORS
65
     *
66
     * @param string $name
67
     * @return array|mixed|\OCP\AppFramework\Http\JSONResponse
68
     */
69
    public function create($name) {
70
        try {
71
            $this->folderService->purgeDeleted($this->userId, false);
72
            return $this->serializer->serialize(
73
                $this->folderService->create($name, $this->userId)
74
            );
75
        } catch(ServiceValidationException $ex) {
76
            return $this->error($ex, Http::STATUS_UNPROCESSABLE_ENTITY);
77
        } catch(ServiceConflictException $ex) {
78
            return $this->error($ex, Http::STATUS_CONFLICT);
79
        }
80
    }
81
82
83
    /**
84
     * @NoAdminRequired
85
     * @NoCSRFRequired
86
     * @CORS
87
     *
88
     * @param int $folderId
89
     * @return array|\OCP\AppFramework\Http\JSONResponse
90
     */
91
    public function delete($folderId) {
92
        try {
93
            $this->folderService->delete($folderId, $this->userId);
94
        } catch(ServiceNotFoundException $ex) {
95
            return $this->error($ex, Http::STATUS_NOT_FOUND);
96
        }
97
98
        return [];
99
    }
100
101
102
    /**
103
     * @NoAdminRequired
104
     * @NoCSRFRequired
105
     * @CORS
106
     * @param int $folderId
107
     * @param string $name
108
     * @return array|\OCP\AppFramework\Http\JSONResponse
109
     */
110
    public function update($folderId, $name) {
111
        try {
112
            $this->folderService->rename($folderId, $name, $this->userId);
113
114
        } catch(ServiceValidationException $ex) {
115
            return $this->error($ex, Http::STATUS_UNPROCESSABLE_ENTITY);
116
        } catch(ServiceConflictException $ex) {
117
            return $this->error($ex, Http::STATUS_CONFLICT);
118
        } catch(ServiceNotFoundException $ex) {
119
            return $this->error($ex, Http::STATUS_NOT_FOUND);
120
        }
121
122
        return [];
123
    }
124
125
126
    /**
127
     * @NoAdminRequired
128
     * @NoCSRFRequired
129
     * @CORS
130
     *
131
     * @param int $folderId
132
     * @param int $newestItemId
133
     */
134
    public function read($folderId, $newestItemId) {
135
        $this->itemService->readFolder($folderId, $newestItemId, $this->userId);
136
    }
137
138
139
}
140