Passed
Push — master ( 6e914f...262974 )
by Sébastien
13:23 queued 11:19
created

ServiceProvider::provides()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 5
ccs 0
cts 0
cp 0
crap 2
rs 10
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 78
    public function boot()
15
    {
16 78
        if ($this->app->runningInConsole()) {
17 78
            $this->publishes([
18 78
                __DIR__.'/../config/cloudflare.php' => config_path('cloudflare.php'),
19
            ], 'config');
20
        }
21
    }
22
23 78
    public function register()
24
    {
25 78
        $this->mergeConfigFrom(__DIR__.'/../config/cloudflare.php', 'cloudflare');
26
27 78
        $this->registerClient();
28 78
        $this->registerCommands();
29 78
        $this->registerMacros();
30
    }
31
32 78
    protected function registerClient()
33
    {
34 78
        $this->app->bind(Client::class, function () {
35 9
            return new Client(
36 9
                $this->bootGuzzleClient(),
37 9
                $this->app['log']
38
            );
39
        });
40
    }
41
42 9
    protected function bootGuzzleClient()
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 6
            'X-Auth-Email' => $config['email'],
51
        ];
52
53 9
        return new GuzzleClient([
54
            'base_uri' => Client::BASE_URI,
55
            \GuzzleHttp\RequestOptions::HEADERS => $authorization,
56
        ]);
57
    }
58
59 78
    protected function registerCommands()
60
    {
61 78
        $this->app->bind(Commands\Cache\Purge::class, function () {
62 36
            return new Commands\Cache\Purge(
63 36
                $this->app['config']['cloudflare.zones'] ?? []
64
            );
65
        });
66
67 78
        $this->commands([
68
            Commands\Cache\Purge::class,
69
        ]);
70
    }
71
72 78
    protected function registerMacros()
73
    {
74
        /*
75
         * Transpose with keys.
76
         *
77
         * Implementation for PHP < 5.6 and Laravel ~5.1.
78
         *
79
         * @return \Illuminate\Support\Collection
80
         */
81 78
        Collection::macro('_transpose', function () {
82 33
            $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

82
            $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...
83
84 33
            $params = array_merge([function () use ($keys) {
85 33
                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 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

85
                return new static(/** @scrutinizer ignore-type */ array_combine($keys, func_get_args()));
Loading history...
86 33
            }], $this->values()->toArray());
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

86
            }], $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...
87
88 33
            return new static(call_user_func_array('array_map', $params));
89
        });
90
91
        /*
92
         * Add a value between each item.
93
         *
94
         * @param  mixed $value
95
         * @return \Illuminate\Support\Collection
96
         */
97 78
        Collection::macro('insertBetween', function ($value) {
98 33
            return $this->values()->flatMap(function ($item, $index) use ($value) {
99 33
                return [$index ? $value : null, $item];
100 33
            })->forget(0)->values();
101
        });
102
103
        /*
104
         * Reorder the collection according to an array of keys
105
         *
106
         * @param  mixed $keys
107
         * @return \Illuminate\Support\Collection
108
         */
109 78
        Collection::macro('reorder', function ($keys) {
110 33
            $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

110
            /** @scrutinizer ignore-call */ 
111
            $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...
111
112 33
            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

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