Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
P
Platform
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Administrator
Platform
Commits
ad5d2e36
Unverified
Commit
ad5d2e36
authored
Oct 10, 2017
by
Nicolas Widart
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Extracting and testing logic to find a folder or make a root folder to the folder repository
parent
354601f4
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
55 additions
and
24 deletions
+55
-24
MoveMediaController.php
Modules/Media/Http/Controllers/Api/MoveMediaController.php
+1
-24
EloquentFolderRepository.php
.../Media/Repositories/Eloquent/EloquentFolderRepository.php
+30
-0
FolderRepository.php
Modules/Media/Repositories/FolderRepository.php
+8
-0
EloquentFolderRepositoryTest.php
Modules/Media/Tests/EloquentFolderRepositoryTest.php
+16
-0
No files found.
Modules/Media/Http/Controllers/Api/MoveMediaController.php
View file @
ad5d2e36
...
...
@@ -37,7 +37,7 @@ class MoveMediaController extends Controller
public
function
__invoke
(
MoveMediaRequest
$request
)
{
$destination
=
$this
->
getDestinationFolder
(
$request
->
get
(
'destinationFolder'
));
$destination
=
$this
->
folder
->
findFolderOrRoot
(
$request
->
get
(
'destinationFolder'
));
$failedMoves
=
0
;
foreach
(
$request
->
get
(
'files'
)
as
$file
)
{
...
...
@@ -50,27 +50,4 @@ class MoveMediaController extends Controller
'folder_id'
=>
$destination
->
id
,
]);
}
private
function
makeRootFolder
()
:
File
{
return
new
File
([
'id'
=>
0
,
'folder_id'
=>
0
,
'path'
=>
config
(
'asgard.media.config.files-path'
),
]);
}
/**
* @param int $destinationFolderId
* @return File
*/
protected
function
getDestinationFolder
(
$destinationFolderId
)
:
File
{
$destination
=
$this
->
folder
->
findFolder
(
$destinationFolderId
);
if
(
$destination
===
null
)
{
$destination
=
$this
->
makeRootFolder
();
}
return
$destination
;
}
}
Modules/Media/Repositories/Eloquent/EloquentFolderRepository.php
View file @
ad5d2e36
...
...
@@ -107,6 +107,23 @@ class EloquentFolderRepository extends EloquentBaseRepository implements FolderR
return
$folder
;
}
/**
* Find the folder by ID or return a root folder
* which is an instantiated File class
* @param int $folderId
* @return File
*/
public
function
findFolderOrRoot
(
$folderId
)
:
File
{
$destination
=
$this
->
findFolder
(
$folderId
);
if
(
$destination
===
null
)
{
$destination
=
$this
->
makeRootFolder
();
}
return
$destination
;
}
private
function
getNewPathFor
(
string
$filename
,
File
$folder
)
{
return
$this
->
removeDoubleSlashes
(
$folder
->
path
->
getRelativeUrl
()
.
'/'
.
str_slug
(
$filename
));
...
...
@@ -132,4 +149,17 @@ class EloquentFolderRepository extends EloquentBaseRepository implements FolderR
return
config
(
'asgard.media.config.files-path'
)
.
str_slug
(
array_get
(
$data
,
'name'
));
}
/**
* Create an instantiated File entity, appointed as root
* @return File
*/
private
function
makeRootFolder
()
:
File
{
return
new
File
([
'id'
=>
0
,
'folder_id'
=>
0
,
'path'
=>
config
(
'asgard.media.config.files-path'
),
]);
}
}
Modules/Media/Repositories/FolderRepository.php
View file @
ad5d2e36
...
...
@@ -25,4 +25,12 @@ interface FolderRepository extends BaseRepository
public
function
allNested
()
:
NestedFoldersCollection
;
public
function
move
(
File
$folder
,
File
$destination
)
:
File
;
/**
* Find the folder by ID or return a root folder
* which is an instantiated File class
* @param int $folderId
* @return File
*/
public
function
findFolderOrRoot
(
$folderId
)
:
File
;
}
Modules/Media/Tests/EloquentFolderRepositoryTest.php
View file @
ad5d2e36
...
...
@@ -330,6 +330,22 @@ final class EloquentFolderRepositoryTest extends MediaTestCase
$this
->
assertCount
(
2
,
File
::
all
());
}
/** @test */
public
function
it_finds_a_folder_or_returns_a_root_folder
()
{
$this
->
folder
->
create
([
'name'
=>
'My Folder'
]);
$foundFolder
=
$this
->
folder
->
findFolderOrRoot
(
1
);
$this
->
assertInstanceOf
(
File
::
class
,
$foundFolder
);
$this
->
assertEquals
(
'My Folder'
,
$foundFolder
->
filename
);
$this
->
assertTrue
(
$foundFolder
->
exists
);
$notFolder
=
$this
->
folder
->
findFolderOrRoot
(
0
);
$this
->
assertFalse
(
$notFolder
->
exists
);
$this
->
assertEquals
(
0
,
$notFolder
->
id
);
$this
->
assertEquals
(
0
,
$notFolder
->
folder_id
);
$this
->
assertEquals
(
config
(
'asgard.media.config.files-path'
),
$notFolder
->
path
->
getRelativeUrl
());
}
private
function
createFile
(
$fileName
=
'random/name.jpg'
)
{
return
File
::
create
([
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment