Passed
Push — master ( 30b5af...c8fd96 )
by Peter
04:21
created

File::getSharedData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 7
nc 1
nop 0
dl 0
loc 13
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AbterPhp\Files\Http\Controllers\Api;
6
7
use AbterPhp\Files\Service\Execute\Api\File as RepoService;
8
use AbterPhp\Framework\Databases\Queries\FoundRows;
9
use AbterPhp\Framework\Http\Controllers\Admin\ApiAbstract;
10
use League\Flysystem\Filesystem;
11
use Psr\Log\LoggerInterface;
12
13
class File extends ApiAbstract
14
{
15
    const FILENAMESYSTEM_NAME_LENGTH = 6;
16
17
    const ENTITY_SINGULAR = 'file';
18
    const ENTITY_PLURAL   = 'files';
19
20
    /**
21
     * @var Filesystem
22
     */
23
    protected $filesystem;
24
25
    /**
26
     * File constructor.
27
     *
28
     * @param LoggerInterface $logger
29
     * @param RepoService     $repoService
30
     * @param FoundRows       $foundRows
31
     * @param Filesystem      $filesystem
32
     */
33
    public function __construct(
34
        LoggerInterface $logger,
35
        RepoService $repoService,
36
        FoundRows $foundRows,
37
        Filesystem $filesystem
38
    ) {
39
        parent::__construct($logger, $repoService, $foundRows);
40
41
        $this->filesystem = $filesystem;
42
    }
43
44
    /**
45
     * @return array
46
     */
47
    public function getSharedData(): array
48
    {
49
        $data = $this->request->getJsonBody();
50
51
        $path    = \bin2hex(\random_bytes(static::FILENAMESYSTEM_NAME_LENGTH));
52
        $content = base64_decode($data['data'], true);
53
54
        $this->filesystem->write($path, $content);
55
56
        $data['filesystem_name'] = $path;
57
        $data['public_name']     = $data['name'];
58
59
        return $data;
60
    }
61
}
62