<?php
namespace App\Service\MemoryStorage;
use App\Entity\PlatformAppEntity;
use phpseclib3\Net\SFTP;
/**
* Class MemoryStorage
* @package App\Service\MemoryStorage
*/
class MemoryStorage
{
private const PATH = '/var/www/fieldbi-storage/data/www/files.fieldbi.com.ua/';
/**
* @var SFTP
*/
private $sftp;
/**
* MemoryStorage constructor.
* @param array $config
* @param SFTP $sftp
*/
public function __construct(array $config, SFTP $sftp)
{
$this->sftp = $sftp;
$this->sftp->login($config['user'], $config['password']);
}
/**
* @param PlatformAppEntity $entity
*/
public function createStorage(PlatformAppEntity $entity): void
{
$pathDirectory = $this->getDirectoryPath($entity->getDomain());
if (!$this->sftp->file_exists($pathDirectory)) {
$this->sftp->mkdir($pathDirectory);
}
}
/**
* @param PlatformAppEntity $entity
*
* @return bool
*/
public function isDirectory(PlatformAppEntity $entity): bool
{
$pathDirectory = $this->getDirectoryPath($entity->getDomain());
return $this->sftp->file_exists($pathDirectory);
}
/**
* @param PlatformAppEntity $entity
* @return float|null
*/
public function sizeMemoryStorage(PlatformAppEntity $entity): ?float
{
$pathDirectory = $this->getDirectoryPath($entity->getDomain());
return (float)$this->sftp->exec("du -m -s $pathDirectory");
}
/**
* @param PlatformAppEntity $entity
* @return bool
*/
public function deleteDirectory(PlatformAppEntity $entity): bool
{
$pathDirectory = $this->getDirectoryPath($entity->getDomain());
return $this->sftp->delete($pathDirectory);
}
/**
* @param string $name
*
* @return string
*/
private function getDirectoryPath(string $name): string
{
return self::PATH . $name;
}
}