SaveListener::accept()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 7
nc 1
nop 2
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace AmaTeam\Image\Projection\Conversion\Listener;
4
5
use AmaTeam\Image\Projection\API\Conversion\ListenerInterface;
6
use AmaTeam\Image\Projection\API\SpecificationInterface;
7
use AmaTeam\Image\Projection\API\Tile\TileInterface;
8
use AmaTeam\Image\Projection\Image\EncodingOptions;
9
use AmaTeam\Image\Projection\API\Image\Format;
10
use League\Flysystem\FilesystemInterface;
11
use Psr\Log\LoggerInterface;
12
use Psr\Log\NullLogger;
13
14
class SaveListener implements ListenerInterface
15
{
16
    /**
17
     * @var FilesystemInterface
18
     */
19
    private $filesystem;
20
    /**
21
     * @var string
22
     */
23
    private $format;
24
    /**
25
     * @var EncodingOptions
26
     */
27
    private $encoding;
28
    /**
29
     * @var LoggerInterface
30
     */
31
    private $logger;
32
33
    /**
34
     * @param FilesystemInterface $filesystem
35
     * @param string $format
36
     * @param EncodingOptions $encoding
37
     * @param LoggerInterface $logger
38
     */
39
    public function __construct(
40
        FilesystemInterface $filesystem,
41
        $format = Format::JPEG,
42
        EncodingOptions $encoding = null,
43
        LoggerInterface $logger = null
44
    ) {
45
        $this->filesystem = $filesystem;
46
        $this->format = $format;
47
        $this->encoding = $encoding ?: EncodingOptions::defaults();
48
        $this->logger = $logger ?: new NullLogger();
49
    }
50
51
    public function accept(
52
        TileInterface $tile,
53
        SpecificationInterface $specification
54
    ) {
55
        $context = ['position' => $tile->getPosition()];
56
        $this->logger->debug('Saving tile {position}', $context);
57
        $parameters = $tile->getPosition()->toPatternParameters();
58
        $path = (string) $specification->getPattern()->resolve($parameters);
59
        $this->filesystem->put(
60
            (string) $path,
61
            $tile->getImage()->getBinary($this->format, $this->encoding)
62
        );
63
    }
64
}
65