Passed
Push — master ( 609758...a29d3e )
by Vincent
04:26
created

PrimeFailerDriverConfigurator::scheme()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
namespace Bdf\QueueBundle\DependencyInjection\Failer;
4
5
use Bdf\Dsn\DsnRequest;
6
use Bdf\Queue\Console\Command\Failer\InitCommand;
0 ignored issues
show
Bug introduced by
The type Bdf\Queue\Console\Command\Failer\InitCommand was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
7
use Bdf\Queue\Failer\DbFailedJobStorage;
0 ignored issues
show
Bug introduced by
The type Bdf\Queue\Failer\DbFailedJobStorage was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
use Bdf\Queue\Failer\DbFailedJobRepository;
0 ignored issues
show
Bug introduced by
The type Bdf\Queue\Failer\DbFailedJobRepository was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
9
use Bdf\Queue\Failer\FailedJobRepositoryAdapter;
10
use Bdf\Queue\Failer\FailedJobRepositoryInterface;
11
use InvalidArgumentException;
12
use Symfony\Component\DependencyInjection\ContainerBuilder;
13
use Symfony\Component\DependencyInjection\Reference;
14
15
/**
16
 * Configurator for failer repository using prime
17
 *
18
 * The package "b2pweb/bdf-queue-prime-adapter" must be installed and
19
 * "b2pweb/bdf-prime-bundle" configure to enable this failer repository
20
 *
21
 * DSN format: "prime://[connection]/[table]?maxRows=[cursorSize]"
22
 * with:
23
 * - [connection] as the prime connection to use for store failed jobs
24
 * - [table] as the table name
25
 * - [cursorSize] (optional): number of loaded jobs each times by the cursor with calling
26
 *                `FailedJobRepositoryInterface::all()` or `search()`. Default value is 50
27
 */
28
final class PrimeFailerDriverConfigurator implements FailerDriverConfiguratorInterface
29
{
30
    /**
31
     * {@inheritdoc}
32
     */
33 3
    public function scheme(): string
34
    {
35 3
        return 'prime';
36
    }
37
38
    /**
39
     * {@inheritdoc}
40
     */
41 3
    public function available(ContainerBuilder $container): bool
42
    {
43 3
        if (!$container->has('prime')) {
44
            return false;
45
        }
46
47 3
        return class_exists(DbFailedJobRepository::class) || class_exists(DbFailedJobStorage::class);
48
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53 3
    public function configure(DsnRequest $dsn, ContainerBuilder $container): string
54
    {
55 3
        if (!$host = $dsn->getHost()) {
56 1
            throw new InvalidArgumentException('The connection name is required on prime failer DSN');
57
        }
58
59 2
        if (!$path = trim($dsn->getPath(), '/')) {
0 ignored issues
show
Bug introduced by
It seems like $dsn->getPath() can also be of type null; however, parameter $string of trim() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

59
        if (!$path = trim(/** @scrutinizer ignore-type */ $dsn->getPath(), '/')) {
Loading history...
60 1
            throw new InvalidArgumentException('The table name is required on prime failer DSN');
61
        }
62
63 1
        $className = class_exists(DbFailedJobRepository::class)
64 1
            ? DbFailedJobRepository::class
65 1
            : DbFailedJobStorage::class
66
        ;
67
68 1
        $container->register($className, $className)
69 1
            ->setFactory([$className, 'make'])
70 1
            ->setArguments([
71 1
                new Reference('prime'),
72
                [
73 1
                    'connection' => $host,
74 1
                    'table' => $path,
75
                ],
76 1
                $dsn->query('maxRows', 50)
77
            ])
78
        ;
79
80
        // Register init command
81 1
        $container->register(InitCommand::class, InitCommand::class)
82 1
            ->addArgument(new Reference($className))
83 1
            ->addTag('console.command')
84
        ;
85
86 1
        if (is_subclass_of($className, FailedJobRepositoryInterface::class)) {
87 1
            return $className;
88
        }
89
90
        // Compatibility with legacy prime driver : adapt to repository interface
91
        $container->register('bdf_queue.failer.prime_repository', FailedJobRepositoryAdapter::class)
92
            ->setFactory([FailedJobRepositoryAdapter::class, 'adapt'])
93
            ->setArguments([new Reference($className)])
94
        ;
95
96
        return 'bdf_queue.failer.prime_repository';
97
    }
98
}
99