UserApiController   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 0
dl 0
loc 50
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
B index() 0 29 3
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\IUserSession;
18
use \OCP\IURLGenerator;
19
use \OCP\Files\IRootFolder;
20
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...
21
use \OCP\AppFramework\Http;
22
23
class UserApiController extends ApiController {
24
25
    private $userSession;
26
    private $rootFolder;
27
28
    public function __construct($AppName,
29
                                IRequest $request,
30
                                IUserSession $userSession,
31
                                IRootFolder $rootFolder){
32
        parent::__construct($AppName, $request);
33
        $this->userSession = $userSession;
34
        $this->rootFolder = $rootFolder;
35
    }
36
37
    /**
38
     * @NoAdminRequired
39
     * @NoCSRFRequired
40
     * @CORS
41
     */
42
    public function index() {
43
        $user = $this->userSession->getUser();
44
45
        // find the avatar
46
        $jpgAvatar = '/' . $user->getUID() . '/avatar.jpg';
47
        $pngAvatar = '/' . $user->getUID() . '/avatar.png';
48
        $avatar = null;
49
50
        if ($this->rootFolder->nodeExists($jpgAvatar)) {
51
            $file = $this->rootFolder->get($jpgAvatar);
52
            $avatar = [
53
                'data' => base64_encode($file->getContent()),
54
                'mime' =>  'image/jpeg'
55
            ];
56
        } elseif ($this->rootFolder->nodeExists($pngAvatar)) {
57
            $file = $this->rootFolder->get($pngAvatar);
58
            $avatar = [
59
                'data' => base64_encode($file->getContent()),
60
                'mime' =>  'image/png'
61
            ];
62
        }
63
64
        return [
65
            'userId' => $user->getUID(),
66
            'displayName' => $user->getDisplayName(),
67
            'lastLoginTimestamp' => $user->getLastLogin(),
68
            'avatar' => $avatar
69
        ];
70
    }
71
72
}
73