Passed
Push — main ( d28073...f4e9d2 )
by Jesse
01:39
created

DebugLoggerFactory::make()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 4
dl 0
loc 7
rs 10
1
<?php declare(strict_types=1);
2
3
namespace Stratadox\PuzzleSolver\SearchStrategy;
4
5
use Stratadox\PuzzleSolver\Puzzle;
6
use function fopen;
7
use function str_repeat;
8
use const PHP_EOL;
9
10
/**
11
 * Factory for @see DebugLogger
12
 *
13
 * @author Stratadox
14
 */
15
final class DebugLoggerFactory implements SearchStrategyFactory
16
{
17
    /** @var SearchStrategyFactory */
18
    private $factory;
19
    /** @var int */
20
    private $timeout;
21
    /** @var string */
22
    private $separator;
23
    /** @var string */
24
    private $output;
25
26
    public function __construct(
27
        SearchStrategyFactory $factory,
28
        int $timeout,
29
        string $separator,
30
        string $output
31
    ) {
32
        $this->factory = $factory;
33
        $this->timeout = $timeout;
34
        $this->separator = $separator;
35
        $this->output = $output;
36
    }
37
38
    public static function withTimeout(
39
        int $timeout,
40
        SearchStrategyFactory $factory,
41
        string $output = 'php://stdout'
42
    ): SearchStrategyFactory {
43
        return new self($factory, $timeout, str_repeat(PHP_EOL, 20), $output);
44
    }
45
46
    public static function make(
47
        int $timeout,
48
        SearchStrategyFactory $factory,
49
        string $separator,
50
        string $outputFile
51
    ): SearchStrategyFactory {
52
        return new self($factory, $timeout, $separator, $outputFile);
53
    }
54
55
    public function begin(Puzzle $puzzle): SearchStrategy
56
    {
57
        return new DebugLogger(
58
            $this->factory->begin($puzzle),
59
            $this->timeout,
60
            $this->separator,
61
            fopen($this->output, 'wb')
62
        );
63
    }
64
}
65