ExternalImage::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 3
dl 0
loc 5
ccs 0
cts 5
cp 0
crap 2
rs 10
1
<?php
2
3
/**
4
 * This file is part of PhpAidc LabelPrinter package.
5
 *
6
 * © Appwilio (https://appwilio.com)
7
 * © JhaoDa (https://github.com/jhaoda)
8
 *
9
 * For the full copyright and license information, please view the LICENSE
10
 * file that was distributed with this source code.
11
 */
12
13
declare(strict_types=1);
14
15
namespace PhpAidc\LabelPrinter\Command;
16
17
use PhpAidc\LabelPrinter\Contract\Command;
18
use PhpAidc\LabelPrinter\Command\Concerns\Rotatable;
19
use PhpAidc\LabelPrinter\Command\Concerns\Invertible;
20
use PhpAidc\LabelPrinter\Command\Concerns\PositionAware;
21
22
final class ExternalImage implements Command
23
{
24
    use Rotatable;
25
    use Invertible;
26
    use PositionAware;
27
28
    private $filename;
29
30
    /**
31
     * @param  int                  $x
32
     * @param  int                  $y
33
     * @param  \SplFileInfo|string  $filename
34
     */
35
    public function __construct(int $x, int $y, $filename)
36
    {
37
        $this->x = $x;
38
        $this->y = $y;
39
        $this->filename = $filename;
40
    }
41
42
    public function getData(): string
43
    {
44
        return $this->read($this->filename);
45
    }
46
47
    private function read($data): string
48
    {
49
        if (\is_string($data)) {
50
            $data = new \SplFileInfo($data);
51
        }
52
53
        if ($data instanceof \SplFileInfo) {
54
            if (!$data->isReadable()) {
55
                throw new \RuntimeException();
56
            }
57
58
            return \file_get_contents($data->getRealPath());
59
        }
60
61
        throw new \InvalidArgumentException();
62
    }
63
}
64