DomainPluginServiceProvider::boot()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 9
rs 10
1
<?php
2
3
namespace W2w\Lib\ApieDomainPlugin;
4
5
use Illuminate\Support\ServiceProvider;
6
use Pdp\Cache;
7
use Pdp\CurlHttpClient;
8
use Pdp\HttpClient;
9
use Pdp\Manager;
10
use Pdp\Rules;
11
use W2w\Lib\ApieDomainPlugin\HttpClient\MockHttpClient;
12
13
/**
14
 * Adds functionality to parse domain names to Laravel/Lumen.
15
 */
16
class DomainPluginServiceProvider extends ServiceProvider
17
{
18
    /**
19
     * Perform post-registration booting of services.
20
     *
21
     * @return void
22
     */
23
    public function boot()
24
    {
25
        $this->publishes(
26
            [
27
                __DIR__ . '/../config/apie-domain-plugin.php' => $this->app->get('path.config') . DIRECTORY_SEPARATOR . 'apie-domain-plugin.php',
28
            ]
29
        );
30
        if ($this->app->make('config')->get('apie-domain-plugin.mock', false)) {
31
            $this->app->bind(HttpClient::class, MockHttpClient::class);
32
        }
33
    }
34
35
    public function register()
36
    {
37
        $this->app->bind(HttpClient::class, CurlHttpClient::class);
38
39
        $this->app->singleton(Manager::class, function () {
40
            $path = $this->app->get('path.storage') . DIRECTORY_SEPARATOR . 'app/domains';
41
            return new Manager(new Cache($path), $this->app->get(HttpClient::class));
42
        });
43
        $this->app->singleton(Rules::class, function () {
44
            return $this->app->get(Manager::class)->getRules()
45
                ->withAsciiIDNAOption(IDNA_NONTRANSITIONAL_TO_ASCII)
46
                ->withUnicodeIDNAOption(IDNA_NONTRANSITIONAL_TO_UNICODE);
47
        });
48
        $this->app->singleton(DomainPlugin::class);
49
    }
50
}
51