Test Failed
Push — main ( 1f6f9b...f1a6e0 )
by Sugeng
03:22
created

UrlGeneratorTrait::publicUrl()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 5
c 1
b 0
f 0
dl 0
loc 9
rs 10
cc 1
nc 1
nop 2
1
<?php
2
3
namespace diecoding\flysystem\traits;
4
5
use DateTimeInterface;
6
use yii\helpers\Json;
7
use yii\helpers\Url;
8
9
/**
10
 * Trait UrlGeneratorTrait for Model
11
 * 
12
 * @package diecoding\flysystem\traits
13
 * 
14
 * @method string normalizePath(string $path, \League\Flysystem\PathNormalizer $pathNormalizer = null)
15
 * 
16
 * @link      https://sugengsulistiyawan.my.id/
17
 * @author    Sugeng Sulistiyawan <[email protected]>
18
 * @copyright Copyright (c) 2023
19
 */
20
trait UrlGeneratorTrait
21
{
22
    use EncrypterTrait;
23
24
    /**
25
     * @var string
26
     */
27
    public $action = '/site/file';
28
29
    public function publicUrl(string $path, array $config = []): string
30
    {
31
        $params = [
32
            'path'    => $this->normalizePath($path),
33
            'expires' => 0,
34
            'config'  => $config,
35
        ];
36
37
        return Url::toRoute([$this->action, 'data' => $this->encrypt(Json::encode($params))], true);
38
    }
39
40
    public function temporaryUrl(string $path, DateTimeInterface $expiresAt, array $config = []): string
41
    {
42
        $params = [
43
            'path'    => $this->normalizePath($path),
44
            'expires' => (int) $expiresAt->getTimestamp(),
45
            'config'  => $config,
46
        ];
47
48
        return Url::toRoute([$this->action, 'data' => $this->encrypt(Json::encode($params))], true);
49
    }
50
}
51