src/Service/MemoryStorage/MemoryStorage.php line 27

Open in your IDE?
  1. <?php
  2. namespace App\Service\MemoryStorage;
  3. use App\Entity\PlatformAppEntity;
  4. use phpseclib3\Net\SFTP;
  5. /**
  6.  * Class MemoryStorage
  7.  * @package App\Service\MemoryStorage
  8.  */
  9. class MemoryStorage
  10. {
  11.     private const PATH '/var/www/fieldbi-storage/data/www/files.fieldbi.com.ua/';
  12.     /**
  13.      * @var SFTP
  14.      */
  15.     private $sftp;
  16.     /**
  17.      * MemoryStorage constructor.
  18.      * @param array $config
  19.      * @param SFTP $sftp
  20.      */
  21.     public function __construct(array $configSFTP $sftp)
  22.     {
  23.         $this->sftp $sftp;
  24.         $this->sftp->login($config['user'], $config['password']);
  25.     }
  26.     /**
  27.      * @param PlatformAppEntity $entity
  28.      */
  29.     public function createStorage(PlatformAppEntity $entity): void
  30.     {
  31.         $pathDirectory $this->getDirectoryPath($entity->getDomain());
  32.         if (!$this->sftp->file_exists($pathDirectory)) {
  33.             $this->sftp->mkdir($pathDirectory);
  34.         }
  35.     }
  36.     /**
  37.      * @param PlatformAppEntity $entity
  38.      *
  39.      * @return bool
  40.      */
  41.     public function isDirectory(PlatformAppEntity $entity): bool
  42.     {
  43.         $pathDirectory $this->getDirectoryPath($entity->getDomain());
  44.         return $this->sftp->file_exists($pathDirectory);
  45.     }
  46.     /**
  47.      * @param PlatformAppEntity $entity
  48.      * @return float|null
  49.      */
  50.     public function sizeMemoryStorage(PlatformAppEntity $entity): ?float
  51.     {
  52.         $pathDirectory $this->getDirectoryPath($entity->getDomain());
  53.         return (float)$this->sftp->exec("du -m -s $pathDirectory");
  54.     }
  55.     /**
  56.      * @param PlatformAppEntity $entity
  57.      * @return bool
  58.      */
  59.     public function deleteDirectory(PlatformAppEntity $entity): bool
  60.     {
  61.         $pathDirectory $this->getDirectoryPath($entity->getDomain());
  62.         return $this->sftp->delete($pathDirectory);
  63.     }
  64.     /**
  65.      * @param string $name
  66.      *
  67.      * @return string
  68.      */
  69.     private function getDirectoryPath(string $name): string
  70.     {
  71.         return self::PATH $name;
  72.     }
  73. }