<commit_before><?php
namespace Mopa\BarcodeBundle\Model;


use Monolog\Logger;

use Imagine\Gd\Image;

use Imagine\Image\ImagineInterface;
use Zend\Barcode\Barcode;

class BarcodeService{
    private $types;
    private $imagine;
    private $kernelcachedir;
    private $kernelrootdir;
    private $webdir;
    private $logger;
    
    public function __construct(ImagineInterface $imagine, $kernelcachedir, $kernelrootdir, $webdir,  Logger $logger){
        $this->types = BarcodeTypes::getTypes();
        $this->imagine = $imagine;
        $this->kernelcachedir = $kernelcachedir;
        $this->kernelrootdir = $kernelrootdir;
        $this->webdir = $webdir;
        $this->logger = $logger;
    }
    public function saveAs($type, $text, $file){
        @unlink($file);
        switch ($type){
            case $type == 'qr':
                include __DIR__."/../Resources/phpqrcode/qrlib.php";
                \QRcode::png($text, $file);
            break;
            case is_numeric($type):
                $type = $this->types[$type];
            default:
                $barcodeOptions = array('text' => $text);
                $rendererOptions = array();
                $image = new Image(
                    $imageResource = Barcode::factory(
                        $type, 'image', $barcodeOptions, $rendererOptions, false
                    )->draw()
                );
                $image->save($file);
        }
        return true;
    }
    /**
     * Get a Barcodes Filename
     * Generates it if its not here
     * 
     * @param string $type BarcodeType
     * @param string $text BarcodeText
     * @param boolean $absolute get absolute path, default: false
     * @param array $options Options
     */
    public function get($type, $enctext, $absolut = false, $options = array()){
        $text = urldecode($enctext);
        $filename = $this->getAbsoluteBarcodeDir($type).$this->getBarcodeFilename($text);
        if(!file_exists($filename)){
            $this->saveAs($type, $text, $filename);
        }
        if(!$absolut){
            return "/".$this->webdir.$this->getTypeDir($type).$this->getBarcodeFilename($text);
        }
        return $filename;
    }
    protected function getTypeDir($type){
        if(is_numeric($type)){
            $type = $this->types[$type];
        }
        return $type.DIRECTORY_SEPARATOR;
    }<commit_msg>Remove first slash from relative path - more compatible with asset helper<commit_after><?php
namespace Mopa\BarcodeBundle\Model;


use Monolog\Logger;

use Imagine\Gd\Image;

use Imagine\Image\ImagineInterface;
use Zend\Barcode\Barcode;

class BarcodeService{
    private $types;
    private $imagine;
    private $kernelcachedir;
    private $kernelrootdir;
    private $webdir;
    private $logger;
    
    public function __construct(ImagineInterface $imagine, $kernelcachedir, $kernelrootdir, $webdir,  Logger $logger){
        $this->types = BarcodeTypes::getTypes();
        $this->imagine = $imagine;
        $this->kernelcachedir = $kernelcachedir;
        $this->kernelrootdir = $kernelrootdir;
        $this->webdir = $webdir;
        $this->logger = $logger;
    }
    public function saveAs($type, $text, $file){
        @unlink($file);
        switch ($type){
            case $type == 'qr':
                include __DIR__."/../Resources/phpqrcode/qrlib.php";
                \QRcode::png($text, $file);
            break;
            case is_numeric($type):
                $type = $this->types[$type];
            default:
                $barcodeOptions = array('text' => $text);
                $rendererOptions = array();
                $image = new Image(
                    $imageResource = Barcode::factory(
                        $type, 'image', $barcodeOptions, $rendererOptions, false
                    )->draw()
                );
                $image->save($file);
        }
        return true;
    }
    /**
     * Get a Barcodes Filename
     * Generates it if its not here
     * 
     * @param string $type BarcodeType
     * @param string $text BarcodeText
     * @param boolean $absolute get absolute path, default: false
     * @param array $options Options
     */
    public function get($type, $enctext, $absolut = false, $options = array()){
        $text = urldecode($enctext);
        $filename = $this->getAbsoluteBarcodeDir($type).$this->getBarcodeFilename($text);
        if(!file_exists($filename)){
            $this->saveAs($type, $text, $filename);
        }
        if(!$absolut){
            return $this->webdir.$this->getTypeDir($type).$this->getBarcodeFilename($text);
        }
        return $filename;
    }
    protected function getTypeDir($type){
        if(is_numeric($type)){
            $type = $this->types[$type];
        }
        return $type.DIRECTORY_SEPARATOR;
    }
{'fraction_non_alphanumeric': 0.1331262939958592, 'fraction_numerical': 0.0, 'mean_word_length': 2.114764667956157, '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': '4003848', 'n_tokens_mistral': 1405, 'n_tokens_neox': 1390, 'n_words': 300}