Controller.php 1.6 KB

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