Skip to content

Commit

Permalink
added AssetService class
Browse files Browse the repository at this point in the history
  • Loading branch information
wpeisert committed Jan 29, 2018
1 parent 518982a commit 3473290
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/PimcoreDevkitBundle/Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ services:
PimcoreDevkitBundle\Service\UserService: ~
PimcoreDevkitBundle\Service\WorkspaceService: ~
PimcoreDevkitBundle\Service\DocTypeService: ~
PimcoreDevkitBundle\Service\AssetService: ~
57 changes: 57 additions & 0 deletions src/PimcoreDevkitBundle/Service/AssetService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php
/**
* @date        29/01/2018
* @author      Wojciech Peisert <wpeisert@divante.pl>
* @copyright   Copyright (c) 2018 DIVANTE (http://divante.pl)
*/

declare(strict_types=1);

namespace PimcoreDevkitBundle\Service;

use Pimcore\Model\Asset\Folder;
use Pimcore\Model\Asset;

/**
* Class AssetService
*
* @package PimcoreDevkitBundle\Service
*/
class AssetService
{
/**
* Returns asset folder. If not existent, creates it.
*
* @param int $parentId
* @param string $key
* @return Folder
* @internal param string $name
*/
public function getOrCreateAssetFolder($parentId, $key)
{
/** @var Asset $parent */
$parent = Asset::getById($parentId);
$key = Asset\Service::getValidKey($key, 'folder');
$path = $parent->getRealFullPath() . '/' . $key;

$folder = Folder::getByPath($path);
if (!$folder instanceof Folder) {
$folder = Folder::create(
$parentId,
[
'parentId' => $parentId,
'creationDate' => time(),
'userOwner' => 0,
'userModification' => 0,
'path' => $key,
'published' => true,
'type' => 'folder',
'locked' => true,
'filename' => $key,
]
);
}

return $folder;
}
}

0 comments on commit 3473290

Please sign in to comment.