Completed
Push — master ( a0bb08...bcf055 )
by Kanstantsin
03:07
created

File::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 3
dl 0
loc 5
ccs 4
cts 4
cp 1
crap 1
rs 9.4285
c 0
b 0
f 0
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 1
    }
65
66
    /**
67
     * Initialize app
68
     * @throws InvalidConfigException
69
     * @throws \ReflectionException
70
     */
71 1
    public function init(): void
72
    {
73 1
        parent::init();
74
75 1
        if ($this->name === null || !\is_string($this->name) || $this->name === '') {
76
            throw new InvalidConfigException('Formatter name must be defined');
77
        }
78 1
        if ($this->path === null) {
79
            throw new InvalidConfigException('File path property must be defined and be not empty');
80
        }
81 1
        foreach ($this->formatAdapterArray as $i => $formatAdapter) {
82
            $this->formatAdapterArray[$i] = Container::createObject($formatAdapter);
83
84
            if (!($this->formatAdapterArray[$i] instanceof IFormatAdapter)) {
85
                throw new InvalidConfigException(sprintf('Format adapter must be instance of %s.', IFormatAdapter::class));
86
            }
87
        }
88 1
    }
89
90
    /**
91
     * @return string
92
     */
93
    public function getName(): string
94
    {
95
        return $this->name;
96
    }
97
98
    /**
99
     * @return resource|string|bool
100
     * @throws \League\Flysystem\FileNotFoundException
101
     */
102
    public function getContent()
103
    {
104
        if (!$this->filesystem->has($this->path)) {
105
            return false;
106
        }
107
108
        $content = $this->getContentInternal();
109
        foreach ($this->formatAdapterArray as $formatAdapter) {
110
            $content = $formatAdapter->exec($this->file, $content);
111
        }
112
113
        return $content;
114
    }
115
116
    /**
117
     * @return resource|bool
118
     * @throws \League\Flysystem\FileNotFoundException
119
     */
120
    protected function getContentInternal()
121
    {
122
        return $this->filesystem->readStream($this->path);
123
    }
124
}