Controller.php 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <?php
  2. namespace App\Controllers;
  3. use App\Database\DB;
  4. use App\Web\Lang;
  5. use App\Web\Session;
  6. use League\Flysystem\FileNotFoundException;
  7. use League\Flysystem\Filesystem;
  8. use Monolog\Logger;
  9. use Slim\Container;
  10. /**
  11. * @property Session|null session
  12. * @property mixed|null view
  13. * @property DB|null database
  14. * @property Logger|null logger
  15. * @property Filesystem|null storage
  16. * @property Lang lang
  17. * @property array settings
  18. */
  19. abstract class Controller
  20. {
  21. /** @var Container */
  22. protected $container;
  23. public function __construct(Container $container)
  24. {
  25. $this->container = $container;
  26. }
  27. /**
  28. * @param $name
  29. * @return mixed|null
  30. * @throws \Interop\Container\Exception\ContainerException
  31. */
  32. public function __get($name)
  33. {
  34. if ($this->container->has($name)) {
  35. return $this->container->get($name);
  36. }
  37. return null;
  38. }
  39. /**
  40. * @param $id
  41. * @return int
  42. */
  43. protected function getUsedSpaceByUser($id): int
  44. {
  45. $medias = $this->database->query('SELECT `uploads`.`storage_path` FROM `uploads` WHERE `user_id` = ?', $id);
  46. $totalSize = 0;
  47. $filesystem = $this->storage;
  48. foreach ($medias as $media) {
  49. try {
  50. $totalSize += $filesystem->getSize($media->storage_path);
  51. } catch (FileNotFoundException $e) {
  52. $this->logger->error('Error calculating file size', ['exception' => $e]);
  53. }
  54. }
  55. return $totalSize;
  56. }
  57. }