Passed
Pull Request — 2.x (#1376)
by Harings
09:59
created

HandlesPresets::getAppRootStorage()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 5
nc 2
nop 0
dl 0
loc 10
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace A17\Twill\Commands\Traits;
4
5
use Illuminate\Filesystem\FilesystemAdapter;
6
use Illuminate\Support\Facades\Storage;
7
8
/**
9
 * @method void publishConfig;
10
 */
11
trait HandlesPresets
12
{
13
    /**
14
     * @var \Illuminate\Filesystem\FilesystemAdapter[]
15
     */
16
    protected $examplesStorage = [];
17
18
    /**
19
     * @var \Illuminate\Filesystem\FilesystemAdapter
20
     */
21
    protected $appRootStorage;
22
23
    protected function presetExists(string $preset): bool
24
    {
25
        return $this->getExamplesDirectoryStorage()->exists($preset);
26
    }
27
28
    protected function installPresetFiles(string $preset): void
29
    {
30
        $this->checkMeetsRequirementsForPreset($preset);
31
32
        // First publish the config as we overwrite it later.
33
        $this->publishConfig();
0 ignored issues
show
Bug introduced by
The method publishConfig() does not exist on A17\Twill\Commands\Traits\HandlesPresets. Did you maybe mean publishConfig;()? ( Ignorable by Annotation )

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

33
        $this->/** @scrutinizer ignore-call */ 
34
               publishConfig();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
34
35
        $examplesStorage = $this->getExamplesStorage($preset);
36
        $appRootStorage = $this->getAppRootStorage();
37
38
        foreach ($examplesStorage->allDirectories() as $directory) {
39
            if ($appRootStorage->makeDirectory($directory)) {
40
                foreach ($examplesStorage->files($directory) as $file) {
41
                    $appRootStorage->put($file, $examplesStorage->get($file));
42
                }
43
            }
44
        }
45
    }
46
47
    /**
48
     * This method reverses the process. It goes over all the files in the example and copies them from the project to
49
     * the twill examples folder. If you have new files you need to manually copy them once.
50
     *
51
     * This is useful for developing examples as you can install it, update it, then copy it back.
52
     */
53
    protected function updatePreset(string $preset): void {
54
        $examplesStorage = $this->getExamplesStorage($preset);
55
        $appRootStorage = $this->getAppRootStorage();
56
57
        foreach ($examplesStorage->allDirectories() as $directory) {
58
            foreach ($examplesStorage->files($directory) as $file) {
59
                $examplesStorage->put($file, $appRootStorage->get($file));
60
            }
61
        }
62
    }
63
64
    private function getExamplesStorage(string $preset): FilesystemAdapter
65
    {
66
        if (!isset($this->examplesStorage[$preset])) {
67
            $this->examplesStorage[$preset] = Storage::build([
68
                'driver' => 'local',
69
                'root' => __DIR__ . '/../../../examples/' . $preset,
70
            ]);
71
        }
72
73
        return $this->examplesStorage[$preset];
74
    }
75
76
    private function getExamplesDirectoryStorage(): FilesystemAdapter
77
    {
78
        return Storage::build([
79
            'driver' => 'local',
80
            'root' => __DIR__ . '/../../../examples/',
81
        ]);
82
    }
83
84
    private function getAppRootStorage(): FilesystemAdapter
85
    {
86
        if (!$this->appRootStorage) {
87
            $this->appRootStorage = Storage::build([
88
                'driver' => 'local',
89
                'root' => base_path(),
90
            ]);
91
        }
92
93
        return $this->appRootStorage;
94
    }
95
96
    private function checkMeetsRequirementsForPreset(string $preset): void
97
    {
98
        if ($preset === 'blog') {
99
            if (!\Composer\InstalledVersions::isInstalled('kalnoy/nestedset')) {
100
                throw new \RuntimeException(
101
                    'Missing nestedset, please install it using "composer require kalnoy/nestedset"'
102
                );
103
            }
104
        }
105
    }
106
}
107