Passed
Pull Request — master (#23)
by Keoghan
02:43
created

PorterLibrary::setUp()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 24
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 14
dl 0
loc 24
rs 9.4888
c 0
b 0
f 0
cc 5
nc 5
nop 2
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
    public function __construct(FilePublisher $filePublisher, $path)
25
    {
26
        $this->filePublisher = $filePublisher;
27
        $this->files = $filePublisher->getFilesystem();
28
        $this->path = $path;
29
    }
30
31
    public function configPath()
32
    {
33
        return $this->path.'/config';
34
    }
35
36
    public function databaseFile()
37
    {
38
        return $this->path.'/database.sqlite';
39
    }
40
41
    public function dockerComposeFile()
42
    {
43
        return $this->path.'/docker-compose.yaml';
44
    }
45
46
    public function dockerImagesPath()
47
    {
48
        return $this->path.'/docker';
49
    }
50
51
    public function sslPath()
52
    {
53
        return $this->path.'/ssl';
54
    }
55
56
    public function path()
57
    {
58
        return $this->path;
59
    }
60
61
    public function viewsPath()
62
    {
63
        return $this->path.'/views';
64
    }
65
66
    public function alreadySetUp()
67
    {
68
        return $this->path && $this->files->exists($this->path);
69
    }
70
71
    public function setUp(Application $app, $force = false)
72
    {
73
        if ($this->alreadySetUp() && ! $force) {
74
            return;
75
        }
76
77
        if (! $this->path) {
78
            $this->path = ChooseMechanic::forOS()->getUserHomePath().'/.porter';
79
80
            $this->moveExistingConfig();
81
            $this->publishEnv();
82
            $this->updateEnv();
83
        }
84
85
        $this->publishConfigFiles();
86
        $this->createDatabase();
87
        $this->updateAppConfig($app);
88
89
        if ($this->shouldMigrateAndSeedDatabase) {
90
            Artisan::call('migrate:fresh');
91
            Artisan::call('db:seed');
92
        }
93
94
        $app->instance(PorterLibrary::class, $this);
95
    }
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
    protected function createDatabase()
115
    {
116
        $this->files->put($this->databaseFile(), '');
117
    }
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
    protected function updateAppConfig(Application $app)
127
    {
128
        $app['config']->set('database.connections.default.database', $this->databaseFile());
129
        $app['config']->set('porter.library_path', $this->path());
130
    }
131
132
    protected function publishConfigFiles()
133
    {
134
        $this->filePublisher->publish(resource_path('stubs/config'), $this->configPath());
135
    }
136
137
    public function dontMigrateAndSeedDatabase()
138
    {
139
        $this->shouldMigrateAndSeedDatabase = false;
140
141
        return $this;
142
    }
143
}
144