AbstractHandler::createReader()   B
last analyzed

Complexity

Conditions 6
Paths 2

Size

Total Lines 25
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 18
nc 2
nop 2
dl 0
loc 25
rs 8.439
c 0
b 0
f 0
1
<?php
2
3
namespace AmaTeam\Image\Projection\Type;
4
5
use AmaTeam\Image\Projection\API\ConversionOptionsInterface;
6
use AmaTeam\Image\Projection\API\SpecificationInterface;
7
use AmaTeam\Image\Projection\API\Tile\TileInterface;
8
use AmaTeam\Image\Projection\API\Type\ReaderOptionsInterface;
9
use AmaTeam\Image\Projection\API\Type\ValidatingMappingInterface;
10
use AmaTeam\Image\Projection\Constants;
11
use AmaTeam\Image\Projection\Framework\Validation\ValidationException;
12
use AmaTeam\Image\Projection\Geometry\Box;
13
use AmaTeam\Image\Projection\Image\Manager;
14
use AmaTeam\Image\Projection\Specification;
15
use AmaTeam\Image\Projection\Tile\Loader;
16
use AmaTeam\Image\Projection\Tile\Tile;
17
use AmaTeam\Image\Projection\API\Type\HandlerInterface;
18
use AmaTeam\Image\Projection\API\Type\MappingInterface;
19
use AmaTeam\Image\Projection\API\Type\ReaderInterface;
20
use BadMethodCallException;
21
use League\Flysystem\FilesystemInterface;
22
use Psr\Log\LoggerInterface;
23
use Psr\Log\NullLogger;
24
25
/**
26
 * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
27
 */
28
abstract class AbstractHandler implements HandlerInterface
29
{
30
    /**
31
     * @var Manager
32
     */
33
    private $imageManager;
34
    /**
35
     * @var FilesystemInterface
36
     */
37
    private $filesystem;
38
    /**
39
     * @var LoggerInterface
40
     */
41
    private $logger;
42
43
    /**
44
     * @param FilesystemInterface $filesystem
45
     * @param Manager $imageManager
46
     * @param LoggerInterface|null $logger
47
     */
48
    public function __construct(
49
        FilesystemInterface $filesystem,
50
        Manager $imageManager,
51
        LoggerInterface $logger = null
52
    ) {
53
        $this->filesystem = $filesystem;
54
        $this->imageManager = $imageManager;
55
        $this->logger = $logger ?: new NullLogger();
56
    }
57
58
    /**
59
     * @inheritDoc
60
     */
61
    public function createReader(
62
        SpecificationInterface $specification,
63
        ReaderOptionsInterface $options = null
64
    ) {
65
        $options = $options ?: new ReaderOptions();
66
        $loader = new Loader($this->imageManager, $this->filesystem);
67
        $pattern = $specification->getPattern();
68
        $tiles = $loader->load($pattern);
69
        if (empty($tiles)) {
70
            $message = "Couldn't find any tiles specified by pattern $pattern";
71
            throw new BadMethodCallException($message);
72
        }
73
        $tree = Tile::treeify($tiles);
74
        $face = reset($tree);
75
        $tileSize = $specification->getTileSize();
76
        $tileSize = $tileSize ?: SizeExtractor::extractTileSize($face);
77
        if (!$tileSize) {
78
            throw new ValidationException('Could not compute tile size');
79
        }
80
        $wrapper = $this->wrapSpecification($specification, $tileSize);
81
        $mapping = $this->getMapping();
82
        if ($mapping instanceof ValidatingMappingInterface) {
83
            $mapping->validate($tree, $wrapper);
84
        }
85
        return $this->instantiateReader($wrapper, $mapping, $tree, $options);
86
    }
87
88
    /**
89
     * @inheritDoc
90
     */
91
    public function createGenerator(
92
        ReaderInterface $source,
93
        SpecificationInterface $target,
94
        ConversionOptionsInterface $options = null
95
    ) {
96
        if (!$target->getSize()) {
97
            $message = 'Provided specification doesn\'t contain it\'s size';
98
            throw new BadMethodCallException($message);
99
        }
100
        $mapping = $this->getMapping();
101
        $context = ['target' => $target,];
102
        $this->logger->debug('Creating tile generator for {target}', $context);
103
        return new DefaultGenerator(
104
            $this->imageManager,
105
            new GenerationDetails($source, $mapping, $target, $options->getFilters()),
0 ignored issues
show
Bug introduced by
The method getFilters() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

105
            new GenerationDetails($source, $mapping, $target, $options->/** @scrutinizer ignore-call */ getFilters()),

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
106
            $this->logger
107
        );
108
    }
109
110
    /**
111
     * @param SpecificationInterface $specification
112
     * @param MappingInterface $mapping
113
     * @param TileInterface[][][] $tiles
114
     * @param ReaderOptionsInterface $options
115
     *
116
     * @return ReaderInterface
117
     */
118
    protected function instantiateReader(
119
        SpecificationInterface $specification,
120
        MappingInterface $mapping,
121
        array $tiles,
122
        ReaderOptionsInterface $options
123
    ) {
124
        $mode = $options->getInterpolationMode();
125
        if ($mode === Constants::INTERPOLATION_BILINEAR) {
126
            return new BilinearReader($specification, $mapping, $tiles);
127
        }
128
        return new NearestNeighbourReader($specification, $mapping, $tiles);
129
    }
130
131
    /**
132
     * @param SpecificationInterface $specification
133
     * @param Box $tileSize
134
     * @return SpecificationInterface
135
     */
136
    private function wrapSpecification(
137
        SpecificationInterface $specification,
138
        Box $tileSize
139
    ) {
140
        return new Specification(
141
            $specification->getType(),
142
            $specification->getPattern(),
143
            $tileSize,
144
            $specification->getLayout()
145
        );
146
    }
147
148
    /**
149
     * @return MappingInterface
150
     */
151
    abstract protected function getMapping();
152
}
153