Completed
Push — master ( 8ecc57...8ecc57 )
by Nicolai
03:52 queued 01:52
created

ModuleTestingServiceProvider::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 Illuminate\Support\Collection;
7
use Illuminate\Support\Facades\File;
8
use Illuminate\Support\ServiceProvider;
9
use SmartWeb\ModuleTesting\Console\Command\GenerateCest;
10
use SmartWeb\ModuleTesting\Console\Command\GenerateSetup;
11
use SmartWeb\ModuleTesting\Console\Command\GenerateTest;
12
use const DIRECTORY_SEPARATOR;
13
use function glob;
14
use function implode;
15
16
17
/**
18
 * Class ModuleTestingServiceProvider
19
 *
20
 * @package SmartWeb\ModuleTesting\Providers
21
 */
22
class ModuleTestingServiceProvider extends ServiceProvider
23
{
24
    
25
    /**
26
     * Indicates if loading of the provider is deferred.
27
     *
28
     * @var bool
29
     */
30
    protected $defer = false;
31
    
32
    /**
33
     * Boot the application events.
34
     *
35
     * @return void
36
     */
37
    public function boot()
38
    {
39
        if ($this->app->environment() !== 'production') {
40
            $this->registerConfig();
41
            $this->registerHelpers();
42
            $this->registerMacros();
43
        }
44
    }
45
    
46
    /**
47
     * Register the service provider.
48
     *
49
     * @return void
50
     */
51
    public function register()
52
    {
53
        $this->registerCommands();
54
    }
55
    
56
    /**
57
     * Get the services provided by the provider.
58
     *
59
     * @return array
60
     */
61
    public function provides()
62
    {
63
        return [];
64
    }
65
    
66
    /**
67
     * Register config.
68
     *
69
     * @return void
70
     */
71
    protected function registerConfig()
72
    {
73
        $this->publishes([
74
            __DIR__ . '/../Config/config.php' => config_path('testing.php'),
75
        ], 'config');
76
        $this->mergeConfigFrom(
77
            __DIR__ . '/../Config/config.php', 'testing'
78
        );
79
    }
80
    
81
    protected function registerHelpers()
82
    {
83
        foreach (glob($this->path('Helpers/*.php')) as $file) {
84
            File::requireOnce($file);
85
        }
86
    }
87
    
88
    protected function registerMacros()
89
    {
90
        Collection::macro('mapInto', function (string $class)
91
        {
92
            /**
93
             * @var Collection $this
94
             */
95
            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...
96
            {
97
                return new $class($value);
98
            });
99
        });
100
        
101
        Collection::macro('mapIntoWithKeys', function (string $class)
102
        {
103
            /**
104
             * @var Collection $this
105
             */
106
            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...
107
            {
108
                return new $class($value, $key);
109
            });
110
        });
111
        
112
        Collection::macro('dd', function ()
113
        {
114
            /**
115
             * @var Collection $this
116
             */
117
            dd($this);
118
        });
119
    }
120
    
121
    protected function registerCommands()
122
    {
123
        $this->app->singleton(
124
            'command.smartweb.testing.generate-setup',
125
            GenerateSetup::class
126
        );
127
        
128
        $this->app->singleton(
129
            'command.smartweb.testing.generate-test',
130
            GenerateTest::class
131
        );
132
        
133
        $this->app->singleton(
134
            'command.smartweb.testing.generate-cest',
135
            GenerateCest::class
136
        );
137
        
138
        $this->commands(
139
            'command.smartweb.testing.generate-setup',
140
            'command.smartweb.testing.generate-test',
141
            'command.smartweb.testing.generate-cest');
142
    }
143
    
144
    /**
145
     * @param string[] ...$parts
146
     *
147
     * @return string
148
     */
149
    protected function path(...$parts) : string
150
    {
151
        return __DIR__ . '/../' . implode(DIRECTORY_SEPARATOR, $parts);
152
    }
153
}
154