Completed
Push — master ( 06b292...adafac )
by Nicolai
02:22
created

TestingServiceProvider::provides()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
4
namespace SmartWeb\ModuleTesting\Providers;
5
6
use Config;
7
use File;
8
use Illuminate\Support\Collection;
9
use Illuminate\Support\ServiceProvider;
10
use Module as ModuleFacade;
11
use Nwidart\Modules\Module;
12
use SmartWeb\ModuleTesting\Console\Command\GenerateCest;
13
use SmartWeb\ModuleTesting\Console\Command\GenerateSetup;
14
use SmartWeb\ModuleTesting\Console\Command\GenerateTest;
15
use const DIRECTORY_SEPARATOR;
16
use function array_merge;
17
use function glob;
18
use function implode;
19
use function is_array;
20
21
22
/**
23
 * Class TestingServiceProvider
24
 *
25
 * @package SmartWeb\Testing\Providers
26
 */
27
class TestingServiceProvider extends ServiceProvider
28
{
29
    
30
    /**
31
     * Indicates if loading of the provider is deferred.
32
     *
33
     * @var bool
34
     */
35
    protected $defer = false;
36
    
37
    /**
38
     * @var Module
39
     */
40
    protected $module;
41
    
42
    /**
43
     * Boot the application events.
44
     *
45
     * @return void
46
     */
47
    public function boot()
48
    {
49
        $this->registerConfig();
50
        $this->registerHelpers();
51
        $this->registerMacros();
52
    }
53
    
54
    /**
55
     * Register the service provider.
56
     *
57
     * @return void
58
     */
59
    public function register()
60
    {
61
        $this->setIdeHelperPreferredSettings();
62
        $this->registerCommands();
63
    }
64
    
65
    /**
66
     * Get the services provided by the provider.
67
     *
68
     * @return array
69
     */
70
    public function provides()
71
    {
72
        return [];
73
    }
74
    
75
    /**
76
     * Register config.
77
     *
78
     * @return void
79
     */
80
    protected function registerConfig()
81
    {
82
        $this->publishes([
83
            __DIR__ . '/../Config/config.php' => config_path('testing.php'),
84
        ], 'config');
85
        $this->mergeConfigFrom(
86
            __DIR__ . '/../Config/config.php', 'testing'
87
        );
88
    }
89
    
90
    protected function registerHelpers()
91
    {
92
        foreach (glob($this->path('Helpers/*.php')) as $file) {
93
            File::requireOnce($file);
94
        }
95
        
96
        if (!defined('LARAVEL_BOOTSTRAP_PATH')) {
97
            define('LARAVEL_BOOTSTRAP_PATH', realpath(__DIR__ . '/../../../../bootstrap'));
98
        }
99
    }
100
    
101
    protected function registerMacros()
102
    {
103
        Collection::macro('mapInto', function (string $class)
104
        {
105
            return $this->map(function ($value) use ($class)
0 ignored issues
show
Bug introduced by
The method map() does not seem to exist on object<SmartWeb\ModuleTe...TestingServiceProvider>.

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...
106
            {
107
                return new $class($value);
108
            });
109
        });
110
        
111
        Collection::macro('mapIntoWithKeys', function (string $class)
112
        {
113
            return $this->map(function ($value, $key) use ($class)
0 ignored issues
show
Bug introduced by
The method map() does not seem to exist on object<SmartWeb\ModuleTe...TestingServiceProvider>.

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...
114
            {
115
                return new $class($value, $key);
116
            });
117
        });
118
        
119
        Collection::macro('dd', function ()
120
        {
121
            dd($this);
122
        });
123
    }
124
    
125
    protected function setIdeHelperPreferredSettings()
126
    {
127
        foreach ($this->getIdeHelperPreferredSettings() as $name => $setting) {
128
            $key = "ide-helper.{$name}";
129
            $existing = Config::get($key);
130
            
131
            if (is_array($existing)) {
132
                Config::set($key, array_merge($existing, (array)$setting));
133
            } else {
134
                Config::set($key, $setting);
135
            }
136
        }
137
    }
138
    
139
    protected function registerCommands()
140
    {
141
        $this->app->singleton(
142
            'command.smartweb.testing.generate-setup',
143
            GenerateSetup::class
144
        );
145
        
146
        $this->app->singleton(
147
            'command.smartweb.testing.generate-test',
148
            GenerateTest::class
149
        );
150
        
151
        $this->app->singleton(
152
            'command.smartweb.testing.generate-cest',
153
            GenerateCest::class
154
        );
155
        
156
        $this->commands(
157
            'command.smartweb.testing.generate-setup',
158
            'command.smartweb.testing.generate-test',
159
            'command.smartweb.testing.generate-cest');
160
    }
161
    
162
    /**
163
     * @param string[] ...$parts
164
     *
165
     * @return string
166
     */
167
    protected function path(...$parts) : string
168
    {
169
        return $this->getModule()->getExtraPath(implode(DIRECTORY_SEPARATOR, $parts));
170
    }
171
    
172
    /**
173
     * @return Module
174
     */
175
    protected function getModule() : Module
176
    {
177
        return $this->module = $this->module ?? ModuleFacade::find('Testing');
178
    }
179
    
180
    /**
181
     * @return array
182
     */
183
    protected function getIdeHelperPreferredSettings() : array
184
    {
185
        return [
186
            'include_fluent'              => true,
187
            'model_camel_case_properties' => true,
188
            'model_locations'             => 'app/Administration/Testing',
189
        ];
190
    }
191
}
192