Passed
Branch dev (a860f8)
by Alan
02:15
created

InvalidFileException::onFileLine()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 2
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace FigTree\Exceptions;
4
5
use Throwable;
6
use RuntimeException;
7
use FigTree\Exceptions\Contracts\{
8
	SevereExceptionInterface,
9
	LocatableExceptionInterface,
10
};
11
use FigTree\Exceptions\Concerns\HasSeverity;
12
13
/**
14
 * Exception thrown when a path is expected to be a file but is not a file.
15
 */
16
class InvalidFileException extends RuntimeException implements SevereExceptionInterface, LocatableExceptionInterface
17
{
18
	use HasSeverity;
19
20
	/**
21
	 * Exception thrown when a path is expected to be a file but is not a file.
22
	 *
23
	 * @param string $path The path being checked as a file.
24
	 * @param int $code The Exception code.
25
	 * @param \Throwable $previous The previous throwable used for the exception chaining.
26
	 */
27
	public function __construct(string $path, int $code = 0, Throwable $previous = null)
28
	{
29
		$this->severity = E_RECOVERABLE_ERROR;
30
31
		$message = sprintf('Path %s is not a file.', $path);
32
33
		parent::__construct($message, $code, $previous);
34
	}
35
36
	/**
37
	 * If required, set the file and line where the Exception was thrown.
38
	 *
39
	 * @param string $file
40
	 * @param int $line
41
	 *
42
	 * @return $this
43
	 */
44
	public function onFileLine(string $file, int $line): LocatableExceptionInterface
45
	{
46
		if (file_exists($file)) {
47
			$this->file = $file;
48
			$this->line = max(0, $line);
49
		}
50
51
		return $this;
52
	}
53
}
54