Passed
Push — master ( 2492b4...7a1b45 )
by Blizzz
15:22 queued 13s
created

FileName   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 24
c 1
b 0
f 0
dl 0
loc 59
rs 10
wmc 16

5 Methods

Rating   Name   Duplication   Size   Complexity  
B getActualValue() 0 16 10
A __construct() 0 4 1
A isAvailableForScope() 0 2 1
A supportedEntities() 0 2 1
A executeStringCheck() 0 6 3
1
<?php
2
3
declare(strict_types=1);
4
/**
5
 * @copyright Copyright (c) 2018 Daniel Kesselberg <[email protected]>
6
 *
7
 * @license GNU AGPL version 3 or any later version
8
 *
9
 * This program is free software: you can redistribute it and/or modify
10
 * it under the terms of the GNU Affero General Public License as
11
 * published by the Free Software Foundation, either version 3 of the
12
 * License, or (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU Affero General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU Affero General Public License
20
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
 *
22
 */
23
24
namespace OCA\WorkflowEngine\Check;
25
26
use OC\Files\Storage\Local;
27
use OCA\WorkflowEngine\Entity\File;
28
use OCP\Files\Mount\IMountManager;
29
use OCP\IL10N;
30
use OCP\IRequest;
31
use OCP\WorkflowEngine\IFileCheck;
32
33
class FileName extends AbstractStringCheck implements IFileCheck {
34
	use TFileCheck;
35
36
	/** @var IRequest */
37
	protected $request;
38
	/** @var IMountManager */
39
	private $mountManager;
40
41
	/**
42
	 * @param IL10N $l
43
	 * @param IRequest $request
44
	 */
45
	public function __construct(IL10N $l, IRequest $request, IMountManager $mountManager) {
46
		parent::__construct($l);
47
		$this->request = $request;
48
		$this->mountManager = $mountManager;
49
	}
50
51
	/**
52
	 * @return string
53
	 */
54
	protected function getActualValue(): string {
55
		$fileName = $this->path === null ? '' : basename($this->path);
56
		if ($fileName === '' && (!$this->storage->isLocal() || $this->storage->instanceOfStorage(Local::class))) {
57
			// Return the mountpoint name of external storages that are not mounted as user home
58
			$mountPoints = $this->mountManager->findByStorageId($this->storage->getId());
59
			if (empty($mountPoints) || $mountPoints[0]->getMountType() !== 'external') {
60
				return $fileName;
61
			}
62
			$mountPointPath = rtrim($mountPoints[0]->getMountPoint(), '/');
63
			$mountPointPieces = explode('/', $mountPointPath);
64
			$mountPointName = array_pop($mountPointPieces);
65
			if (!empty($mountPointName) && $mountPointName !== 'files' && count($mountPointPieces) !== 2) {
66
				return $mountPointName;
67
			}
68
		}
69
		return $fileName;
70
	}
71
72
	/**
73
	 * @param string $operator
74
	 * @param string $checkValue
75
	 * @param string $actualValue
76
	 * @return bool
77
	 */
78
	protected function executeStringCheck($operator, $checkValue, $actualValue): bool {
79
		if ($operator === 'is' || $operator === '!is') {
80
			$checkValue = mb_strtolower($checkValue);
81
			$actualValue = mb_strtolower($actualValue);
82
		}
83
		return parent::executeStringCheck($operator, $checkValue, $actualValue);
84
	}
85
86
	public function supportedEntities(): array {
87
		return [ File::class ];
88
	}
89
90
	public function isAvailableForScope(int $scope): bool {
91
		return true;
92
	}
93
}
94