Service   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 20
dl 0
loc 80
rs 10
c 0
b 0
f 0
wmc 8

4 Methods

Rating   Name   Duplication   Size   Complexity  
A validateNode() 0 3 3
A __construct() 0 8 1
A getNode() 0 7 2
A getFile() 0 9 2
1
<?php
2
/**
3
 * Nextcloud - Gallery
4
 *
5
 * This file is licensed under the Affero General Public License version 3 or
6
 * later. See the COPYING file.
7
 *
8
 * @author Olivier Paroz <[email protected]>
9
 *
10
 * @copyright Olivier Paroz 2017
11
 */
12
13
namespace OCA\Gallery\Service;
14
15
use OCP\Files\Node;
0 ignored issues
show
Bug introduced by
The type OCP\Files\Node was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
16
use OCP\ILogger;
0 ignored issues
show
Bug introduced by
The type OCP\ILogger was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
17
18
use OCA\Gallery\Environment\Environment;
19
20
/**
21
 * Contains methods which all services will need
22
 *
23
 * @package OCA\Gallery\Service
24
 */
25
abstract class Service {
26
27
	/**
28
	 * @var string
29
	 */
30
	protected $appName;
31
	/**
32
	 * @var Environment
33
	 */
34
	protected $environment;
35
	/**
36
	 * @var ILogger
37
	 */
38
	protected $logger;
39
40
	/**
41
	 * Constructor
42
	 *
43
	 * @param string $appName
44
	 * @param Environment $environment
45
	 * @param ILogger $logger
46
	 */
47
	public function __construct(
48
		$appName,
49
		Environment $environment,
50
		ILogger $logger
51
	) {
52
		$this->appName = $appName;
53
		$this->environment = $environment;
54
		$this->logger = $logger;
55
	}
56
57
	/**
58
	 * Returns the file matching the given ID
59
	 *
60
	 * @param int $nodeId ID of the resource to locate
61
	 *
62
	 * @return Node
63
	 * @throws NotFoundServiceException
64
	 */
65
	public function getFile($nodeId) {
66
		$node = $this->getNode($nodeId);
67
68
		if ($node->getType() === 'file') {
69
			$this->validateNode($node);
70
71
			return $node;
72
		} else {
73
			throw new NotFoundServiceException("Cannot find a file with this ID");
74
		}
75
	}
76
77
	/**
78
	 * Returns the node matching the given ID
79
	 *
80
	 * @param int $nodeId ID of the resource to locate
81
	 *
82
	 * @return Node
83
	 * @throws NotFoundServiceException
84
	 */
85
	private function getNode($nodeId) {
86
		try {
87
			$node = $this->environment->getResourceFromId($nodeId);
88
89
			return $node;
90
		} catch (\Exception $exception) {
91
			throw new NotFoundServiceException($exception->getMessage());
92
		}
93
	}
94
95
	/**
96
	 * Makes extra sure that we can actually do something with the file
97
	 *
98
	 * @param Node $node
99
	 *
100
	 * @throws NotFoundServiceException
101
	 */
102
	private function validateNode($node) {
103
		if (!$node->getMimetype() || !$node->isReadable()) {
104
			throw new NotFoundServiceException("Can't access the file");
105
		}
106
	}
107
108
}
109