Test Failed
Push — master ( dc798f...d76e2d )
by Daniel
48:22
created

PicoPage::getAbsolutePath()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 6
c 2
b 0
f 0
dl 0
loc 10
ccs 0
cts 9
cp 0
rs 10
cc 3
nc 2
nop 0
crap 12
1
<?php
2
/**
3
 * CMS Pico - Create websites using Pico CMS for Nextcloud.
4
 *
5
 * @copyright Copyright (c) 2019, Daniel Rudolf (<[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
declare(strict_types=1);
24
25
namespace OCA\CMSPico\Model;
26
27
use OCA\CMSPico\Service\MiscService;
28
use OCP\Files\InvalidPathException;
29
use Pico;
30
31
class PicoPage
32
{
33
	/** @var MiscService */
34
	private $miscService;
35
36
	/** @var Website */
37
	private $website;
38
39
	/** @var Pico */
40
	private $pico;
41
42
	/** @var string */
43
	private $output;
44
45
	/**
46
	 * PicoPage constructor.
47
	 *
48
	 * @param Website $website
49
	 * @param Pico    $pico
50
	 * @param string  $output
51
	 */
52
	public function __construct(Website $website, Pico $pico, string $output)
53
	{
54
		$this->miscService = \OC::$server->query(MiscService::class);
1 ignored issue
show
Documentation Bug introduced by
It seems like OC::server->query(OCA\CM...ice\MiscService::class) can also be of type stdClass. However, the property $miscService is declared as type OCA\CMSPico\Service\MiscService. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
Deprecated Code introduced by
The function OC\ServerContainer::query() has been deprecated: 20.0.0 use \Psr\Container\ContainerInterface::get ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

54
		$this->miscService = /** @scrutinizer ignore-deprecated */ \OC::$server->query(MiscService::class);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
55
56
		$this->website = $website;
57
		$this->pico = $pico;
58
		$this->output = $output;
59
	}
60
61
	/**
62
	 * @return string
63
	 */
64
	public function getAbsolutePath(): string
65
	{
66
		$requestFile = $this->pico->getRequestFile();
67
		if ($requestFile) {
68
			return $requestFile;
69
		}
70
71
		$contentDir = $this->pico->getConfig('content_dir');
72
		$contentExt = $this->pico->getConfig('content_ext');
73
		return $contentDir . ($this->website->getPage() ?: 'index') . $contentExt;
74
	}
75
76
	/**
77
	 * @return string
78
	 */
79
	public function getRelativePath(): string
80
	{
81
		$requestFile = $this->pico->getRequestFile();
82
		if ($requestFile) {
83
			try {
84
				$contentDir = $this->pico->getConfig('content_dir');
85
				$relativePath = $this->miscService->getRelativePath($requestFile, $contentDir);
86
87
				$contentExt = $this->pico->getConfig('content_ext');
88
				return $this->miscService->dropFileExtension($relativePath, $contentExt);
89
			} catch (InvalidPathException $e) {
90
				// fallback to the website's page
91
			}
92
		}
93
94
		return ($this->website->getPage() ?: 'index');
95
	}
96
97
	/**
98
	 * @return string
99
	 */
100
	public function getRawContent(): string
101
	{
102
		return $this->pico->getRawContent() ?? '';
103
	}
104
105
	/**
106
	 * @return array
107
	 */
108
	public function getMeta(): array
109
	{
110
		return $this->pico->getFileMeta() ?? [];
111
	}
112
113
	/**
114
	 * @return string
115
	 */
116
	public function getContent(): string
117
	{
118
		return $this->pico->getFileContent() ?? '';
119
	}
120
121
	/**
122
	 * @return bool
123
	 */
124
	public function is404Content(): bool
125
	{
126
		return $this->pico->is404Content();
127
	}
128
129
	/**
130
	 * @return string
131
	 */
132
	public function render(): string
133
	{
134
		return $this->output;
135
	}
136
}
137