oms-Media/Admin/Installer.php
2020-12-05 21:39:54 +01:00

95 lines
2.6 KiB
PHP
Executable File

<?php
/**
* Orange Management
*
* PHP Version 8.0
*
* @package Modules\Media\Admin
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link https://orange-management.org
*/
declare(strict_types=1);
namespace Modules\Media\Admin;
use Modules\Admin\Models\NullAccount;
use Modules\Media\Models\Collection;
use Modules\Media\Models\CollectionMapper;
use phpOMS\DataStorage\Database\DatabasePool;
use phpOMS\Module\InstallerAbstract;
use phpOMS\System\File\PathException;
/**
* Installer class.
*
* @package Modules\Media\Admin
* @license OMS License 1.0
* @link https://orange-management.org
* @since 1.0.0
*/
final class Installer extends InstallerAbstract
{
/**
* Install data from providing modules.
*
* @param DatabasePool $dbPool Database pool
* @param array $data Module info
*
* @return void
*
* @throws PathException This exception is thrown if the Navigation install file couldn't be found
* @throws \Exception This exception is thrown if the Navigation install file is invalid json
*
* @since 1.0.0
*/
public static function installExternal(DatabasePool $dbPool, array $data) : void
{
try {
$dbPool->get()->con->query('select 1 from `media`');
} catch (\Exception $e) {
return; // @codeCoverageIgnore
}
if (!\is_file($data['path'] ?? '')) {
throw new PathException($data['path'] ?? '');
}
$mediaFile = \file_get_contents($data['path'] ?? '');
if ($mediaFile === false) {
throw new PathException($data['path'] ?? ''); // @codeCoverageIgnore
}
$mediaData = \json_decode($mediaFile, true) ?? [];
if ($mediaData === false) {
throw new \Exception(); // @codeCoverageIgnore
}
foreach ($mediaData as $media) {
self::installMedia($dbPool, $media);
}
}
/**
* Install media element.
*
* @param DatabasePool $dbPool Database instance
* @param array $data Media info
*
* @return void
*
* @since 1.0.0
*/
private static function installMedia($dbPool, $data) : void
{
$collection = new Collection();
$collection->name = (string) $data['name'] ?? '';
$collection->setVirtualPath((string) $data['virtualPath'] ?? '/');
$collection->setPath((string) ($data['path'] ?? '/Modules/Media/Files/' . ((string) $data['name'] ?? '')));
$collection->createdBy = new NullAccount((int) $data['user'] ?? 1);
CollectionMapper::create($collection);
}
}