Issues (7)

src/ServiceProvider.php (5 issues)

1
<?php
2
3
namespace Sebdesign\ArtisanCloudflare;
4
5
use GuzzleHttp\Client as GuzzleClient;
6
use Illuminate\Support\Collection;
7
use Illuminate\Support\ServiceProvider as IlluminateServiceProvider;
8
9
class ServiceProvider extends IlluminateServiceProvider
10
{
11
    /**
12
     * Bootstrap the application services.
13
     */
14 108
    public function boot(): void
15
    {
16 108
        if ($this->app->runningInConsole()) {
17 108
            $this->publishes([
18 108
                __DIR__.'/../config/cloudflare.php' => config_path('cloudflare.php'),
19 108
            ], 'config');
20
        }
21 108
    }
22
23 108
    public function register(): void
24
    {
25 108
        $this->mergeConfigFrom(__DIR__.'/../config/cloudflare.php', 'cloudflare');
26
27 108
        $this->registerClient();
28 108
        $this->registerCommands();
29 108
        $this->registerMacros();
30 108
    }
31
32 108
    protected function registerClient(): void
33
    {
34
        $this->app->bind(Client::class, function () {
35 9
            return new Client(
36 9
                $this->bootGuzzleClient(),
37 9
                $this->app['log']
38
            );
39 108
        });
40 108
    }
41
42 9
    protected function bootGuzzleClient(): GuzzleClient
43
    {
44 9
        $config = $this->app['config']['cloudflare'];
45
46 9
        $authorization = isset($config['token']) ? [
47 3
            'Authorization' => "Bearer {$config['token']}",
48
        ] : [
49 6
            'X-Auth-Key' => $config['key'],
50 9
            'X-Auth-Email' => $config['email'],
51
        ];
52
53 9
        return new GuzzleClient([
54 9
            'base_uri' => Client::BASE_URI,
55 9
            \GuzzleHttp\RequestOptions::HEADERS => $authorization,
56
        ]);
57
    }
58
59 108
    protected function registerCommands(): void
60
    {
61
        $this->app->bind(Commands\Cache\Purge::class, function () {
62 63
            return new Commands\Cache\Purge(
63 63
                $this->app['config']['cloudflare.zones'] ?? []
64
            );
65 108
        });
66
67
        $this->app->bind(Commands\Waf\BlockIP::class, function () {
68 60
            return new Commands\Waf\BlockIP(
69 60
                $this->app['config']['cloudflare.zones'] ?? []
70
            );
71 108
        });
72
73 108
        $this->commands([
74 108
            Commands\Cache\Purge::class,
75
            Commands\Waf\BlockIP::class,
76
        ]);
77 108
    }
78
79 108
    protected function registerMacros(): void
80
    {
81
        /*
82
         * Transpose with keys.
83
         *
84
         * Implementation for PHP < 5.6 and Laravel ~5.1.
85
         *
86
         * @return \Illuminate\Support\Collection
87
         */
88
        Collection::macro('_transpose', function () {
89 54
            $keys = $this->keys()->all();
0 ignored issues
show
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

89
            $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...
90
91
            $params = array_merge([function () use ($keys) {
92 54
                return new static(array_combine($keys, func_get_args()));
0 ignored issues
show
array_combine($keys, func_get_args()) of type array 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

92
                return new static(/** @scrutinizer ignore-type */ array_combine($keys, func_get_args()));
Loading history...
93 54
            }], $this->values()->toArray());
0 ignored issues
show
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

93
            }], $this->/** @scrutinizer ignore-call */ values()->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...
94
95 54
            return new static(call_user_func_array('array_map', $params));
96 108
        });
97
98
        /*
99
         * Add a value between each item.
100
         *
101
         * @param  mixed $value
102
         * @return \Illuminate\Support\Collection
103
         */
104
        Collection::macro('insertBetween', function ($value) {
105
            return $this->values()->flatMap(function ($item, $index) use ($value) {
106 54
                return [$index ? $value : null, $item];
107 54
            })->forget(0)->values();
108 108
        });
109
110
        /*
111
         * Reorder the collection according to an array of keys
112
         *
113
         * @param  mixed $keys
114
         * @return \Illuminate\Support\Collection
115
         */
116
        Collection::macro('reorder', function ($keys) {
117 33
            $order = $this->getArrayableItems($keys);
0 ignored issues
show
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

117
            /** @scrutinizer ignore-call */ 
118
            $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...
118
119
            return $this->sortBy(function ($item, $key) use ($order) {
0 ignored issues
show
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

119
            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...
120 33
                return array_search($key, $order);
121 33
            });
122 108
        });
123 108
    }
124
125
    /**
126
     * Determine if the provider is deferred.
127
     *
128
     * @return bool
129
     */
130 6
    public function isDeferred()
131
    {
132 6
        return true;
133
    }
134
135
    /**
136
     * Get the services provided by the provider.
137
     *
138
     * @return array
139
     */
140 9
    public function provides()
141
    {
142
        return [
143 9
            Client::class,
144
            Commands\Cache\Purge::class,
145
        ];
146
    }
147
}
148