Skip to content

Instantly share code, notes, and snippets.

@madmis
Last active October 26, 2015 17:01
Show Gist options
  • Save madmis/6a30f1c7e8d6c23fa119 to your computer and use it in GitHub Desktop.
Save madmis/6a30f1c7e8d6c23fa119 to your computer and use it in GitHub Desktop.
<?php
namespace library\ExternalConfig\Provider;
use library\ExternalConfig\Entity\Size\Byte;
/**
* Class PHPProvider
* @package library\ExternalConfig\Provider
*/
class PHPProvider implements ProviderInterface, FileProviderInterface
{
/**
* @var Byte
*/
private $maxFileSize;
/**
* Get max file size in bytes
* @see Symfony\Component\HttpFoundation\File\UploadedFile::getMaxFilesize
* @return Byte
*/
public function getMaxFileSize()
{
if (!$this->maxFileSize) {
$iniMax = strtolower(ini_get('upload_max_filesize'));
if ('' === $iniMax) {
$this->maxFileSize = new Byte(PHP_INT_MAX);
} else {
$max = ltrim($iniMax, '+');
if (0 === strpos($max, '0x')) {
$max = intval($max, 16);
} elseif (0 === strpos($max, '0')) {
$max = intval($max, 8);
} else {
$max = (int)$max;
}
switch (substr($iniMax, -1)) {
case 't':
$max *= 1024;
case 'g':
$max *= 1024;
case 'm':
$max *= 1024;
case 'k':
$max *= 1024;
}
$this->maxFileSize = new Byte($max);
}
}
return $this->maxFileSize;
}
}
<?php
namespace tests\unit\hub\library\ExternalConfig\Provider;
use Codeception\TestCase\Test;
use library\ExternalConfig\Provider\PHPProvider;
/**
* Class PHPProviderTest
* @package tests\unit\hub\library\ExternalConfig\Provider
*/
class PHPProviderTest extends Test
{
public function testGetMaxFileSize()
{
$provider = new PHPProvider();
$this->assertInstanceOf(
'library\ExternalConfig\Provider\FileProviderInterface',
$provider
);
$this->assertInstanceOf(
'library\ExternalConfig\Entity\Size\Byte',
$provider->getMaxFileSize()
);
$bytes = $provider->getMaxFileSize();
$iniMax = strtolower(ini_get('upload_max_filesize'));
switch (substr($iniMax, -1)) {
case 't':
$bytes = $bytes->toTerabyte();
break;
case 'g':
$bytes = $bytes->toGigabyte();
break;
case 'm':
$bytes = $bytes->toMegabyte();
break;
case 'k':
$bytes = $bytes->toKilobyte();
break;
}
$this->assertEquals((int)$iniMax, $bytes->getValue());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment