Passed
Push — main ( f4e9d2...4a4379 )
by Jesse
01:41
created

SlidingCratesPuzzleFactory   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 22
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 7
c 1
b 0
f 0
dl 0
loc 22
rs 10
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A make() 0 3 1
A __construct() 0 3 1
A fromString() 0 7 2
1
<?php declare(strict_types=1);
2
3
namespace Stratadox\PuzzleSolver\Puzzle\SlidingCrates;
4
5
use Stratadox\PuzzleSolver\InvalidFormat;
6
use Stratadox\PuzzleSolver\Puzzle;
7
use Stratadox\PuzzleSolver\PuzzleFactory;
8
use function explode;
9
use function substr_count;
10
use function trim;
11
12
final class SlidingCratesPuzzleFactory implements PuzzleFactory
13
{
14
    /** @var string */
15
    private $separator;
16
17
    public function __construct(string $separator)
18
    {
19
        $this->separator = $separator;
20
    }
21
22
    public static function make(): self
23
    {
24
        return new self('TARGET:');
25
    }
26
27
    public function fromString(string $puzzle): Puzzle
28
    {
29
        if (substr_count($puzzle, $this->separator) !== 1) {
30
            throw InvalidFormat::couldNotLoad('sliding crates', $puzzle, "Missing `$this->separator` in input.");
31
        }
32
        [$level, $target] = explode($this->separator, $puzzle);
33
        return SlidingCratesPuzzle::fromString($level, trim($target)[0]);
34
    }
35
}
36