Passed
Push — master ( ce3136...58f09e )
by Alexander
02:19
created

MigrationCreator::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
3
4
namespace Quantick\DeployMigration\Lib\Service;
5
6
7
use Illuminate\Contracts\Filesystem\FileNotFoundException;
8
use Illuminate\Filesystem\Filesystem;
9
10
class MigrationCreator
11
{
12
    /**
13
     * @var Filesystem
14
     */
15
    private $filesystem;
16
    /**
17
     * @var Config
18
     */
19
    private $config;
20
21
    /**
22
     * MigrationCreator constructor.
23
     * @param Filesystem $filesystem
24
     * @param Config $config
25
     */
26
    public function __construct(
27
        Filesystem $filesystem,
28
        Config $config
29
    )
30
    {
31
        $this->filesystem = $filesystem;
32
        $this->config     = $config;
33
    }
34
35
36
    /**
37
     * return version path
38
     * @return string
39
     * @throws FileNotFoundException
40
     */
41
    public function create(): string
42
    {
43
        $migrationTemplate = $this->filesystem->get(__DIR__ . '/stub/DeployMigration.stub');
44
        $version           = $this->getVersionNumber();
45
        $migrationContent  = $this->prepareTemplate($migrationTemplate, [
46
            '{version}' => $version
47
        ]);
48
        $versionFile       = sprintf('Version%s.php', $version);
49
        $versionPath       = sprintf('%s/%s', $this->config->getMigrationsPath(), $versionFile);
0 ignored issues
show
Bug introduced by
It seems like $this->config->getMigrationsPath() can also be of type Illuminate\Config\Repository; however, parameter $args of sprintf() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

49
        $versionPath       = sprintf('%s/%s', /** @scrutinizer ignore-type */ $this->config->getMigrationsPath(), $versionFile);
Loading history...
50
51
        $this->filesystem->put($versionPath, $migrationContent);
52
53
        return $versionPath;
54
    }
55
56
    /**
57
     * @return string
58
     */
59
    private function getVersionNumber(): string
60
    {
61
        return date('Ymdhis');
62
    }
63
64
    private function prepareTemplate(string $templateContent, array $replaces)
65
    {
66
        return str_replace(array_keys($replaces), array_values($replaces), $templateContent);
67
    }
68
}