Passed
Pull Request — master (#2)
by Vincent
04:15 queued 01:21
created

PrimeFailerFactory::supported()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 1
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 2
rs 10
1
<?php
2
3
namespace Bdf\QueueBundle\FailerFactory;
4
5
use Bdf\Dsn\DsnRequest;
6
use Bdf\Prime\ServiceLocator;
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 InvalidArgumentException;
12
13
/**
14
 * Factory for storage using prime
15
 *
16
 * The package "b2pweb/bdf-queue-prime-adapter" must be installed and
17
 * "b2pweb/bdf-prime-bundle" configure to enable this failer repository
18
 *
19
 * DSN format: "prime://[connection]/[table]?maxRows=[cursorSize]"
20
 * with:
21
 * - [connection] as the prime connection to use for store failed jobs
22
 * - [table] as the table name
23
 * - [cursorSize] (optional): number of loaded jobs each times by the cursor with calling
24
 *                `FailedJobRepositoryInterface::all()` or `search()`. Default value is 50
25
 */
26
final class PrimeFailerFactory implements FailerDsnFactoryInterface
27
{
28
    /**
29
     * @var ServiceLocator
30
     */
31
    private $prime;
32
33
    /**
34
     * @param ServiceLocator $prime
35
     */
36 7
    public function __construct(ServiceLocator $prime)
37
    {
38 7
        $this->prime = $prime;
39 7
    }
40
41
    /**
42
     * {@inheritdoc}
43
     */
44 4
    public function scheme(): string
45
    {
46 4
        return 'prime';
47
    }
48
49
    /**
50
     * {@inheritdoc}
51
     */
52 4
    public function create(DsnRequest $dsn): FailedJobRepositoryInterface
53
    {
54 4
        if (!$host = $dsn->getHost()) {
55 1
            throw new InvalidArgumentException('The connection name is required on prime failer DSN');
56
        }
57
58 3
        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
        $schema = [
63 2
            'connection' => $host,
64 2
            'table' => $path
65
        ];
66 2
        $maxRows = (int) $dsn->query('maxRows', 50);
67
68 2
        if (class_exists(DbFailedJobRepository::class)) {
69
            return DbFailedJobRepository::make($this->prime, $schema, $maxRows);
70
        } else {
71 2
            return FailedJobRepositoryAdapter::adapt(DbFailedJobStorage::make($this->prime, $schema, $maxRows));
72
        }
73
    }
74
75
    /**
76
     * Check if the prime repository is supported (i.e. package "b2pweb/bdf-queue-prime-adapter" is installed)
77
     *
78
     * @return bool
79
     */
80 2
    public static function supported(): bool
81
    {
82 2
        return class_exists(DbFailedJobRepository::class) || class_exists(DbFailedJobStorage::class);
83
    }
84
}
85