Completed
Push — master ( c632d0...4a7120 )
by Peter
23:08
created

PhpFile   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 20%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
c 1
b 0
f 0
lcom 1
cbo 3
dl 0
loc 56
ccs 3
cts 15
cp 0.2
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A setSignal() 0 5 1
A read() 0 12 2
A write() 0 12 2
1
<?php
2
3
/**
4
 * This software package is licensed under `AGPL, Commercial` license[s].
5
 *
6
 * @package maslosoft/signals
7
 * @license AGPL, Commercial
8
 *
9
 * @copyright Copyright (c) Peter Maselkowski <[email protected]>
10
 *
11
 */
12
13
namespace Maslosoft\Signals\Builder\IO;
14
15
use Maslosoft\Cli\Shared\Helpers\PhpExporter;
16
use Maslosoft\Signals\Interfaces\BuilderIOInterface;
17
use Maslosoft\Signals\Signal;
18
19
/**
20
 * PhpFileOutput
21
 *
22
 * @author Piotr Maselkowski <pmaselkowski at gmail.com>
23
 */
24
class PhpFile implements BuilderIOInterface
25
{
26
27
	/**
28
	 * Generated path.
29
	 * This is path, where signals definition will be stored.
30
	 * Path is relative to project root.
31
	 * @var string
32
	 */
33
	public $generatedPath = 'generated';
34
35
	/**
36
	 * File name for file containing signals definitions.
37
	 * @var string
38
	 */
39
	public $configFilename = 'signals-definition.php';
40
41
	/**
42
	 * Signal instance
43
	 * @var Signal
44
	 */
45
	private $signal = null;
46
47
	public function read()
48
	{
49
		$file = $this->generatedPath . '/' . $this->configFilename;
50
		if (file_exists($file))
51
		{
52
			return (array) require $file;
53
		}
54
		else
55
		{
56
			$this->signal->getLogger()->debug('Config file "{file}" does not exists, have you generated signals config file?', ['file' => $file]);
57
		}
58
	}
59
60 1
	public function setSignal(Signal $signal)
61
	{
62 1
		$this->signal = $signal;
63 1
		return $this;
64
	}
65
66
	public function write($data)
67
	{
68
		$path = sprintf("%s/%s", $this->generatedPath, $this->configFilename);
69
70
		// Use dirname here in case configFilename contains dir
71
		$dir = dirname($path);
72
		if (!is_dir($dir))
73
		{
74
			mkdir($dir, 0777, true);
75
		}
76
		return file_put_contents($path, PhpExporter::export($data, 'Auto generated, any changes will be lost'));
77
	}
78
79
}
80