Skip to content

Commit

Permalink
Clean psr2 (#55)
Browse files Browse the repository at this point in the history
* Cleanup PSR2

* Tell if we failed

Fix some missed things

* Clean up tester, fix test coverage

* Fix last tests I hope
  • Loading branch information
whikloj authored and dannylamb committed Dec 13, 2018
1 parent b920087 commit 2a16ceb
Show file tree
Hide file tree
Showing 10 changed files with 145 additions and 133 deletions.
13 changes: 13 additions & 0 deletions .scripts/tester
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/bin/sh

FAIL=0
for D in */; do
cd $D
echo "Operating on $D"
composer test
if [ $? -ne 0 ]; then
FAIL=1
fi
cd ..
done
exit $FAIL
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ install:

script:
- $SCRIPT_DIR/line_endings.sh $TRAVIS_BUILD_DIR
- for D in */; do (cd $D; composer test) done
- .scripts/tester

notifications:
irc:
Expand Down
2 changes: 1 addition & 1 deletion Gemini/phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<log type="coverage-clover" target="clover.xml"/>
</logging>
<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<whitelist processUncoveredFilesFromWhitelist="false">
<exclude>
<file>./src/index.php</file>
<file>./src/app.php</file>
Expand Down
37 changes: 17 additions & 20 deletions Gemini/src/Migrations/Version20180530031926.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,22 @@ final class Version20180530031926 extends AbstractMigration
{
public function up(Schema $schema)
{
$this->addSql(
'DROP TABLE IF EXISTS Gemini;'
);

if ('mysql' == $this->connection->getDatabasePlatform()->getName()) {
$this->addSql(
'CREATE TABLE Gemini (fedora_hash VARCHAR(128) NOT NULL,
'DROP TABLE IF EXISTS Gemini;'
);

if ('mysql' == $this->connection->getDatabasePlatform()->getName()) {
$this->addSql(
'CREATE TABLE Gemini (fedora_hash VARCHAR(128) NOT NULL,
drupal_hash VARCHAR(128) NOT NULL, uuid VARCHAR(36) NOT NULL,
drupal_uri LONGTEXT NOT NULL, fedora_uri LONGTEXT NOT NULL,
dateCreated DATETIME NOT NULL, dateUpdated DATETIME NOT NULL,
UNIQUE KEY(fedora_hash, drupal_hash), PRIMARY KEY(uuid))
DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci ENGINE = InnoDB'
);
}
elseif
('postgresql' == $this->connection->getDatabasePlatform()->getName()) {
$this->addSql(
'CREATE TABLE Gemini (
);
} elseif ('postgresql' == $this->connection->getDatabasePlatform()->getName()) {
$this->addSql(
'CREATE TABLE Gemini (
fedora_hash VARCHAR(128) NOT NULL,
drupal_hash VARCHAR(128) NOT NULL,
uuid VARCHAR(36) PRIMARY KEY,
Expand All @@ -38,14 +36,13 @@ public function up(Schema $schema)
dateCreated TIMESTAMP NOT NULL,
dateUpdated TIMESTAMP NOT NULL
);'
);
$this->addSql(
'CREATE UNIQUE INDEX fedora_drupal_hash ON Gemini (fedora_hash, drupal_hash);'
);
}
else {
$this->abortIf(TRUE, "Only MySQL/MariaDB and PostgreSQL are supported.");
}
);
$this->addSql(
'CREATE UNIQUE INDEX fedora_drupal_hash ON Gemini (fedora_hash, drupal_hash);'
);
} else {
$this->abortIf(true, "Only MySQL/MariaDB and PostgreSQL are supported.");
}
}

