YamlBuilder::build()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 10
ccs 5
cts 5
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
namespace App\Support\Console\DockerCompose;
4
5
use App\Models\PhpVersion;
6
use App\PorterLibrary;
7
use App\Support\Contracts\ImageRepository;
8
use Illuminate\Filesystem\Filesystem;
9
10
class YamlBuilder
11
{
12
    /** @var Filesystem */
13
    protected $files;
14
15
    /** @var PorterLibrary */
16
    private $porterLibrary;
17
18 210
    public function __construct(Filesystem $files, PorterLibrary $porterLibrary)
19
    {
20 210
        $this->files = $files;
21 210
        $this->porterLibrary = $porterLibrary;
22
    }
23
24
    /**
25
     * Build the docker-compose.yaml file.
26
     *
27
     * @param $imageSet
28
     *
29
     * @throws \Throwable
30
     *
31
     * @return string
32
     */
33 8
    public function build(ImageRepository $imageSet)
34
    {
35 8
        $path = $this->porterLibrary->dockerComposeFile();
36
37 8
        $this->files->put(
38
            $path,
39 8
            $this->renderDockerComposeFile($imageSet)
40
        );
41
42 8
        return $path;
43
    }
44
45
    /**
46
     * Render the docker compose file.
47
     *
48
     * @param ImageRepository $imageSet
49
     *
50
     * @throws \Throwable
51
     *
52
     * @return string
53
     */
54 12
    public function renderDockerComposeFile(ImageRepository $imageSet)
55
    {
56 12
        return (string) view("{$imageSet->getName()}::base")->with([
57 12
            'home'              => setting('home'),
58 12
            'host_machine_name' => setting('host_machine_name'),
59 12
            'activePhpVersions' => PhpVersion::active()->get(),
60 12
            'useMysql'          => setting('use_mysql') === 'on',
61 12
            'useRedis'          => setting('use_redis') === 'on',
62 12
            'useBrowser'        => setting('use_browser') === 'on',
63 12
            'useMeilisearch'    => setting('use_meilisearch') === 'on',
64 12
            'useElasticsearch'  => setting('use_elasticsearch') === 'on',
65 12
            'useDns'            => setting('use_dns') === 'on' || setting_missing('use_dns'),
66 12
            'httpPort'          => setting('http_port', 80),
67 12
            'httpsPort'         => setting('https_port', 443),
68
            'imageSet'          => $imageSet,
69 12
            'libraryPath'       => $this->porterLibrary->path(),
70 12
        ])->render();
71
    }
72
73
    /**
74
     * Destroy the docker-compose.yaml file.
75
     */
76 1
    public function destroy()
77
    {
78 1
        $this->files->delete($this->porterLibrary->dockerComposeFile());
79
    }
80
}
81