Controller.php 1.2 KB

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