AbstractVcrFactory::getRecorder()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 6
c 1
b 0
f 0
dl 0
loc 11
ccs 0
cts 6
cp 0
rs 10
cc 3
nc 3
nop 2
crap 12
1
<?php
2
3
declare(strict_types=1);
4
5
namespace TMV\HTTPlugModule\PluginFactory;
6
7
use Http\Client\Plugin\Vcr\NamingStrategy\NamingStrategyInterface;
8
use Http\Client\Plugin\Vcr\NamingStrategy\PathNamingStrategy;
9
use Http\Client\Plugin\Vcr\Recorder\FilesystemRecorder;
10
use Http\Client\Plugin\Vcr\Recorder\InMemoryRecorder;
11
use Http\Client\Plugin\Vcr\Recorder\PlayerInterface;
12
use Http\Client\Plugin\Vcr\Recorder\RecorderInterface;
13
use Psr\Container\ContainerInterface;
14
15
abstract class AbstractVcrFactory implements PluginFactory
16
{
17
    protected ContainerInterface $container;
18
19
    public function __construct(ContainerInterface $container)
20
    {
21
        $this->container = $container;
22
    }
23
24
    /**
25
     * @param array<string, mixed> $config
26
     */
27
    protected function getRecorder(string $recorder, array $config = []): RecorderInterface
28
    {
29
        switch ($recorder) {
30
            case 'filesystem':
31
                return new FilesystemRecorder($config['fixtures_directory']);
32
33
            case 'in_memory':
34
                return new InMemoryRecorder();
35
        }
36
37
        return $this->container->get($recorder);
38
    }
39
40
    /**
41
     * @param array<string, mixed> $config
42
     */
43
    protected function getPlayer(string $recorder, array $config = []): PlayerInterface
44
    {
45
        switch ($recorder) {
46
            case 'filesystem':
47
                return new FilesystemRecorder($config['fixtures_directory']);
48
49
            case 'in_memory':
50
                return new InMemoryRecorder();
51
        }
52
53
        return $this->container->get($recorder);
54
    }
55
56
    /**
57
     * @param array<string, mixed> $config
58
     */
59
    protected function getNamingStrategy(string $namingStrategy, array $config = []): NamingStrategyInterface
60
    {
61
        if ($namingStrategy === 'default') {
62
            return new PathNamingStrategy($config['naming_strategy_options']);
63
        }
64
65
        return $this->container->get($namingStrategy);
66
    }
67
}
68