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

FileManager::buildFormatter()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 3
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
ccs 0
cts 3
cp 0
crap 2
1
<?php
2
declare(strict_types=1);
3
4
namespace tkanstantsin\fileupload;
5
6
use League\Flysystem\Filesystem;
7
use tkanstantsin\fileupload\config\Factory as AliasFactory;
8
use tkanstantsin\fileupload\config\InvalidConfigException;
9
use tkanstantsin\fileupload\formatter\Factory as FormatterFactory;
10
use tkanstantsin\fileupload\formatter\File as FileFormatter;
11
use tkanstantsin\fileupload\formatter\icon\IconGenerator;
12
use tkanstantsin\fileupload\model\BaseObject;
13
use tkanstantsin\fileupload\model\IFile;
14
use tkanstantsin\fileupload\saver\Saver;
15
16
/**
17
 * Class FileComponent
18
 */
19
class FileManager extends BaseObject
20
{
21
    /**
22
     * For uploading
23
     * @var Filesystem
24
     */
25
    public $contentFS;
26
    /**
27
     * Web accessible FS
28
     * @var Filesystem
29
     */
30
    public $cacheFS;
31
32
    /**
33
     * @see config\Alias
34
     * @var array
35
     */
36
    public $aliasArray;
37
    /**
38
     * @see config\Alias
39
     * @var array
40
     */
41
    public $defaultAlias = [
42
        'maxSize' => config\Alias::DEFAULT_MAX_SIZE,
43
        'maxCount' => config\Alias::DEFAULT_MAX_COUNT,
44
        'hashMethod' => config\Alias::DEFAULT_HASH_METHOD,
45
        'cacheHashLength' => config\Alias::DEFAULT_HASH_LENGTH,
46
    ];
47
    /**
48
     * @var AliasFactory
49
     */
50
    private $aliasFactory;
51
52
    /**
53
     * Icon set class for icons
54
     * @see formatter\icon\FontAwesome
55
     * @see formatter\icon\ElusiveIcons
56
     * @var string|null
57
     */
58
    public $iconSet;
59
    /**
60
     * @var IconGenerator
61
     */
62
    private $iconGenerator;
63
64
    /**
65
     * @var array
66
     */
67
    public $formatterConfigArray = [];
68
    /**
69
     * @var FormatterFactory
70
     */
71
    private $formatterFactory;
72
73
    /**
74
     * Check initialization parameters and parse configs
75
     * @throws config\InvalidConfigException
76
     */
77 1
    public function init(): void
78
    {
79 1
        parent::init();
80
81 1
        if (!($this->contentFS instanceof Filesystem)) {
82
            throw new InvalidConfigException(sprintf('ContentFS must be instance of %s.', Filesystem::class));
83
        }
84 1
        if (!($this->cacheFS instanceof Filesystem)) {
85
            throw new InvalidConfigException(sprintf('CacheFS must be instance of %s.', Filesystem::class));
86
        }
87
88 1
        $this->iconGenerator = IconGenerator::build($this->iconSet);
89 1
        $this->formatterFactory = new FormatterFactory((array) $this->formatterConfigArray);
90 1
        $this->aliasFactory = AliasFactory::build($this->defaultAlias);
91 1
        $this->aliasFactory->addMultiple($this->aliasArray);
92 1
    }
93
94
95
    /* PROXY methods */
96
97
    /**
98
     * @param string $name
99
     * @return config\Alias
100
     * @throws \RuntimeException
101
     */
102 1
    public function getAliasConfig(string $name): config\Alias
103
    {
104 1
        return $this->aliasFactory->getAliasConfig($name);
105
    }
106
107
    /**
108
     * @param string $name
109
     * @return string|null
110
     * @throws \RuntimeException
111
     */
112
    public function getModelByAlias(string $name): ?string
113
    {
114
        return $this->getAliasConfig($name)->class ?? null;
115
    }
116
117
    /**
118
     * @see IconGenerator::getIcon()
119
     * @param null|string $extension
120
     * @return string
121
     */
122
    public function getIcon(?string $extension): ?string
123
    {
124
        return $this->iconGenerator->getIcon($extension);
125
    }
126
127
128
    /* OTHER methods */
129
130
    /**
131
     * @param IFile $file
132
     * @param string $format
133
     * @param array $formatterConfig
134
     * @return FileFormatter
135
     * @throws \RuntimeException
136
     */
137
    public function buildFormatter(IFile $file, string $format, array $formatterConfig = []): FileFormatter
138
    {
139
        $alias = $this->getAliasConfig($file->getModelAlias());
140
141
        return $this->formatterFactory->build($file, $alias, $this->contentFS, $format, $formatterConfig);
142
    }
143
144
    /**
145
     * Caches file and returns url to it.
146
     * @param IFile $file
147
     * @param string $format
148
     * @param array $formatterConfig
149
     * @return string
150
     * @throws \InvalidArgumentException
151
     * @throws \RuntimeException
152
     * @throws \League\Flysystem\FileNotFoundException
153
     * @throws InvalidConfigException
154
     */
155
    public function getFilePath(IFile $file, string $format, array $formatterConfig = []): string
156
    {
157
        $alias = $this->getAliasConfig($file->getModelAlias());
158
        $formatter = $this->buildFormatter($file, $format, $formatterConfig);
159
160
        if (!$this->cacheFile($file, $alias, $formatter)) {
161
            return $format;
162
        }
163
164
        return $alias->getCachePath($file, $format);
165
    }
166
167
    /**
168
     * Caches file available in web.
169
     *
170
     * @param IFile $file
171
     * @param config\Alias $alias
172
     * @param FileFormatter $formatter
173
     * @return bool
174
     * @throws \InvalidArgumentException
175
     * @throws \League\Flysystem\FileNotFoundException
176
     * @throws InvalidConfigException
177
     */
178
    protected function cacheFile(IFile $file, config\Alias $alias, FileFormatter $formatter): bool
179
    {
180
        $path = $alias->getCachePath($file, $formatter->getName());
181
182
        return (new Saver($file, $this->cacheFS, $path))->save($formatter);
183
    }
184
185
}
186