<commit_before><commit_msg>Added thumbnail handler class which create and delete thumbnails with specified event listener<commit_after><?php
/*
* This file is part of the Lossendae\PreviouslyOn.
*
* (c) <NAME> <<EMAIL>>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Lossendae\PreviouslyOn\Services\Handlers;

use PHPThumb\GD as PHPThumb;
use Illuminate\Container\Container;

/**
 * Class ThumbnailHandler
 *
 * @package Lossendae\PreviouslyOn\Services\Handlers
 */
class ThumbnailHandler
{
    /**
     * @var
     */
    protected $config;
    /**
     * @var
     */
    protected $fileSystem;

    /**
     * @var \Illuminate\Container\Container
     */
    protected $app;

    /**
     * @param Container $app
     */
    function __construct(Container $app)
    {
        $this->app        = $app;
        $this->config     = $this->app['config'];
        $this->fileSystem = $this->app['files'];
    }

    /**
     * Cache the poster image locally on tv show creation
     *
     * @param object $show
     * @param array  $fromApi
     */
    public function handleCreatePoster($show, $fromApi)
    {
        $cacheDir  = $this->config->get('previously-on::app.assets_path') . '/images/cache/';
        $targetDir = $cacheDir . $show->id . '/';
        $origine   = $fromApi['tvshow'];

        if(!is_dir($cacheDir))
        {
            $this->fileSystem->makeDirectory($cacheDir);
        }

        if(!is_dir($targetDir))
        {
            $this->fileSystem->makeDirectory($targetDir);
        }

        $name      = 'poster';
        $extension = '.jpg';
        $target    = $targetDir . $name . $extension;

        $check = $this->checkRemoteImageSource($origine->getPosterUrl());

        if($check < 1)
        {
            /* Use a fallback poster image when there is no poster from the remote API server */
            copy($this->config->get('previously-on::app.assets_path') . '/images/fallback-poster.jpg', $targetThumb = $targetDir . $name . '-thumb' . $extension);
        }
        else
        {
            copy($origine->getPosterUrl(), $target);

            /* Resize the image */
            $targetThumb = $targetDir . $name . '-thumb' . $extension;
            $options     = array(
                'jpegQuality' => 90,
            );

            $thumb = new PHPThumb($target, $options);
            $thumb->resize(225, 330);
            $thumb->save($targetThumb);
        }
    }

    /**
     * Check the poster value to avoid error on poster cache
     *
     * @param $source
     * @return int
     */
    protected function checkRemoteImageSource($source)
    {
        return strlen(str_replace('http://www.thetvdb.com/banners/', '', $source));
    }

    /**
     * Delete the cache directory for the poster on tv show deletion
     *
     * @param int $id
     */
    public function handleDeletePoster($id)
    {
        $cacheDir = $this->config->get('previously-on::app.assets_path') . '/images/cache/' . $id;
        $this->fileSystem->deleteDirectory($cacheDir);
    }
}
{'fraction_non_alphanumeric': 0.14180537772087068, 'fraction_numerical': 0.002880921895006402, 'mean_word_length': 2.1001984126984126, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 30, 'https://': 0, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7476001', 'n_tokens_mistral': 930, 'n_tokens_neox': 910, 'n_words': 207}