Completed
Pull Request — stable9 (#47)
by Olivier
08:29
created

Service   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 2
dl 0
loc 56
ccs 12
cts 12
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A getResourceFromId() 0 14 4
1
<?php
2
/**
3
 * ownCloud - galleryplus
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 2014-2015
11
 */
12
13
namespace OCA\GalleryPlus\Service;
14
15
use OCP\Files\Node;
16
use OCP\ILogger;
17
18
use OCA\GalleryPlus\Environment\Environment;
19
20
/**
21
 * Contains methods which all services will need
22
 *
23
 * @package OCA\GalleryPlus\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 108
	public function __construct(
48
		$appName,
49
		Environment $environment,
50
		ILogger $logger
51
	) {
52 108
		$this->appName = $appName;
53 108
		$this->environment = $environment;
54 108
		$this->logger = $logger;
55 108
	}
56
57
	/**
58
	 * Returns the node matching the given ID
59
	 *
60
	 * @param int $nodeId ID of the resource to locate
61
	 *
62
	 * @return Node
63
	 * @throws NotFoundServiceException
64
	 */
65 14
	public function getResourceFromId($nodeId) {
66
		try {
67 14
			$node = $this->environment->getResourceFromId($nodeId);
68
69
			// Making extra sure that we can actually do something with the file
70 11
			if (!$node->getMimetype() || !$node->isReadable()) {
71 1
				throw new NotFoundServiceException("Can't access the file");
72
			}
73
74 10
			return $node;
75 4
		} catch (\Exception $exception) {
76 4
			throw new NotFoundServiceException($exception->getMessage());
77
		}
78
	}
79
80
}
81