<commit_before><?php
/**
 * Neap (http://neap.io/)
 *
 * @link      http://github.com/e7d/neap for the canonical source repository
 * @copyright Copyright (c) 2016 Michaël "e7d" Ferrand (http://github.com/e7d)
 * @license   https://github.com/e7d/neap/blob/master/LICENSE.txt The MIT License
 */

namespace RTMP\V1\Rpc\Event;

use Application\Authorization\LocalhostController;
use ZF\ApiProblem\ApiProblemResponse;
use ZF\ApiProblem\ApiProblem;
use ZF\ContentNegotiation\ViewModel;

class EventController extends LocalhostController
{
    private $channelModel;
    private $ingestModel;
    private $streamModel;

    public function __construct($channelModel, $ingestModel, $streamModel)
    {
        $this->channelModel = $channelModel;
        $this->ingestModel = $ingestModel;
        $this->streamModel = $streamModel;
    }

    public function eventAction()
    {
        try {
            $this->assertLocalConnection();

            switch ($_POST['app']) {
                case 'transcode':
                    $streamKey = $_POST['name'];
                    $hostname = parse_url($_POST['tcurl'], PHP_URL_HOST);

                    switch ($_POST['call']) {
                        case 'publish':
                            $channel = $this->channelModel->fetchByStreamKey($streamKey);

                            if (is_null($channel)) {
                                return new ApiProblemResponse(
                                    new ApiProblem(403, 'This stream key is invalid')
                                );
                            }

                            $ingest = $this->ingestModel->fetchByHostname($hostname);

                            $this->streamModel->create(array(
                                'channel_id' => $channel->channel_id,
                                'ingest_id' => $ingest->ingest_id,
                                'title' => $channel->title,
                                'topic_id' => $channel->topic_id,
                                'topic' => $channel->topic,
                            ));

                            break;
                        case 'publish_done':
                            $channel = $this->channelModel->fetchByStreamKey($streamKey);

                            if (is_null($channel)) {
                                return new ApiProblemResponse(
                                    new ApiProblem(403, 'This stream key is invalid')
                                );
                            }

                            $stream = $this->streamModel->fetchByChannel($channel->channel_id, $live = true);

                            $now = \DateTime::createFromFormat('U.u', microtime(true));
                            $this->streamModel->update(
                                $stream->stream_id,
                                array(
                                    'ended_at' => $now->format('Y-m-d H:i:s.uO')
                                )
                            );

                            break;
                    }
            }

            return new ViewModel(array());
        } catch (\DomainException $e) {
            return new ApiProblemResponse(
                new ApiProblem(403, 'This route must be invoked from the local host')
            );
        } catch (\Exception $e) {
            return new ApiProblemResponse(
                new ApiProblem(500, 'A technical error occured')
            );
        }
    }
}<commit_msg>improved controller error message readability<commit_after><?php
/**
 * Neap (http://neap.io/)
 *
 * @link      http://github.com/e7d/neap for the canonical source repository
 * @copyright Copyright (c) 2016 Michaël "e7d" Ferrand (http://github.com/e7d)
 * @license   https://github.com/e7d/neap/blob/master/LICENSE.txt The MIT License
 */

namespace RTMP\V1\Rpc\Event;

use Application\Authorization\LocalhostController;
use ZF\ApiProblem\ApiProblemResponse;
use ZF\ApiProblem\ApiProblem;
use ZF\ContentNegotiation\ViewModel;

class EventController extends LocalhostController
{
    private $channelModel;
    private $ingestModel;
    private $streamModel;

    public function __construct($channelModel, $ingestModel, $streamModel)
    {
        $this->channelModel = $channelModel;
        $this->ingestModel = $ingestModel;
        $this->streamModel = $streamModel;
    }

    public function eventAction()
    {
        try {
            $this->assertLocalConnection();

            switch ($_POST['app']) {
                case 'transcode':
                    $streamKey = $_POST['name'];
                    $hostname = parse_url($_POST['tcurl'], PHP_URL_HOST);

                    switch ($_POST['call']) {
                        case 'publish':
                            $channel = $this->channelModel->fetchByStreamKey($streamKey);

                            if (is_null($channel)) {
                                return new ApiProblemResponse(
                                    new ApiProblem(403, 'This stream key is invalid')
                                );
                            }

                            $ingest = $this->ingestModel->fetchByHostname($hostname);

                            $this->streamModel->create(array(
                                'channel_id' => $channel->channel_id,
                                'ingest_id' => $ingest->ingest_id,
                                'title' => $channel->title,
                                'topic_id' => $channel->topic_id,
                                'topic' => $channel->topic,
                            ));

                            break;
                        case 'publish_done':
                            $channel = $this->channelModel->fetchByStreamKey($streamKey);

                            if (is_null($channel)) {
                                return new ApiProblemResponse(
                                    new ApiProblem(403, 'This stream key is invalid')
                                );
                            }

                            $stream = $this->streamModel->fetchByChannel($channel->channel_id, $live = true);

                            $now = \DateTime::createFromFormat('U.u', microtime(true));
                            $this->streamModel->update(
                                $stream->stream_id,
                                array(
                                    'ended_at' => $now->format('Y-m-d H:i:s.uO')
                                )
                            );

                            break;
                    }
            }

            return new ViewModel(array());
        } catch (\DomainException $e) {
            return new ApiProblemResponse(
                new ApiProblem(403, 'This route can only be invoked from the local host')
            );
        } catch (\Exception $e) {
            return new ApiProblemResponse(
                new ApiProblem(500, 'A technical error occured')
            );
        }
    }
}
{'fraction_non_alphanumeric': 0.10500853727945361, 'fraction_numerical': 0.00597609561752988, 'mean_word_length': 1.215952080706179, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 63, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1886526', 'n_tokens_mistral': 1779, 'n_tokens_neox': 1676, 'n_words': 329}