<commit_before><?php

/**
 * Copyright (c) dotBlue (http://dotblue.net)
 */

namespace DotBlue\WebImages;

use Nette\Application;


class Route extends Application\Routers\Route
{

	/** @var array */
	private $defaults = [
		'algorithm' => IProvider::FIT,
	];



	/**
	 * @param  string
	 * @param  array
	 * @param  Validator
	 */
	public function __construct($mask, array $defaults, Generator $generator)
	{
		$this->defaults = array_replace($this->defaults, $defaults);

		$defaults[NULL][self::FILTER_OUT] = function ($params) use ($defaults, $generator) {
			$width = $this->acquireArgument('width', $params);
			$height = $this->acquireArgument('height', $params);
			$algorithm = $this->acquireArgument('algorithm', $params);

			if (!$generator->getValidator()->validate($width, $height, $algorithm)) {
				throw new NotAllowedImageException("Image with params ({$width}x{$height}, {$algorithm}) is not allowed - check your 'webimages.rules' please.");
			}

			if (isset($defaults[NULL][self::FILTER_OUT])) {
				$params = call_user_func($defaults[NULL][self::FILTER_OUT], $params);
			}

			return $params;
		};

		$defaults['presenter'] = 'Nette:Micro';
		$defaults['callback'] = function ($presenter) use ($generator) {
			$params = $presenter->getRequest()->getParameters();

			$id = $params['id'];

			$width = $this->acquireArgument('width', $params);
			$height = $this->acquireArgument('height', $params);
			$algorithm = $this->acquireArgument('algorithm', $params);

			$generator->generateImage($id, $width, $height, $algorithm);
		};

		parent::__construct($mask, $defaults);
	}



	private function acquireArgument($name, array $data)
	{
		if (isset($data[$name])) {
			return $data[$name];
		} elseif (isset($this->defaults[$name])) {
			return $this->defaults[$name];
		} else {
			throw new \Exception;
		}
	}

}

class NotAllowedImageException extends \Exception {}<commit_msg>Route passes itself as callback<commit_after><?php

/**
 * Copyright (c) dotBlue (http://dotblue.net)
 */

namespace DotBlue\WebImages;

use Nette\Application;


class Route extends Application\Routers\Route
{

	/** @var array */
	private $defaults = [
		'algorithm' => IProvider::FIT,
	];

	/** @var Generator */
	private $generator;



	/**
	 * @param  string
	 * @param  array
	 * @param  Validator
	 */
	public function __construct($mask, array $defaults, Generator $generator)
	{
		$this->defaults = array_replace($this->defaults, $defaults);
		$this->generator = $generator;

		$defaults[NULL][self::FILTER_OUT] = function ($params) use ($defaults, $generator) {
			$width = $this->acquireArgument('width', $params);
			$height = $this->acquireArgument('height', $params);
			$algorithm = $this->acquireArgument('algorithm', $params);

			if (!$generator->getValidator()->validate($width, $height, $algorithm)) {
				throw new NotAllowedImageException("Image with params ({$width}x{$height}, {$algorithm}) is not allowed - check your 'webimages.rules' please.");
			}

			if (isset($defaults[NULL][self::FILTER_OUT])) {
				$params = call_user_func($defaults[NULL][self::FILTER_OUT], $params);
			}

			return $params;
		};

		$defaults['presenter'] = 'Nette:Micro';
		$defaults['callback'] = $this;

		parent::__construct($mask, $defaults);
	}



	private function acquireArgument($name, array $data)
	{
		if (isset($data[$name])) {
			return $data[$name];
		} elseif (isset($this->defaults[$name])) {
			return $this->defaults[$name];
		} else {
			throw new \Exception;
		}
	}



	public function __invoke($presenter)
	{
		$params = $presenter->getRequest()->getParameters();

		$id = $params['id'];

		$width = $this->acquireArgument('width', $params);
		$height = $this->acquireArgument('height', $params);
		$algorithm = $this->acquireArgument('algorithm', $params);

		$this->generator->generateImage($id, $width, $height, $algorithm);
	}

}

class NotAllowedImageException extends \Exception {}
{'fraction_non_alphanumeric': 0.18353783231083845, 'fraction_numerical': 0.0, 'mean_word_length': 4.712408759124088, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '3983800', 'n_tokens_mistral': 1345, 'n_tokens_neox': 1204, 'n_words': 254}