UrlGeneratorAdapterTrait   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Importance

Changes 13
Bugs 0 Features 0
Metric Value
wmc 7
eloc 19
c 13
b 0
f 0
dl 0
loc 47
rs 10

3 Methods

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