Completed
Push — master ( 396778...5e0c6d )
by Changwan
03:27
created

QueueServiceProvider::boot()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 0
cts 3
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
namespace Wandu\Q;
3
4
use Aws\Sqs\SqsClient;
5
use Pheanstalk\PheanstalkInterface;
6
use Wandu\DI\ContainerInterface;
7
use Wandu\DI\ServiceProviderInterface;
8
use Wandu\Q\Adapter\BeanstalkdAdapter;
9
use Wandu\Q\Adapter\NullAdapter;
10
use Wandu\Q\Adapter\SqsAdapter;
11
use Wandu\Q\Contracts\AdapterInterface;
12
use Wandu\Q\Contracts\SerializerInterface;
13
use Wandu\Q\Serializer\JsonSerializer;
14
use Wandu\Q\Serializer\PhpSerializer;
15
use function Wandu\Foundation\config;
16
17
class QueueServiceProvider implements ServiceProviderInterface
18
{
19
    /**
20
     * {@inheritdoc}
21
     */
22
    public function register(ContainerInterface $app)
23
    {
24
        $app->closure(SerializerInterface::class, function () {
25
            switch (config('q.serializer')) {
26
                case 'json':
27
                    return new JsonSerializer();
28
            }
29
            return new PhpSerializer();
30
        });
31
        $app->closure(AdapterInterface::class, function (ContainerInterface $app) {
32
            switch (config('q.type')) {
33
                case 'beanstalkd':
34
                    $app->assert(PheanstalkInterface::class, 'pda/pheanstalk');
35
                    return new BeanstalkdAdapter($app->get(PheanstalkInterface::class));
36
                case 'sqs':
37
                    $app->assert(SqsClient::class, 'aws/aws-sdk-php');
38
                    return new SqsAdapter($app->get(SqsClient::class), config('q.sqs.url'));
39
            }
40
            return new NullAdapter();
41
        });
42
    }
43
44
    /**
45
     * {@inheritdoc}
46
     */
47
    public function boot(ContainerInterface $app)
48
    {
49
    }
50
}
51