From 34732907fc03278ba6529ee68252e14c0582c781 Mon Sep 17 00:00:00 2001 From: Wojciech Peisert Date: Mon, 29 Jan 2018 16:59:31 +0100 Subject: [PATCH] added AssetService class --- .../Resources/config/services.yml | 1 + .../Service/AssetService.php | 57 +++++++++++++++++++ 2 files changed, 58 insertions(+) create mode 100644 src/PimcoreDevkitBundle/Service/AssetService.php diff --git a/src/PimcoreDevkitBundle/Resources/config/services.yml b/src/PimcoreDevkitBundle/Resources/config/services.yml index ba13520..541b37f 100644 --- a/src/PimcoreDevkitBundle/Resources/config/services.yml +++ b/src/PimcoreDevkitBundle/Resources/config/services.yml @@ -13,3 +13,4 @@ services: PimcoreDevkitBundle\Service\UserService: ~ PimcoreDevkitBundle\Service\WorkspaceService: ~ PimcoreDevkitBundle\Service\DocTypeService: ~ + PimcoreDevkitBundle\Service\AssetService: ~ diff --git a/src/PimcoreDevkitBundle/Service/AssetService.php b/src/PimcoreDevkitBundle/Service/AssetService.php new file mode 100644 index 0000000..8169132 --- /dev/null +++ b/src/PimcoreDevkitBundle/Service/AssetService.php @@ -0,0 +1,57 @@ + + * @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; + } +}