Passed
Push — master ( 960b9d...47eb91 )
by Kanstantsin
03:05
created

File::getContentInternal()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
ccs 0
cts 2
cp 0
crap 2
1
<?php
2
declare(strict_types=1);
3
4
namespace tkanstantsin\fileupload\formatter;
5
6
use League\Flysystem\FilesystemInterface;
7
use tkanstantsin\fileupload\config\InvalidConfigException;
8
use tkanstantsin\fileupload\formatter\adapter\IFormatAdapter;
9
use tkanstantsin\fileupload\model\BaseObject;
10
use tkanstantsin\fileupload\model\Container;
11
use tkanstantsin\fileupload\model\IFile;
12
13
/**
14
 * Class FileProcessor
15
 * TODO: create callbacks or interfaces for getting customized file name or
16
 * filepath
17
 */
18
class File extends BaseObject
19
{
20
    /**
21
     * Additional dynamic config for processor class.
22
     * @var array
23
     */
24
    public $config = [];
25
    /**
26
     * @var IFormatAdapter[]|array
27
     */
28
    public $formatAdapterArray = [];
29
30
    /**
31
     * @see Factory::DEFAULT_FORMATTER_ARRAY
32
     * @example file, _normal, _product_preview
33
     * @var string
34
     */
35
    public $name;
36
    /**
37
     * Path to original file in contentFS
38
     * @var string
39
     */
40
    public $path;
41
42
    /**
43
     * @var IFile
44
     */
45
    protected $file;
46
    /**
47
     * @var FilesystemInterface
48
     */
49
    protected $filesystem;
50
51
    /**
52
     * FileProcessor constructor.
53
     * @param IFile $file
54
     * @param FilesystemInterface $filesystem
55
     * @param array $config
56
     * @throws \tkanstantsin\fileupload\config\InvalidConfigException
57
     * @throws \ReflectionException
58
     */
59 1
    public function __construct(IFile $file, FilesystemInterface $filesystem, array $config = [])
60
    {
61 1
        $this->file = $file;
62 1
        $this->filesystem = $filesystem;
63 1
        parent::__construct($config);
64
65 1
        $this->init();
66 1
    }
67
68
    /**
69
     * Initialize app
70
     * @throws InvalidConfigException
71
     * @throws \ReflectionException
72
     */
73 1
    public function init(): void
74
    {
75 1
        parent::init();
76
77 1
        if ($this->name === null || !\is_string($this->name) || $this->name === '') {
78
            throw new InvalidConfigException('Formatter name must be defined');
79
        }
80 1
        if ($this->path === null) {
81
            throw new InvalidConfigException('File path property must be defined and be not empty');
82
        }
83 1
        foreach ($this->formatAdapterArray as $i => $formatAdapter) {
84
            $this->formatAdapterArray[$i] = Container::createObject($formatAdapter);
85
86
            if (!($this->formatAdapterArray[$i] instanceof IFormatAdapter)) {
87
                throw new InvalidConfigException(sprintf('Format adapter must be instance of %s.', IFormatAdapter::class));
88
            }
89
        }
90 1
    }
91
92
    /**
93
     * @return string
94
     */
95
    public function getName(): string
96
    {
97
        return $this->name;
98
    }
99
100
    /**
101
     * @return resource|string|bool
102
     * @throws \League\Flysystem\FileNotFoundException
103
     */
104
    public function getContent()
105
    {
106
        if (!$this->filesystem->has($this->path)) {
107
            return false;
108
        }
109
110
        $content = $this->getContentInternal();
111
        foreach ($this->formatAdapterArray as $formatAdapter) {
112
            $content = $formatAdapter->exec($this->file, $content);
113
        }
114
115
        return $content;
116
    }
117
118
    /**
119
     * @return resource|bool
120
     * @throws \League\Flysystem\FileNotFoundException
121
     */
122
    protected function getContentInternal()
123
    {
124
        return $this->filesystem->readStream($this->path);
125
    }
126
}