public function down(Schema $schema)
Expand Down
2 changes: 1 addition & 1 deletion Gemini/tests/Islandora/Gemini/Tests/UrlMapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public function testSaveUrlsReturnsFalseOnUpdate()
$connection->beginTransaction()->shouldBeCalled();
$connection->insert(Argument::any(), Argument::any())
->willThrow($exception);
$connection->executeUpdate(Argument::any(), Argument::any())
$connection->update(Argument::any(), Argument::any(), Argument::any())
->willReturn(1);
$connection->commit()->shouldBeCalled();
$connection->rollBack()->shouldNotBeCalled();
Expand Down
188 changes: 96 additions & 92 deletions Homarus/src/Controller/HomarusController.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,19 @@
* @package Islandora\Homarus\Controller
* @param $log
*/
class HomarusController {
class HomarusController
{

/**
* @var \Islandora\Crayfish\Commons\CmdExecuteService
*/
protected $cmd;
protected $cmd;


/**
* @var \Monolog\Logger
*/
protected $log;
protected $log;

/**
* Controller constructor.
Expand All @@ -36,109 +37,112 @@ class HomarusController {
* @param string $executable
* @param $log
*/
public function __construct(
CmdExecuteService $cmd,
$formats,
$default_format,
$executable,
$log,
$mime_to_format
) {
$this->cmd = $cmd;
$this->formats = $formats;
$this->default_format = $default_format;
$this->executable = $executable;
$this->log = $log;
$this->mime_to_format = $mime_to_format;
}
public function __construct(
CmdExecuteService $cmd,
$formats,
$default_format,
$executable,
$log,
$mime_to_format
) {
$this->cmd = $cmd;
$this->formats = $formats;
$this->default_format = $default_format;
$this->executable = $executable;
$this->log = $log;
$this->mime_to_format = $mime_to_format;
}

/**
* @param \Symfony\Component\HttpFoundation\Request $request
* @return \Symfony\Component\HttpFoundation\Response|\Symfony\Component\HttpFoundation\StreamedResponse
*/
public function convert(Request $request) {
$this->log->info('Ffmpeg Convert request.');

// Short circuit if there's no Apix-Ldp-Resource header.
if (!$request->headers->has("Apix-Ldp-Resource"))
public function convert(Request $request)
{
$this->log->debug("Malformed request, no Apix-Ldp-Resource header present");
return new Response(
"Malformed request, no Apix-Ldp-Resource header present",
400
);
} else {
$source = $request->headers->get('Apix-Ldp-Resource');
}

// Find the format
$content_types = $request->getAcceptableContentTypes();
$content_type = $this->get_content_type($content_types);
$format = $this->get_ffmpeg_format($content_type);

$cmd_params = "";
if($format == "mp4") {
$cmd_params = " -vcodec libx264 -preset medium -acodec aac -strict -2 -ab 128k -ac 2 -async 1 -movflags frag_keyframe+empty_moov ";
}

// Arguments to ffmpeg command are sent as a custom header
$args = $request->headers->get('X-Islandora-Args');
$this->log->debug("X-Islandora-Args:", ['args' => $args]);

$cmd_string = "$this->executable -i $source $cmd_params -f $format -";
$this->log->info('Ffempg Command:', ['cmd' => $cmd_string]);

// Return response.
try {
return new StreamedResponse(
$this->cmd->execute($cmd_string, $source),
200,
['Content-Type' => $content_type]
);
} catch (\RuntimeException $e) {
$this->log->error("RuntimeException:", ['exception' => $e]);
return new Response($e->getMessage(), 500);
$this->log->info('Ffmpeg Convert request.');

// Short circuit if there's no Apix-Ldp-Resource header.
if (!$request->headers->has("Apix-Ldp-Resource")) {
$this->log->debug("Malformed request, no Apix-Ldp-Resource header present");
return new Response(
"Malformed request, no Apix-Ldp-Resource header present",
400
);
} else {
$source = $request->headers->get('Apix-Ldp-Resource');
}

// Find the format
$content_types = $request->getAcceptableContentTypes();
$content_type = $this->getContentType($content_types);
$format = $this->getFfmpegFormat($content_type);

$cmd_params = "";
if ($format == "mp4") {
$cmd_params = " -vcodec libx264 -preset medium -acodec aac " .
"-strict -2 -ab 128k -ac 2 -async 1 -movflags " .
"frag_keyframe+empty_moov ";
}

// Arguments to ffmpeg command are sent as a custom header
$args = $request->headers->get('X-Islandora-Args');
$this->log->debug("X-Islandora-Args:", ['args' => $args]);

$cmd_string = "$this->executable -i $source $cmd_params -f $format -";
$this->log->info('Ffempg Command:', ['cmd' => $cmd_string]);

// Return response.
try {
return new StreamedResponse(
$this->cmd->execute($cmd_string, $source),
200,
['Content-Type' => $content_type]
);
} catch (\RuntimeException $e) {
$this->log->error("RuntimeException:", ['exception' => $e]);
return new Response($e->getMessage(), 500);
}
}
}


private function get_content_type($content_types) {
$content_type = null;
foreach ($content_types as $type) {
if (in_array($type, $this->formats)) {
$content_type = $type;
break;
}
private function getContentType($content_types)
{
$content_type = null;
foreach ($content_types as $type) {
if (in_array($type, $this->formats)) {
$content_type = $type;
break;
}
}

if ($content_type === null) {
$content_type = $this->default_format;
$this->log->info('Falling back to default content type');
}
return $content_type;
}

if ($content_type === null) {
$content_type = $this->default_format;
$this->log->info('Falling back to default content type');
}
return $content_type;
}

private function get_ffmpeg_format($content_type){
foreach ($this->mime_to_format as $format) {
if (strpos($format, $content_type) !== false) {
$this->log->info("does it get here");
$format_info = explode("_", $format);
break;
}
private function getFfmpegFormat($content_type)
{
foreach ($this->mime_to_format as $format) {
if (strpos($format, $content_type) !== false) {
$this->log->info("does it get here");
$format_info = explode("_", $format);
break;
}
}
return $format_info[1];
}
return $format_info[1];
}

/**
* @return \Symfony\Component\HttpFoundation\BinaryFileResponse
*/
public function convertOptions()
{
return new BinaryFileResponse(
__DIR__ . "/../../static/convert.ttl",
200,
['Content-Type' => 'text/turtle']
);
}

public function convertOptions()
{
return new BinaryFileResponse(
__DIR__ . "/../../static/convert.ttl",
200,
['Content-Type' => 'text/turtle']
);
}
}
17 changes: 8 additions & 9 deletions Homarus/src/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,20 @@
use Silex\Application;
use Symfony\Component\HttpFoundation\Request;


$app = new Application();

$app->register(new IslandoraServiceProvider());
$app->register(new YamlConfigServiceProvider(__DIR__ . '/../cfg/config.yaml'));

$app['homarus.controller'] = function ($app) {
return new HomarusController(
$app['crayfish.cmd_execute_service'],
$app['crayfish.homarus.mime_types.valid'],
$app['crayfish.homarus.mime_types.default_video'],
$app['crayfish.homarus.executable'],
$app['monolog'],
$app['crayfish.homarus.mime_to_format']
);
return new HomarusController(
$app['crayfish.cmd_execute_service'],
$app['crayfish.homarus.mime_types.valid'],
$app['crayfish.homarus.mime_types.default_video'],
$app['crayfish.homarus.executable'],
$app['monolog'],
$app['crayfish.homarus.mime_to_format']
);
};

$app->options('/convert', "homarus.controller:convertOptions");
Expand Down
10 changes: 5 additions & 5 deletions Homarus/tests/Islandora/Homarus/Tests/HomarusControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@
use Monolog\Logger;

/**
* @coversDefaultClass \Islandora\Houdini\Controller\HomarusControllerTest
* @coversDefaultClass \Islandora\Homarus\Controller\HomarusController
*/
class HomarusControllerTest extends \PHPUnit_Framework_TestCase
{

/**
* @covers ::identifyOptions
* @covers ::convertOptions
* @covers ::convert
* @covers ::getContentType
* @covers ::getFfmpegFormat
*/
public function testOptions()
{
Expand All @@ -30,7 +31,7 @@ public function testOptions()
'',
'convert',
$this->prophesize(Logger::class)->reveal(),
''
''
);

$response = $controller->convertOptions();
Expand All @@ -40,5 +41,4 @@ public function testOptions()
'Convert OPTIONS should return turtle'
);
}

}
2 changes: 1 addition & 1 deletion Milliner/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
],
"test": [
"@check",
"phpunit"
"phpunit"
]
},
"require-dev": {
Expand Down
Loading

0 comments on commit 2a16ceb

Please sign in to comment.