<commit_before><?php

namespace MBTilesGenerator;

use MBTilesGenerator\Core\MBTileFile;
use MBTilesGenerator\Exception\TileNotAvailableException;
use MBTilesGenerator\TileSources\TileSourceInterface;
use MBTilesGenerator\Model\BoundingBox;
use MBTilesGenerator\Util\Calculator;
use MBTilesGenerator\Model\Tile;

class MBTilesGenerator
{
    /**
     * @var TileSources\TileSourceInterface
     */
    protected $tileSource;

    protected $tileLimit = 2000;

    protected $maxZoom = 18;

    protected $effectiveZoom = 0;

    /**
     * How many percentage of the tiles are allowed to fail,
     * while still considering generation a success.
     *
     * @var int
     */
    protected $allowedFail = 5;

    /**
     * @param TileSourceInterface $tileSource
     */
    public function __construct(TileSourceInterface $tileSource)
    {
        $this->tileSource = $tileSource;
    }

    /**
     * Prepares the tiles by getting the tileSource to generate or download them.
     *
     * @param BoundingBox $boundingBox
     * @param string $destination File destination to write the mbtiles file to
     *
     * @throws
     * @return bool
     */
    public function generate(BoundingBox $boundingBox, $destination)
    {
        if (file_exists($destination)) {
            throw new \Exception('Destination file already exists');
        }
        $tiles = $this->generateTileList($boundingBox);

        $this->tileSource->cache($tiles);

        // Start constructing our file
        $mbtiles = new MBTileFile($destination);

        // Set the required meta data
        $this->addMetaDataToDB($mbtiles, $boundingBox);

        // Add tiles to the database
        $this->addTilesToDB($mbtiles, $tiles);

    }

    /**
     * @param MBTileFile $mbtiles
     * @param Tile[] $tiles
     */
    protected function addTilesToDB(MBTileFile $mbtiles, $tiles)
    {
        $failed_tiles = 0;

        foreach ($tiles as $tile) {
            try {
                $mbtiles->addTile($tile->z, $tile->x, $tile->y, $this->tileSource->getTile($tile));
            } catch (TileNotAvailableException $e) {
                $failed_tiles++;
                continue;
            }
        }
<commit_msg>Added more documentation on properties, and a few methods.<commit_after><?php

namespace MBTilesGenerator;

use MBTilesGenerator\Core\MBTileFile;
use MBTilesGenerator\Exception\TileNotAvailableException;
use MBTilesGenerator\TileSources\TileSourceInterface;
use MBTilesGenerator\Model\BoundingBox;
use MBTilesGenerator\Util\Calculator;
use MBTilesGenerator\Model\Tile;

class MBTilesGenerator
{
    /**
     * @var TileSources\TileSourceInterface
     */
    protected $tileSource;

    /**
     * The maximum number of tiles we want in the file
     * if the bounding box and zoom needs more than this
     * the zoom will be stepped down, until we are within this limit.
     * @var int
     */
    protected $tileLimit = 2000;

    /**
     * The zoom we are aiming for
     * @var int
     */
    protected $maxZoom = 18;

    /**
     * The last actual zoom accomplished
     * @var int
     */
    protected $effectiveZoom = -1;

    /**
     * How many percentage of the tiles are allowed to fail,
     * while still considering generation a success.
     *
     * @var int
     */
    protected $allowedFail = 5;

    /**
     * @param TileSourceInterface $tileSource
     */
    public function __construct(TileSourceInterface $tileSource)
    {
        $this->tileSource = $tileSource;
    }

    /**
     * Prepares the tiles by getting the tileSource to generate or download them.
     *
     * @param BoundingBox $boundingBox
     * @param string $destination File destination to write the mbtiles file to
     *
     * @throws
     * @return bool
     */
    public function generate(BoundingBox $boundingBox, $destination)
    {
        if (file_exists($destination)) {
            throw new \Exception('Destination file already exists');
        }
        $tiles = $this->generateTileList($boundingBox);

        $this->tileSource->cache($tiles);

        // Start constructing our file
        $mbtiles = new MBTileFile($destination);

        // Set the required meta data
        $this->addMetaDataToDB($mbtiles, $boundingBox);

        // Add tiles to the database
        $this->addTilesToDB($mbtiles, $tiles);

    }

    /**
     * Set maximum zoom on this instance, defaults to 18.
     * @param int $zoom
     */
    public function setMaxZoom($zoom) {
        $this->maxZoom = $zoom;
    }

    /**
     * Sets the allowed failures
     * @param $allowedFail
     */
    public function setAllowedFail($allowedFail) {
        $this->allowedFail = $allowedFail;
    }

    /**
     * Returns the effective zoom we accomplished on last run.
     * @return int
     */
    public function getEffectiveZoom() {
        return $this->effectiveZoom;
    }

    /**
     * @param MBTileFile $mbtiles
     * @param Tile[] $tiles
     */
    protected function addTilesToDB(MBTileFile $mbtiles, $tiles)
    {
        $failed_tiles = 0;

        foreach ($tiles as $tile) {
            try {
                $mbtiles->addTile($tile->z, $tile->x, $tile->y, $this->tileSource->getTile($tile));
            } catch (TileNotAvailableException $e) {
                $failed_tiles++;
                continue;
            }
        }

{'fraction_non_alphanumeric': 0.1031390134529148, 'fraction_numerical': 0.0037369207772795215, 'mean_word_length': 2.310451453308596, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 30, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3508048', 'n_tokens_mistral': 1573, 'n_tokens_neox': 1515, 'n_words': 407}