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
bc2358f3
Unverified
Commit
bc2358f3
authored
Sep 21, 2017
by
Nicolas Widart
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Adding method to find a folder.
Adding logic to set the correct folder path for children
parent
a3992a41
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
56 additions
and
1 deletion
+56
-1
EloquentFolderRepository.php
.../Media/Repositories/Eloquent/EloquentFolderRepository.php
+27
-1
FolderRepository.php
Modules/Media/Repositories/FolderRepository.php
+7
-0
EloquentFolderRepositoryTest.php
Modules/Media/Tests/EloquentFolderRepositoryTest.php
+22
-0
No files found.
Modules/Media/Repositories/Eloquent/EloquentFolderRepository.php
View file @
bc2358f3
...
...
@@ -3,17 +3,28 @@
namespace
Modules\Media\Repositories\Eloquent
;
use
Modules\Core\Repositories\Eloquent\EloquentBaseRepository
;
use
Modules\Media\Entities\File
;
use
Modules\Media\Events\FolderIsCreating
;
use
Modules\Media\Events\FolderWasCreated
;
use
Modules\Media\Repositories\FolderRepository
;
class
EloquentFolderRepository
extends
EloquentBaseRepository
implements
FolderRepository
{
/**
* Find a folder by its ID
* @param int $folderId
* @return File
*/
public
function
findFolder
(
int
$folderId
)
:
File
{
return
$this
->
model
->
where
(
'is_folder'
,
1
)
->
where
(
'id'
,
$folderId
)
->
first
();
}
public
function
create
(
$data
)
{
$data
=
[
'filename'
=>
array_get
(
$data
,
'name'
),
'path'
=>
config
(
'asgard.media.config.files-path'
)
.
str_slug
(
array_get
(
$data
,
'name'
)
),
'path'
=>
$this
->
getPath
(
$data
),
'is_folder'
=>
true
,
'folder_id'
=>
array_get
(
$data
,
'parent_id'
),
];
...
...
@@ -24,4 +35,19 @@ class EloquentFolderRepository extends EloquentBaseRepository implements FolderR
return
$folder
;
}
/**
* @param array $data
* @return string
*/
private
function
getPath
(
array
$data
)
:
string
{
if
(
array_key_exists
(
'parent_id'
,
$data
))
{
$parent
=
$this
->
findFolder
(
$data
[
'parent_id'
]);
return
$parent
->
path
->
getRelativeUrl
()
.
'/'
.
str_slug
(
array_get
(
$data
,
'name'
));
}
return
config
(
'asgard.media.config.files-path'
)
.
str_slug
(
array_get
(
$data
,
'name'
));
}
}
Modules/Media/Repositories/FolderRepository.php
View file @
bc2358f3
...
...
@@ -3,7 +3,14 @@
namespace
Modules\Media\Repositories
;
use
Modules\Core\Repositories\BaseRepository
;
use
Modules\Media\Entities\File
;
interface
FolderRepository
extends
BaseRepository
{
/**
* Find a folder by its ID
* @param int $folderId
* @return File
*/
public
function
findFolder
(
int
$folderId
)
:
File
;
}
Modules/Media/Tests/EloquentFolderRepositoryTest.php
View file @
bc2358f3
...
...
@@ -3,6 +3,7 @@
namespace
Modules\Media\Tests
;
use
Illuminate\Support\Facades\Event
;
use
Modules\Media\Entities\File
;
use
Modules\Media\Events\FolderIsCreating
;
use
Modules\Media\Events\FolderWasCreated
;
use
Modules\Media\Repositories\FolderRepository
;
...
...
@@ -89,6 +90,27 @@ final class EloquentFolderRepositoryTest extends MediaTestCase
$this
->
assertTrue
(
$this
->
app
[
'files'
]
->
isDirectory
(
public_path
(
'assets/media/my-folder'
)));
}
/** @test */
public
function
it_can_find_a_folder
()
{
$this
->
folder
->
create
([
'name'
=>
'My Folder'
]);
$folder
=
$this
->
folder
->
findFolder
(
1
);
$this
->
assertInstanceOf
(
File
::
class
,
$folder
);
$this
->
assertEquals
(
1
,
$folder
->
id
);
}
/** @test */
public
function
it_can_create_folders_belonging_to_another_folder
()
{
$this
->
folder
->
create
([
'name'
=>
'Root Folder'
]);
$childFolder
=
$this
->
folder
->
create
([
'name'
=>
'Child folder'
,
'parent_id'
=>
1
]);
$this
->
assertEquals
(
'/assets/media/root-folder/child-folder'
,
$childFolder
->
path
->
getRelativeUrl
());
$this
->
assertTrue
(
$this
->
app
[
'files'
]
->
isDirectory
(
public_path
(
'assets/media/root-folder/child-folder'
)));
}
private
function
resetDatabase
()
{
// Makes sure the migrations table is created
...
...
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