Passed
Push — master ( f5f55f...a1640c )
by Kanstantsin
03:13
created

FileManager::cacheFile()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

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