Completed
Push — master ( ac8bb1...1061f5 )
by Daniel
02:21
created

GitlabServiceProvider::boot()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 11
nc 1
nop 0
dl 0
loc 18
ccs 0
cts 13
cp 0
crap 2
rs 9.9
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace DanielPieper\MergeReminder\ServiceProvider;
4
5
use Gitlab\Client;
6
use Gitlab\HttpClient\Builder;
7
use Http\Client\Common\Plugin\CachePlugin;
8
use Http\Client\Common\Plugin\LoggerPlugin;
9
use Http\Discovery\StreamFactoryDiscovery;
10
use League\Container\Container;
11
use League\Container\ServiceProvider\AbstractServiceProvider;
12
use League\Container\ServiceProvider\BootableServiceProviderInterface;
13
use Psr\Cache\CacheItemPoolInterface;
14
use Psr\Log\LoggerAwareInterface;
15
use Psr\Log\LoggerInterface;
16
use Symfony\Component\Cache\Adapter\FilesystemAdapter;
17
use Symfony\Component\Console\Logger\ConsoleLogger;
18
use Symfony\Component\Console\Output\ConsoleOutput;
19
use Symfony\Component\Console\Output\OutputInterface;
20
21
class GitlabServiceProvider extends AbstractServiceProvider implements BootableServiceProviderInterface
22
{
23
    /**
24
     * {@inheritdoc}
25
     */
26
    protected $provides = [
27
        OutputInterface::class,
28
        LoggerInterface::class,
29
        LoggerPlugin::class,
30
        CacheItemPoolInterface::class,
31
        CachePlugin::class,
32
        Builder::class,
33
        Client::class,
34
    ];
35
36
    /**
37
     * {@inheritdoc}
38
     */
39
    public function register()
40
    {
41
        /** @var Container $container */
42
        $container = $this->getContainer();
43
44
        $container->share(LoggerPlugin::class)
45
            ->addArgument(LoggerInterface::class);
46
47
        $container->share(CachePlugin::class)
48
            ->addArguments([
49
                CacheItemPoolInterface::class,
50
                StreamFactoryDiscovery::find(),
51
                [
52
                    'default_ttl' => 600,
53
                    'respect_response_cache_directives' => [],
54
                ]
55
            ]);
56
57
        $container->share(Builder::class)
58
            ->addMethodCall('addPlugin', [LoggerPlugin::class])
59
            ->addMethodCall('addPlugin', [CachePlugin::class]);
60
61
        $container->share(Client::class)
62
            ->addArgument(Builder::class)
63
            ->addMethodCall('setUrl', ['GITLAB_URL'])
64
            ->addMethodCall('authenticate', [
65
                'GITLAB_TOKEN',
66
                Client::AUTH_URL_TOKEN,
67
            ]);
68
    }
69
70
    /**
71
     * Method will be invoked on registration of a service provider implementing
72
     * this interface. Provides ability for eager loading of Service Providers.
73
     *
74
     * @return void
75
     */
76
    public function boot()
77
    {
78
        /** @var Container $container */
79
        $container = $this->getContainer();
80
81
        $container->share(OutputInterface::class, ConsoleOutput::class);
82
83
        $container->share(LoggerInterface::class, ConsoleLogger::class)
84
            ->addArgument(OutputInterface::class);
85
86
        $container->inflector(LoggerAwareInterface::class)
87
            ->invokeMethod('setLogger', [LoggerInterface::class]);
88
89
        $container->share(CacheItemPoolInterface::class, FilesystemAdapter::class)
90
            ->addArguments([
91
                'namespace' => 'mrcli',
92
                'defaultLifetime' => 0,
93
                'directory' => null,
94
            ]);
95
    }
96
}
97