Completed
Push — master ( 345624...4d3da2 )
by Sébastien
09:33
created

ServiceProvider::isDeferred()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Sebdesign\ArtisanCloudflare;
4
5
use Illuminate\Support\Collection;
6
use GuzzleHttp\Client as GuzzleClient;
7
use Illuminate\Support\ServiceProvider as IlluminateServiceProvider;
8
9
class ServiceProvider extends IlluminateServiceProvider
10
{
11
    /**
12
     * Bootstrap the application services.
13
     */
14
    public function boot()
15
    {
16
        if ($this->app->runningInConsole()) {
17
            $this->publishes([
18
                __DIR__.'/../config/cloudflare.php' => config_path('cloudflare.php'),
19
            ], 'config');
20
        }
21
    }
22
23
    public function register()
24
    {
25
        $this->mergeConfigFrom(__DIR__.'/../config/cloudflare.php', 'cloudflare');
26
27
        $this->registerClient();
28
        $this->registerCommands();
29
        $this->registerMacros();
30
    }
31
32
    protected function registerClient()
33
    {
34
        $this->app->bind(Client::class, function () {
35
            return new Client(
36
                $this->bootGuzzleClient(),
37
                $this->app['log']
38
            );
39
        });
40
    }
41
42
    protected function bootGuzzleClient()
43
    {
44
        $config = $this->app['config']['cloudflare'];
45
46
        return new GuzzleClient([
47
            'base_uri' => Client::BASE_URI,
48
            \GuzzleHttp\RequestOptions::HEADERS => [
49
                'X-Auth-Key' => $config['key'],
50
                'X-Auth-Email' => $config['email'],
51
            ],
52
        ]);
53
    }
54
55
    protected function registerCommands()
56
    {
57
        $this->app->bind(Commands\Cache\Purge::class, function () {
58
            return new Commands\Cache\Purge(
59
                $this->app['config']['cloudflare.zones']
60
            );
61
        });
62
63
        $this->commands([
64
            Commands\Cache\Purge::class,
65
        ]);
66
    }
67
68
    protected function registerMacros()
69
    {
70
        /*
71
         * Transpose with keys.
72
         *
73
         * Implementation for PHP < 5.6 and Laravel ~5.1.
74
         *
75
         * @return \Illuminate\Support\Collection
76
         */
77
        Collection::macro('_transpose', function () {
78
            $keys = $this->keys()->all();
0 ignored issues
show
Bug introduced by
The method keys() does not exist on Sebdesign\ArtisanCloudflare\ServiceProvider. ( Ignorable by Annotation )

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

78
            $keys = $this->/** @scrutinizer ignore-call */ keys()->all();

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...
79
80
            $params = array_merge([function () use ($keys) {
81
                return new static(array_combine($keys, func_get_args()));
0 ignored issues
show
Bug introduced by
array_combine($keys, func_get_args()) of type array|false is incompatible with the type Illuminate\Contracts\Foundation\Application expected by parameter $app of Sebdesign\ArtisanCloudfl...Provider::__construct(). ( Ignorable by Annotation )

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

81
                return new static(/** @scrutinizer ignore-type */ array_combine($keys, func_get_args()));
Loading history...
82
            }], $this->toArray());
0 ignored issues
show
Bug introduced by
The method toArray() does not exist on Sebdesign\ArtisanCloudflare\ServiceProvider. ( Ignorable by Annotation )

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

82
            }], $this->/** @scrutinizer ignore-call */ toArray());

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...
83
84
            return new static(call_user_func_array('array_map', $params));
85
        });
86
87
        /*
88
         * Add a value between each item.
89
         *
90
         * @param  mixed $value
91
         * @return \Illuminate\Support\Collection
92
         */
93
        Collection::macro('insertBetween', function ($value) {
94
            return $this->values()->flatMap(function ($item, $index) use ($value) {
0 ignored issues
show
Bug introduced by
The method values() does not exist on Sebdesign\ArtisanCloudflare\ServiceProvider. ( Ignorable by Annotation )

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

94
            return $this->/** @scrutinizer ignore-call */ values()->flatMap(function ($item, $index) use ($value) {

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...
95
                return [$index ? $value : null, $item];
96
            })->forget(0)->values();
97
        });
98
99
        /*
100
         * Reorder the collection according to an array of keys
101
         *
102
         * @param  mixed $keys
103
         * @return \Illuminate\Support\Collection
104
         */
105
        Collection::macro('reorder', function ($keys) {
106
            $order = $this->getArrayableItems($keys);
0 ignored issues
show
Bug introduced by
The method getArrayableItems() does not exist on Sebdesign\ArtisanCloudflare\ServiceProvider. ( Ignorable by Annotation )

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

106
            /** @scrutinizer ignore-call */ 
107
            $order = $this->getArrayableItems($keys);

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 $this->sortBy(function ($item, $key) use ($order) {
0 ignored issues
show
Bug introduced by
The method sortBy() does not exist on Sebdesign\ArtisanCloudflare\ServiceProvider. ( Ignorable by Annotation )

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

108
            return $this->/** @scrutinizer ignore-call */ sortBy(function ($item, $key) use ($order) {

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...
109
                return array_search($key, $order);
110
            });
111
        });
112
    }
113
114
    /**
115
     * Determine if the provider is deferred.
116
     *
117
     * @return bool
118
     */
119
    public function isDeferred()
120
    {
121
        return true;
122
    }
123
124
    /**
125
     * Get the services provided by the provider.
126
     *
127
     * @return array
128
     */
129
    public function provides()
130
    {
131
        return [
132
            Client::class,
133
            Commands\Cache\Purge::class,
134
        ];
135
    }
136
}
137