Completed
Push — webcal-rebased ( 0ce56a...d08b4d )
by Thomas
20:00
created

SubscriptionsProxyController::getIcsFile()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 21
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 14
c 1
b 0
f 0
nc 8
nop 1
dl 0
loc 21
rs 9.0534
1
<?php
2
/**
3
 * ownCloud - Calendar App
4
 *
5
 * @author Georg Ehrke
6
 * @copyright 2016 Georg Ehrke <[email protected]>
7
 * @author Raghu Nayyar
8
 * @copyright 2016 Raghu Nayyar <[email protected]>
9
 *
10
 * This library is free software; you can redistribute it and/or
11
 * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
12
 * License as published by the Free Software Foundation; either
13
 * version 3 of the License, or any later version.
14
 *
15
 * This library is distributed in the hope that it will be useful,
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
 * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
19
 *
20
 * You should have received a copy of the GNU Affero General Public
21
 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
22
 *
23
 */
24
namespace OCA\Calendar\Controller;
25
26
use OC\AppFramework\Http;
27
use OCP\AppFramework\Controller;
28
use OCP\AppFramework\Http\DataDisplayResponse;
29
use OCP\Files\File;
30
use OCP\Files\IRootFolder;
31
use OCP\Files\NotFoundException;
32
use OCP\IRequest;
33
use OCP\IUserSession;
34
35
class SubscriptionsProxyController extends Controller {
36
37
	/**
38
	 * @var IUserSession
39
	 */
40
	private $userSession;
41
42
	/**
43
	 * @var \OCP\Files\Folder
44
	 */
45
	private $storage;
46
47
	/**
48
	 * @param string $appName
49
	 * @param IRequest $request an instance of the request
50
	 * @param IUserSession $userSession
51
	 * @param IRootFolder $storage
52
	 */
53
	public function __construct($appName, IRequest $request,
54
									IUserSession $userSession, IRootFolder $storage) {
55
		parent::__construct($appName, $request);
56
		$this->userSession = $userSession;
57
		$this->storage = $storage;
0 ignored issues
show
Documentation Bug introduced by
It seems like $storage of type object<OCP\Files\IRootFolder> is incompatible with the declared type object<OCP\Files\Folder> of property $storage.

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...
58
	}
59
60
	/**
61
	 * @NoAdminRequired
62
	 * @NoCSRFRequired
63
	 *
64
	 * @param string $icsurl
65
	 * @return DataDisplayResponse
66
	 */
67
	public function getIcsFile($icsurl) {
68
		try {
69
			$file = $this->storage->get('/' . basename($icsurl));
70
71
			if($file instanceof File) {
0 ignored issues
show
Bug introduced by
The class OCP\Files\File does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
72
				$date = new \DateTime();
73
				if ($date->getTimestamp() - $file->getMtime() > 3600) {
74
					$content = $this->fetchFile($file, $icsurl);
75
				} else {
76
					$content = $file->getContent();
77
				}
78
			}
79
		} catch (NotFoundException $e) {
0 ignored issues
show
Bug introduced by
The class OCP\Files\NotFoundException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
80
			$file = $this->createFile($this->userSession->getUser()->getUID(), $icsurl);
81
			$content = $this->fetchFile($file, $icsurl);
82
		}
83
84
		return new DataDisplayResponse($content, HTTP::STATUS_OK, [
0 ignored issues
show
Bug introduced by
The variable $content does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
85
			'content-type' => 'text/calendar',
86
		]);
87
	}
88
89
	/**
90
	 * @param File $file
91
	 * @param $icsurl
92
	 * @return null|string
93
	 */
94
	private function fetchFile(File $file, $icsurl) {
95
		$opts = array('http' => array('method' => 'GET', 'header' => "Content-Type: text/calendar\r\n", 'timeout' => 60));
96
		$context = stream_context_create($opts);
97
		$content = file_get_contents($icsurl, false, $context);
98
		$file->putContent($content);
99
		return $content;
100
	}
101
102
103
	/**
104
	 * @param int $userId
105
	 * @param string $url
106
	 * @return File
107
	 */
108
	private function createFile($userId, $url) {
109
		$folder = $this->getFolderForUser($userId);
110
		$file = $folder->newFile(basename($url));
111
		return $file;
112
	}
113
114
	/**
115
	 * @param $userId
116
	 * @return IRootFolder
117
	 */
118
	private function getFolderForUser($userId) {
119
		$path = '/' . $userId . '/files/CalendarSubscriptions';
120
		if ($this->storage->nodeExists($path)) {
121
			$folder = $this->storage->get($path);
122
		} else {
123
			$folder = $this->storage->newFolder($path);
124
		}
125
		return $folder;
126
	}
127
}