Passed
Push — main ( c0a77e...cc7e6c )
by Sugeng
04:00
created

UrlGeneratorAdapterTrait::generateUrlAction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
c 1
b 0
f 0
dl 0
loc 6
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
namespace diecoding\flysystem\traits;
4
5
use DateTimeInterface;
6
use diecoding\flysystem\AbstractComponent;
7
use diecoding\flysystem\actions\FileAction;
8
use League\Flysystem\Config;
9
use League\Flysystem\PathPrefixer;
10
use yii\helpers\Json;
11
use yii\helpers\Url;
12
13
/**
14
 * Trait UrlGeneratorAdapterTrait for Adapter
15
 * 
16
 * @link      https://sugengsulistiyawan.my.id/
17
 * @author    Sugeng Sulistiyawan <[email protected]>
18
 * @copyright Copyright (c) 2023
19
 */
20
trait UrlGeneratorAdapterTrait
21
{
22
    /**
23
     * @var AbstractComponent
24
     */
25
    public $component;
26
27
    /**
28
     * @var bool
29
     */
30
    protected $skipPrefixer = false;
31
32
    public function publicUrl(string $path, /** @scrutinizer ignore-unused */Config $config): string
33
    {
34
        // TODO: Use absolute path and don't encrypt
35
        if ($this->skipPrefixer !== false && $this->component->prefix) {
36
            $prefixer = new PathPrefixer((string) $this->component->prefix);
37
            $path = $prefixer->stripPrefix($path);
38
        }
39
        $params = [
40
            'path' => $path,
41
            'expires' => 0,
42
        ];
43
44
        return $this->generateUrlAction($params);
45
    }
46
47
    public function temporaryUrl(string $path, DateTimeInterface $expiresAt, /** @scrutinizer ignore-unused */Config $config): string
48
    {
49
        // TODO: Use absolute path and don't encrypt
50
        if ($this->skipPrefixer !== false && $this->component->prefix) {
51
            $prefixer = new PathPrefixer((string) $this->component->prefix);
52
            $path = $prefixer->stripPrefix($path);
53
        }
54
        $params = [
55
            'path' => $path,
56
            'expires' => (int) $expiresAt->getTimestamp(),
57
        ];
58
59
        return $this->generateUrlAction($params);
60
    }
61
62
    private function generateUrlAction(array $params): string
63
    {
64
        $attachmentName = (string) pathinfo($params['path'], PATHINFO_BASENAME);
65
        $data = $this->component->/** @scrutinizer ignore-call */encrypt(Json::encode($params)) . FileAction::FILENAME_SEPARATOR . $attachmentName;
66
67
        return Url::toRoute([$this->component->action, 'data' => $data], true);
68
    }
69
}
70