Skip to content

Commit a4312c9

Browse files
authored
Merge pull request #12 from cesargb/actions
Add actions
2 parents da296d1 + 8101f4e commit a4312c9

File tree

5 files changed

+176
-14
lines changed

5 files changed

+176
-14
lines changed

README.md

Lines changed: 37 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,13 @@ Each MagicLink is associated with an action, which is what will be performed
5959
once the link is visited.
6060

6161
* [Login Action](#login-action)
62+
* [Download file Action](#download-file-action)
63+
* [View Action](#view-action)
6264
* [Http Reponse Action](#http-response-action)
6365

6466
### Login Action
6567

66-
Through the `LoginAction` action, you can log in to the application using the generated link
67-
by `MagicLink`.
68+
Through the `LoginAction` action, you can log in to the application using the generated link by `MagicLink`.
6869

6970
Your constructor supports the user who will login. Optionally we can specify
7071
the [HTTP response](https://laravel.com/docs/master/responses) using the
@@ -99,6 +100,40 @@ $urlShowView = MagicLink::create(
99100
)->url;
100101
```
101102

103+
### Download file Action
104+
105+
This action, `DownloadFileAction`, permit create a link to download a private file.
106+
107+
The constructor require the file path.
108+
109+
Example:
110+
111+
```php
112+
use MagicLink\Actions\DownloadFileAction;
113+
use MagicLink\MagicLink;
114+
115+
// Url to download the file storage_app('private_document.pdf')
116+
$url = MagicLink::create(new DownloadFileAction('private_document.pdf'))->url;
117+
```
118+
119+
### View Action
120+
121+
With the action `ViewAction`, you can provide access to the view. You can use
122+
in his constructor the same arguments than method `view` of Laravel.
123+
124+
Example:
125+
126+
```php
127+
use MagicLink\Actions\ViewAction;
128+
use MagicLink\MagicLink;
129+
130+
// Url to view a internal.blade.php
131+
$url = MagicLink::create(new ViewAction('internal', [
132+
'data' => 'Your private custom content',
133+
]))->url;
134+
```
135+
136+
102137
### Http Response Action
103138

104139
Through the `ResponseAction` action we can access private content without need
@@ -112,18 +147,6 @@ Examples:
112147
use MagicLink\Actions\ResponseAction;
113148
use MagicLink\MagicLink;
114149

115-
$urlToViewContenct = MagicLink::create(
116-
new ResponseAction(
117-
view('promotion.code', ['code' => 'YOUR_CODE'])
118-
)
119-
)->url;
120-
121-
$urlToDownLoadFile = MagicLink::create(
122-
new ResponseAction(function () {
123-
return Storage::download('private/docs.pdf');
124-
})
125-
)->url;
126-
127150
$urlToCustomFunction = MagicLink::create(
128151
new ResponseAction(function () {
129152
Auth::login(User::first());

src/Actions/DownloadFileAction.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
namespace MagicLink\Actions;
4+
5+
use Illuminate\Support\Facades\Storage;
6+
7+
class DownloadFileAction implements ActionInterface
8+
{
9+
protected $path;
10+
11+
protected $name;
12+
13+
protected $headers;
14+
15+
/**
16+
* Constructor to action.
17+
*
18+
* @param string $path
19+
* @param string|null $name
20+
* @param array $headers
21+
* @return void
22+
*/
23+
public function __construct(string $path, ?string $name = null, array $headers = [])
24+
{
25+
$this->path = $path;
26+
$this->name = $name;
27+
$this->headers = $headers;
28+
}
29+
30+
/**
31+
* Execute Action.
32+
*/
33+
public function run()
34+
{
35+
return Storage::download($this->path, $this->name, $this->headers);
36+
}
37+
}

src/Actions/ViewAction.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
namespace MagicLink\Actions;
4+
5+
class ViewAction implements ActionInterface
6+
{
7+
protected $view;
8+
9+
protected $data;
10+
11+
protected $mergeData;
12+
13+
/**
14+
* Constructor action.
15+
*
16+
* @param string $view
17+
* @param \Illuminate\Contracts\Support\Arrayable|array $data
18+
* @param array $mergeData
19+
*/
20+
public function __construct(string $view, $data = [], $mergeData = [])
21+
{
22+
$this->view = $view;
23+
24+
$this->data = $data;
25+
26+
$this->mergeData = $mergeData;
27+
}
28+
29+
/**
30+
* Execute Action.
31+
*/
32+
public function run()
33+
{
34+
return view($this->view, $this->data, $this->mergeData);
35+
}
36+
}

tests/Actions/DownloadFileTest.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
namespace MagicLink\Test\Actions;
4+
5+
use MagicLink\Actions\DownloadFileAction;
6+
use MagicLink\MagicLink;
7+
use MagicLink\Test\TestCase;
8+
9+
class DownloadFileTest extends TestCase
10+
{
11+
public function test_download_file()
12+
{
13+
$magiclink = MagicLink::create(new DownloadFileAction('text.txt'));
14+
15+
$this->get($magiclink->url)
16+
->assertStatus(200)
17+
->assertHeader(
18+
'content-disposition',
19+
'attachment; filename=text.txt'
20+
);
21+
}
22+
23+
public function test_download_file_with_custom_name()
24+
{
25+
$magiclink = MagicLink::create(
26+
new DownloadFileAction('text.txt', 'other.txt')
27+
);
28+
29+
$this->get($magiclink->url)
30+
->assertStatus(200)
31+
->assertHeader(
32+
'content-disposition',
33+
'attachment; filename=other.txt'
34+
);
35+
}
36+
}

tests/Actions/ViewTest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace MagicLink\Test\Actions;
4+
5+
use MagicLink\Actions\ViewAction;
6+
use MagicLink\MagicLink;
7+
use MagicLink\Test\TestCase;
8+
9+
class ViewTest extends TestCase
10+
{
11+
public function test_view()
12+
{
13+
$magiclink = MagicLink::create(new ViewAction('view'));
14+
15+
$this->get($magiclink->url)
16+
->assertStatus(200)
17+
->assertSeeText('This is a tests view');
18+
}
19+
20+
public function test_view_with_data()
21+
{
22+
$magiclink = MagicLink::create(
23+
new ViewAction('data', ['data' => 'Lorem, ipsum dolor.'])
24+
);
25+
26+
$this->get($magiclink->url)
27+
->assertStatus(200)
28+
->assertSeeText('Lorem, ipsum dolor.');
29+
}
30+
}

0 commit comments

Comments
 (0)