XBackBone/app/Controllers/Controller.php

64 lines
1.3 KiB
PHP
Raw Normal View History

2018-04-28 12:20:07 +00:00
<?php
namespace App\Controllers;
2019-01-10 22:22:19 +00:00
use App\Database\DB;
use App\Web\Session;
use League\Flysystem\FileNotFoundException;
use League\Flysystem\Filesystem;
2019-01-10 22:22:19 +00:00
use Monolog\Logger;
2018-11-11 16:02:50 +00:00
use Slim\Container;
2018-04-28 12:20:07 +00:00
2019-01-10 22:22:19 +00:00
/**
* @property Session|null session
* @property mixed|null view
* @property DB|null database
* @property Logger|null logger
* @property Filesystem|null storage
2019-01-10 22:22:19 +00:00
*/
2018-04-28 12:20:07 +00:00
abstract class Controller
{
2018-11-11 16:02:50 +00:00
/** @var Container */
protected $container;
2018-11-11 16:02:50 +00:00
public function __construct(Container $container)
{
$this->container = $container;
2018-04-28 12:20:07 +00:00
}
/**
2018-11-11 16:02:50 +00:00
* @param $name
* @return mixed|null
* @throws \Interop\Container\Exception\ContainerException
2018-04-28 12:20:07 +00:00
*/
2018-11-11 16:02:50 +00:00
public function __get($name)
2018-04-28 12:20:07 +00:00
{
2018-11-11 16:02:50 +00:00
if ($this->container->has($name)) {
return $this->container->get($name);
2018-04-28 12:20:07 +00:00
}
2018-11-11 16:02:50 +00:00
return null;
2018-04-28 12:20:07 +00:00
}
/**
* @param $id
* @return int
*/
protected function getUsedSpaceByUser($id): int
{
$medias = $this->database->query('SELECT `uploads`.`storage_path` FROM `uploads` WHERE `user_id` = ?', $id)->fetchAll();
$totalSize = 0;
$filesystem = $this->storage;
foreach ($medias as $media) {
try {
$totalSize += $filesystem->getSize($media->storage_path);
} catch (FileNotFoundException $e) {
2019-05-12 13:52:26 +00:00
$this->logger->error('Error calculating file size', ['exception' => $e]);
}
}
return $totalSize;
}
2018-04-28 12:20:07 +00:00
}