Passed
Push — master ( 60c4f2...db8d73 )
by Georgi
02:28
created

FileModal::addContents()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 20
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 10
nc 2
nop 1
dl 0
loc 20
rs 9.9332
c 1
b 0
f 0
1
<?php
2
3
namespace Epesi\FileStorage\Seeds;
4
5
use Epesi\FileStorage\Integration\Joints\FileStorageAccessJoint;
6
use Epesi\FileStorage\Database\Models\File;
7
use Epesi\Core\Helpers\Utils;
0 ignored issues
show
Bug introduced by
The type Epesi\Core\Helpers\Utils was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
use atk4\ui\Modal;
9
use Epesi\FileStorage\Database\Models\FileRemoteAccess;
10
use atk4\ui\jsExpression;
11
use Illuminate\Support\Facades\Auth;
12
13
class FileModal extends Modal
14
{
15
	public $defaultPeriod = '1 weeks';
16
	
17
	public $periodSelection;
18
	
19
	protected $canvas;
20
	
21
	protected $file;
22
	
23
	public function init()
24
	{
25
		$this->title = __('File');
0 ignored issues
show
Documentation Bug introduced by
It seems like __('File') can also be of type array. However, the property $title is declared as type string. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
26
		
27
		parent::init();
28
		
29
		$this->setPeriodSelection();
30
		
31
		$this->set(\Closure::fromCallable([$this, 'addContents']));
0 ignored issues
show
Bug introduced by
Closure::fromCallable(ar...($this, 'addContents')) of type Closure is incompatible with the type array|string expected by parameter $fx of atk4\ui\Modal::set(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

31
		$this->set(/** @scrutinizer ignore-type */ \Closure::fromCallable([$this, 'addContents']));
Loading history...
32
	}
33
	
34
	protected function setPeriodSelection()
35
	{
36
		if (! $this->periodSelection) {
37
			$this->periodSelection = [];
38
			foreach ([1, 2, 3, 4] as $count) {
39
				$this->periodSelection[$count . ' weeks'] = trans_choice('{1} :count week |[2,*] :count weeks', $count);
40
			}
41
		}
42
43
		foreach ($this->periodSelection as $key => $period) {
44
			$default = $key == $this->defaultPeriod? ' (' . __('Default') . ')': '';
0 ignored issues
show
Bug introduced by
Are you sure __('Default') of type array|string can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

44
			$default = $key == $this->defaultPeriod? ' (' . /** @scrutinizer ignore-type */ __('Default') . ')': '';
Loading history...
45
			
46
			$this->periodSelection[$key] = $period . $default;
47
		}
48
	}
49
	
50
	protected function addContents($canvas)
51
	{
52
		$this->canvas = $canvas;
53
		
54
		if (! $this->file()) {
55
			$canvas->set(__('Wrong parameters for file'));
56
			return;
57
		}
58
		
59
		$columns = $canvas->add('Columns');
60
		
61
		$column = $columns->addColumn();
62
63
		$this->addFileDetails($column);	
64
				
65
		$column = $columns->addColumn();
66
		
67
		$this->addFileRemoteLinks($column);			
68
		
69
		$this->addFileControlButtons();
70
	}
71
	
72
	protected function file()
73
	{
74
		return $this->file?: ($this->file = File::get($this->canvas->stickyGET('id')));
75
	}
76
	
77
	protected function addFileDetails($container = null)
78
	{
79
		$container = $container?: $this->canvas;
80
		
81
		$container->add(['Lister', 'template'=> new \atk4\ui\Template('
82
						<div class="item" style="margin: 0 15px 15px 0">
83
							<div class="content"><strong>{$title}</strong>
84
                				<div class="description">{$descr}</div>
85
            				</div>
86
        				</div>{empty}' . __('No file details') . '{/}')])->setSource([
0 ignored issues
show
Bug introduced by
Are you sure __('No file details') of type array|string can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

86
        				</div>{empty}' . /** @scrutinizer ignore-type */ __('No file details') . '{/}')])->setSource([
Loading history...
87
        						['title'=> __('Name'), 'descr'=> $this->file()->name],
88
        						['title'=> __('Size'), 'descr'=> Utils::bytesToHuman($this->file()->content->size)],
89
        ]);
90
	}
91
	
92
	protected function addFileRemoteLinks($container = null) {
93
		$container = $container?: $this->canvas;
94
		
95
		foreach ($this->file()->links()->where('created_by', Auth::id())->get() as $link) {
96
			$container->add(['View', __('Remote access link expiring :expiry', ['expiry' => $link->expires_at])]);
97
			$linkInput = $container->add([new \atk4\ui\FormField\Input(['readonly' => true, 'iconLeft' => 'linkify'])])->set($link->href)->setStyle(['width' => '100%', 'margin-bottom' => '15px']);
98
			
99
			$linkInput->action = new \atk4\ui\Button([
100
					'Copy', 'iconRight' => 'copy', 'attr' => ['data-clipboard-target' => "#{$linkInput->id}_input", 'title' => __('Click to copy link')], 'class' => ['copy-button']
101
			]);
102
			
103
			$linkInput->add(['Button', 'icon'=>'red x icon', 'class' => ['delete-link']], 'AfterAfterInput')->setAttr(['data-id' => $link->id, 'title' => __('Disable link')]);
104
		}
105
		
106
		$container->js(true, new jsExpression('new ClipboardJS(".copy-button")'));
107
		
108
		$container->on('click', '.delete-link', $container->add(['jsCallback', 'postTrigger' => 'link'])->set(function($j, $linkId) {
109
			if (! $link = FileRemoteAccess::find($linkId)) return;
110
			
111
			$link->delete();
112
			
113
			return $this->reload();
114
		}, ['link' => new jsExpression('$(this).data("id")')]));
115
	}
116
	
117
	protected function addFileControlButtons() {
118
		$urls = FileStorageAccessJoint::getActionUrls($this->file());
119
		
120
		if ($urls['preview']?? null) {
121
			$this->canvas->add(['Button', 'class' => ['basic'], 'icon' => 'file alternate outline'])->set(__('View'))->link($urls['preview'])->setAttr(['target' => '_blank']);
122
		}
123
		
124
		if ($urls['download']?? null) {
125
			$this->canvas->add(['Button', 'class' => ['basic'], 'icon' => 'file download'])->set(__('Download'))->link($urls['download']);
126
		}
127
		
128
		$linkControl = $this->canvas->add(['View', 'ui' => 'basic buttons']);
129
		
130
		$linkButton = $linkControl->add(['Button', 'class' => ['basic'], 'icon' => 'linkify'])->set(__('Get Remote Link'));
131
		
132
		$dropdown = $linkControl->add(['DropDownButton', 'class' => ['floating icon button']]);
133
		
134
		$dropdown->setSource($this->periodSelection);
135
		
136
		$dropdown->onChange(function($value) {
137
			$period = array_search($value, $this->periodSelection)?: $this->defaultPeriod;
138
			
139
			FileRemoteAccess::grant($this->file(), $period);
140
			
141
			return $this->reload();
142
		});
143
		
144
		$linkButton->on('click', $dropdown->cb);
145
	}
146
	
147
	protected function reload() {
148
		return new \atk4\ui\jsReload($this->canvas);
149
	}
150
	
151
	public function show($args = [])
152
	{
153
		return parent::show(array_merge([
154
				'id' => new jsExpression('$(this).data("id")')
155
		], $args));
156
	}
157
}