Failed Conditions
Push — master ( ac05b5...2858d3 )
by Keoghan
02:49
created

PorterLibrary::dontMigrateAndSeedDatabase()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
namespace App;
4
5
use App\Support\FilePublisher;
6
use App\Support\Mechanics\ChooseMechanic;
7
use Illuminate\Contracts\Foundation\Application;
8
use Illuminate\Filesystem\Filesystem;
9
use Illuminate\Support\Facades\Artisan;
10
11
class PorterLibrary
12
{
13
    /** @var string */
14
    protected $path;
15
16
    /** @var Filesystem */
17
    protected $files;
18
19
    /** @var \App\Support\FilePublisher **/
20
    protected $filePublisher;
21
22
    protected $shouldMigrateAndSeedDatabase = true;
23
24 54
    public function __construct(FilePublisher $filePublisher, $path)
25
    {
26 54
        $this->filePublisher = $filePublisher;
27 54
        $this->files = $filePublisher->getFilesystem();
28 54
        $this->path = $path;
29 54
    }
30
31 54
    public function configPath()
32
    {
33 54
        return $this->path.'/config';
34
    }
35
36 54
    public function databaseFile()
37
    {
38 54
        return $this->path.'/database.sqlite';
39
    }
40
41 16
    public function dockerComposeFile()
42
    {
43 16
        return $this->path.'/docker-compose.yaml';
44
    }
45
46 54
    public function dockerImagesPath()
47
    {
48 54
        return $this->path.'/docker';
49
    }
50
51 6
    public function sslPath()
52
    {
53 6
        return $this->path.'/ssl';
54
    }
55
56 54
    public function path()
57
    {
58 54
        return $this->path;
59
    }
60
61 54
    public function viewsPath()
62
    {
63 54
        return $this->path.'/views';
64
    }
65
66 54
    public function alreadySetUp()
67
    {
68 54
        return $this->path && $this->files->exists($this->path);
69
    }
70
71 54
    public function setUp(Application $app, $force = false)
72
    {
73 54
        if ($this->alreadySetUp() && ! $force) {
74
            return;
75
        }
76
77 54
        if (! $this->path) {
78
            $this->path = ChooseMechanic::forOS()->getUserHomePath().'/.porter';
79
80
            $this->moveExistingConfig();
81
            $this->publishEnv();
82
            $this->updateEnv();
83
        }
84
85 54
        $this->publishConfigFiles();
86 54
        $this->createDatabase();
87 54
        $this->updateAppConfig($app);
88
89 54
        if ($this->shouldMigrateAndSeedDatabase) {
90 1
            Artisan::call('migrate:fresh');
91 1
            Artisan::call('db:seed');
92
        }
93
94 54
        $app->instance(PorterLibrary::class, $this);
95 54
    }
96
97
    protected function publishEnv()
98
    {
99
        $this->filePublisher->publish(
100
            base_path('.env.example'),
101
            base_path('.env')
102
        );
103
    }
104
105
    protected function moveExistingConfig()
106
    {
107
        if (! $this->alreadySetUp()) {
108
            return;
109
        }
110
111
        $this->files->moveDirectory($this->path(), $this->path().'_'.now()->format('YmdHis'));
112
    }
113
114 54
    protected function createDatabase()
115
    {
116 54
        $this->files->put($this->databaseFile(), '');
117 54
    }
118
119
    protected function updateEnv()
120
    {
121
        $envContent = $this->files->get(base_path('.env'));
122
        $envContent = preg_replace('/LIBRARY_PATH=.*\n/', "LIBRARY_PATH=\"{$this->path()}\"\n", $envContent);
123
        $this->files->put(base_path('.env'), $envContent);
124
    }
125
126 54
    protected function updateAppConfig(Application $app)
127
    {
128 54
        $app['config']->set('database.connections.default.database', $this->databaseFile());
129 54
        $app['config']->set('porter.library_path', $this->path());
130 54
    }
131
132 54
    protected function publishConfigFiles()
133
    {
134 54
        $this->filePublisher->publish(resource_path('stubs/config'), $this->configPath());
135 54
    }
136
137 54
    public function dontMigrateAndSeedDatabase()
138
    {
139 54
        $this->shouldMigrateAndSeedDatabase = false;
140
141 54
        return $this;
142
    }
143
}
144