WorkingDirectory::createEnvironmentFile()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 10
c 0
b 0
f 0
ccs 6
cts 6
cp 1
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Tworzenieweb\SqlProvisioner\Filesystem;
4
5
use Symfony\Component\Filesystem\Filesystem;
6
use Symfony\Component\Finder\Finder;
7
8
/**
9
 * @author Luke Adamczewski
10
 * @package Tworzenieweb\SqlProvisioner\Filesystem
11
 */
12
class WorkingDirectory
13
{
14
    const DRAFT_CONTENT = <<<DRAFT
15
DATABASE_USER=[user]
16
DATABASE_PASSWORD=[password]
17
DATABASE_HOST=[host]
18
DATABASE_PORT=[port]
19
DATABASE_NAME=[database]
20
PROVISIONING_TABLE=changelog_database_deployments
21
PROVISIONING_TABLE_CANDIDATE_NUMBER_COLUMN=deploy_script_number
22
DRAFT;
23
    const FILE_SUFFIX = '.env';
24
25
    /** @var Filesystem */
26
    private $filesystem;
27
28
    /** @var string */
29
    private $currentDirectoryAbsolute;
30
31
    /** @var EnvironmentLoaderInterface */
32
    private $environmentLoader;
33
34
    /** @var CandidatesFinder */
35
    private $finder;
36
37
38
    /**
39
     * @param string                     $currentDirectory
40
     * @param CandidatesFinder           $finder
41
     * @param Filesystem                 $filesystem
42
     * @param EnvironmentLoaderInterface $environmentLoader
43
     */
44 7
    public function __construct($currentDirectory, CandidatesFinder $finder, Filesystem $filesystem, EnvironmentLoaderInterface $environmentLoader)
45
    {
46 7
        $this->filesystem = $filesystem;
47 7
        $this->currentDirectoryAbsolute = $this->buildAbsolutePath($currentDirectory);
48 7
        $this->finder = $finder;
49 7
        $this->environmentLoader = $environmentLoader;
50 7
    }
51
52
    /**
53
     * @param string $newPath
54
     * @return WorkingDirectory
55
     */
56 1
    public function cd($newPath)
57
    {
58 1
        return new WorkingDirectory($newPath, $this->finder, $this->filesystem, $this->environmentLoader);
59
    }
60
61
    /**
62
     * @return void
63
     */
64 1
    public function createEnvironmentFile()
65
    {
66 1
        $targetFilename = $this->currentDirectoryAbsolute . DIRECTORY_SEPARATOR . self::FILE_SUFFIX;
67 1
        $this->filesystem->dumpFile(
68 1
            $targetFilename,
69
            self::DRAFT_CONTENT
70 1
        );
71 1
    }
72
73
    /**
74
     * @return void
75
     */
76 1
    public function loadEnvironment()
77
    {
78 1
        $this->environmentLoader->load($this);
79 1
    }
80
81
    /**
82
     * @return Finder
83
     */
84 1
    public function getCandidates()
85
    {
86 1
        return $this->finder->find($this->currentDirectoryAbsolute);
87
    }
88
89
    /**
90
     * @return string
91
     */
92 3
    public function getCurrentDirectoryAbsolute()
93
    {
94 3
        return $this->currentDirectoryAbsolute;
95
    }
96
97
    /**
98
     * @return string
99
     */
100 1
    public function __toString()
101
    {
102 1
        return $this->currentDirectoryAbsolute;
103
    }
104
105
    /**
106
     * @param string $path
107
     * @return string
108
     */
109 7
    private function buildAbsolutePath($path)
110
    {
111 7
        $absolutePath = $path;
112
113 7
        if (!$this->filesystem->isAbsolutePath($path)) {
114
            $absolutePath = realpath($path);
115
        }
116
117 7
        return $absolutePath;
118
    }
119
}
120