ConfigWriter::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 1
1
<?php
2
namespace Spekkionu\Assetcachebuster\Writer;
3
4
use Illuminate\Filesystem\Filesystem;
5
6
class ConfigWriter implements WriterInterface
7
{
8
    /**
9
     * @var Filesystem
10
     */
11
    private $filesystem;
12
    /**
13
     * @var
14
     */
15
    private $config_path;
16
17
    /**
18
     * ConfigWriter constructor.
19
     * @param Filesystem $filesystem
20
     * @param string $config_path
21
     */
22 4
    public function __construct(Filesystem $filesystem, $config_path)
23
    {
24 4
        $this->filesystem = $filesystem;
25 4
        $this->config_path = $config_path;
26 4
    }
27
28
    /**
29
     * Get the key file and contents.
30
     *
31
     * @return array
32
     */
33 4
    protected function getConfigFile()
34
    {
35 4
        return $this->config_path . DIRECTORY_SEPARATOR . "assetcachebuster.php";
36
    }
37
38
    /**
39
     * Check if the config file exists
40
     * 
41
     * @return bool
42
     */
43 4
    protected function configExists()
44
    {
45 4
        return $this->filesystem->exists($this->getConfigFile());
46
    }
47
48
    /**
49
     * Returns current config as string
50
     *
51
     * @return string
52
     */
53 2
    public function getCurrentConfig()
54
    {
55 2
        if (!$this->configExists()) {
56 1
            throw new \InvalidArgumentException('The config file does not exist. Did you run the vendor:publish command?');
57
        }
58 1
        return $this->filesystem->get($this->getConfigFile());
59
    }
60
61
    /**
62
     * Saves config as current config
63
     * @param string $content
64
     * @return void
65
     */
66 2
    public function setCurrentConfig($content)
67
    {
68 2
        if (!$this->configExists()) {
69 1
            throw new \InvalidArgumentException('The config file does not exist. Did you run the vendor:publish command?');
70
        }
71 1
        $this->filesystem->put($this->getConfigFile(), $content);
72 1
    }
73
}
74