Bootstrapper   A
last analyzed

Complexity

Total Complexity 20

Size/Duplication

Total Lines 131
Duplicated Lines 0 %

Test Coverage

Coverage 93.48%

Importance

Changes 0
Metric Value
wmc 20
dl 0
loc 131
ccs 43
cts 46
cp 0.9348
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A getConfigurationPath() 0 11 3
A exportConfiguration() 0 9 1
A unifySlashes() 0 3 1
A __construct() 0 3 1
C bindConfiguration() 0 25 8
A bindBase() 0 7 2
A updateConfiguration() 0 8 3
A bindPaths() 0 4 1
1
<?php
2
3
namespace Trucker;
4
5
use Illuminate\Container\Container;
6
7
/**
8
 * Finds configurations and paths.
9
 *
10
 * @author Alessandro Manno <[email protected]>
11
 */
12
class Bootstrapper
13
{
14
    /**
15
     * The Container.
16
     *
17
     * @var Container
18
     */
19
    protected $container;
20
21
    /**
22
     * Build a new Bootstrapper.
23
     *
24
     * @param Container $app
25
     */
26 6
    public function __construct(Container $app)
27
    {
28 6
        $this->app = $app;
0 ignored issues
show
Bug Best Practice introduced by
The property app does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
29 6
    }
30
31
    /**
32
     * Bind paths to the container.
33
     */
34 6
    public function bindPaths()
35
    {
36 6
        $this->bindBase();
37 6
        $this->bindConfiguration();
38 6
    }
39
40
    /**
41
     * Get the path to the configuration folder.
42
     *
43
     * @return string
44
     */
45 2
    public function getConfigurationPath()
46
    {
47
        // Return path to Laravel configuration
48 2
        if ($this->app->bound('path')) {
49 2
            $laravel = $this->app['path'] . '/config/packages/alexmanno/trucker';
50 2
            if (file_exists($laravel)) {
51
                return $laravel;
52
            }
53
        }
54
55 2
        return $this->app['path.trucker.config'];
56
    }
57
58
    /**
59
     * Export the configuration files.
60
     */
61 2
    public function exportConfiguration()
62
    {
63 2
        $source = __DIR__ . '/../config';
64 2
        $destination = $this->getConfigurationPath();
65
66
        // Unzip configuration files
67 2
        $this->app['files']->copyDirectory($source, $destination);
68
69 2
        return $destination;
70
    }
71
72
    /**
73
     * Replace placeholders in configuration.
74
     *
75
     * @param string $folder
76
     * @param array  $values
77
     */
78 1
    public function updateConfiguration($folder, array $values = [])
79
    {
80
        // Replace stub values in files
81 1
        $files = $this->app['files']->files($folder);
82 1
        foreach ($files as $file) {
83 1
            foreach ($values as $name => $value) {
84 1
                $contents = str_replace('{' . $name . '}', $value, file_get_contents($file));
85 1
                $this->app['files']->put($file, $contents);
86
            }
87
        }
88 1
    }
89
90
    /**
91
     * Bind the base path to the Container.
92
     */
93 6
    protected function bindBase()
94
    {
95 6
        if ($this->app->bound('path.base')) {
96 1
            return;
97
        }
98
99 5
        $this->app->instance('path.base', getcwd());
100 5
    }
101
102
    /**
103
     * Bind paths to the configuration files.
104
     */
105 6
    protected function bindConfiguration()
106
    {
107 6
        $path = $this->app['path.base'] ? $this->app['path.base'] . '/' : '';
108 6
        $logs = $this->app->bound('path.storage') ? str_replace($this->unifySlashes($path), null, $this->unifySlashes($this->app['path.storage'])) : '.trucker';
109
110
        $paths = [
111 6
            'config' => '.trucker',
112 6
            'logs' => $logs . '/logs',
113
        ];
114
115 6
        foreach ($paths as $key => $file) {
116 6
            $filename = $path . $file;
117
118
            // Check whether we provided a file or folder
119 6
            if (!is_dir($filename) and file_exists($filename . '.php')) {
120
                $filename .= '.php';
121
            }
122
123
            // Use configuration in current folder if none found
124 6
            $realpath = realpath('.') . '/' . $file;
0 ignored issues
show
Bug introduced by
Are you sure realpath('.') of type false|string can be used in concatenation? ( Ignorable by Annotation )

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

124
            $realpath = /** @scrutinizer ignore-type */ realpath('.') . '/' . $file;
Loading history...
125 6
            if (!file_exists($filename) and file_exists($realpath)) {
126
                $filename = $realpath;
127
            }
128
129 6
            $this->app->instance('path.trucker.' . $key, $filename);
130
        }
131 6
    }
132
133
    /**
134
     * Unify the slashes to the UNIX mode (forward slashes).
135
     *
136
     * @param string $path
137
     *
138
     * @return string
139
     */
140 5
    protected function unifySlashes($path)
141
    {
142 5
        return str_replace('\\', '/', $path);
143
    }
144
}
145