Passed
Push — master ( c6992b...62cc34 )
by Kanstantsin
02:33
created

File::afterCacheCallback()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.2559

Importance

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