PrimeFailerDriverConfigurator::configure()   A
last analyzed

Complexity

Conditions 5
Paths 6

Size

Total Lines 44
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 25
CRAP Score 5.1811

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 5
eloc 23
c 2
b 0
f 0
nc 6
nop 2
dl 0
loc 44
ccs 25
cts 31
cp 0.8065
crap 5.1811
rs 9.2408
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\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...
8
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...
9
use Bdf\Queue\Failer\FailedJobRepositoryAdapter;
10
use Bdf\Queue\Failer\FailedJobRepositoryInterface;
11
use Symfony\Component\DependencyInjection\ContainerBuilder;
12
use Symfony\Component\DependencyInjection\Reference;
13
14
/**
15
 * Configurator for failer repository using prime.
16
 *
17
 * The package "b2pweb/bdf-queue-prime-adapter" must be installed and
18
 * "b2pweb/bdf-prime-bundle" configure to enable this failer repository
19
 *
20
 * DSN format: "prime://[connection]/[table]?maxRows=[cursorSize]"
21
 * with:
22
 * - [connection] as the prime connection to use for store failed jobs
23
 * - [table] as the table name
24
 * - [cursorSize] (optional): number of loaded jobs each times by the cursor with calling
25
 *                `FailedJobRepositoryInterface::all()` or `search()`. Default value is 50
26
 */
27
final class PrimeFailerDriverConfigurator implements FailerDriverConfiguratorInterface
28
{
29
    /**
30
     * {@inheritdoc}
31
     */
32 3
    public function scheme(): string
33
    {
34 3
        return 'prime';
35
    }
36
37
    /**
38
     * {@inheritdoc}
39
     */
40 3
    public function available(ContainerBuilder $container): bool
41
    {
42 3
        if (!$container->has('prime')) {
43
            return false;
44
        }
45
46 3
        return class_exists(DbFailedJobRepository::class) || class_exists(DbFailedJobStorage::class);
47
    }
48
49
    /**
50
     * {@inheritdoc}
51
     */
52 3
    public function configure(DsnRequest $dsn, ContainerBuilder $container): string
53
    {
54 3
        if (!$host = $dsn->getHost()) {
55 1
            throw new \InvalidArgumentException('The connection name is required on prime failer DSN');
56
        }
57
58 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

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