<commit_before><?php namespace DMA\Recommendations\Classes;

use DMA\Recommendations\Models\Settings;

/**
 * DMA recomendation manager 
 * @author <NAME>
 */
class RecommendationManager
{
    
    /**
     * Dictionary of Recommendation Items
     * @var array
     */
    private $items = [];

    /**
     * Dictionary of Recommendation Backends
     * @var array
     */
    private $backends = [];
    
    /**
     * Flag to keep track if Item Events are 
     * already bind to the engine
     * @var boolean
     */
    private $bindEvents = false;
    
    /**
     * Active recomendation backend engine
     * @var DMA\Recommendations\Classes\Backends\BackendBase
     */
    private $engine = null;
    
    /**
     * Register recomentation items
     * @param array $items classnames of Recommendation Items to be register
     */
    public function registerItems(array $items)
    {
    	foreach($items as $class => $details){
    
    		$it = \App::make($class);
    
    		// Fill detail information used by the Admin interface
    	    $it->info = $details;
            // Is this item active
    	    $it->active = Settings::get(strtolower($it->getKey()) .'_active', false);  	    
    	    
    	    $this->items[$it->getKey()] = $it;
    	
    	}
    	
    }

    
    /**
     * Return an array of all register Recommendation Items
     * @return array
     */
    public function getRegisterItems()
    {
        return $this->items;
    }
   
    /**
     * Register recomentation backend
     * @param array $backends classnames of recomedation backends
     */
    public function registerBackends(array $backends)
    {
    	foreach($backends as $class => $details){
    
    		$backend = \App::make($class);
    
    		// Fill detail information used by the Admin interface
    		
    		$backend->info = $details;
    		$this->backends[$backend->getKey()] = $backend;
    		
    		// TODO : read settings to set active engine
    		$this->engine = $backend;
    	}
        
        // Initialize active Recomendation Backend engine
        // Only pass active items to Recomendations Backend
    	$this->engine->items = array_filter($this->items, function($it){
    	    return $it->active;
    	});

    	$this->engine->boot($this);
    	
    	// Temporal
    	$this->populateEngine();
    	
    }
    
    
    /**
     * Return an array of all register Recommendation Backends
     * @return array
     */
    public function getRegisterBackends()
    {
    	return $this->backends;
    }
    
    /**
     * Return and array of recomendations of the given user.
     *
     * @param RainLab\User\Models\User $user
     *
     * @param array $itemKeys
     * An array of Items requested to be returned by the recomendation engine.
     *
     * @param int $limit
     * If limit is not given configured values per Item in the admin interface
     * will be use.<commit_msg>- Stop populating after each request. This was done only for development<commit_after><?php namespace DMA\Recommendations\Classes;

use DMA\Recommendations\Models\Settings;

/**
 * DMA recomendation manager 
 * @author <NAME>
 */
class RecommendationManager
{
    
    /**
     * Dictionary of Recommendation Items
     * @var array
     */
    private $items = [];

    /**
     * Dictionary of Recommendation Backends
     * @var array
     */
    private $backends = [];
    
    /**
     * Flag to keep track if Item Events are 
     * already bind to the engine
     * @var boolean
     */
    private $bindEvents = false;
    
    /**
     * Active recomendation backend engine
     * @var DMA\Recommendations\Classes\Backends\BackendBase
     */
    private $engine = null;
    
    /**
     * Register recomentation items
     * @param array $items classnames of Recommendation Items to be register
     */
    public function registerItems(array $items)
    {
    	foreach($items as $class => $details){
    
    		$it = \App::make($class);
    
    		// Fill detail information used by the Admin interface
    	    $it->info = $details;
            // Is this item active
    	    $it->active = Settings::get(strtolower($it->getKey()) .'_active', false);  	    
    	    
    	    $this->items[$it->getKey()] = $it;
    	
    	}
    	
    }

    
    /**
     * Return an array of all register Recommendation Items
     * @return array
     */
    public function getRegisterItems()
    {
        return $this->items;
    }
   
    /**
     * Register recomentation backend
     * @param array $backends classnames of recomedation backends
     */
    public function registerBackends(array $backends)
    {
    	foreach($backends as $class => $details){
    
    		$backend = \App::make($class);
    
    		// Fill detail information used by the Admin interface
    		
    		$backend->info = $details;
    		$this->backends[$backend->getKey()] = $backend;
    		
    		// TODO : read settings to set active engine
    		$this->engine = $backend;
    	}
        
        // Initialize active Recomendation Backend engine
        // Only pass active items to Recomendations Backend
    	$this->engine->items = array_filter($this->items, function($it){
    	    return $it->active;
    	});

    	$this->engine->boot($this);
    	
    	// Temporal
    	//$this->populateEngine();
    	
    }
    
    
    /**
     * Return an array of all register Recommendation Backends
     * @return array
     */
    public function getRegisterBackends()
    {
    	return $this->backends;
    }
    
    /**
     * Return and array of recomendations of the given user.
     *
     * @param RainLab\User\Models\User $user
     *
     * @param array $itemKeys
     * An array of Items requested to be returned by the recomendation engine.
     *
     * @param int $limit
     * If limit is not given configured values per Item in the admin interface
     * will be use.
{'fraction_non_alphanumeric': 0.10734367013119782, 'fraction_numerical': 0.0, 'mean_word_length': 1.9979570990806945, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 45, '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': '6698166', 'n_tokens_mistral': 1758, 'n_tokens_neox': 1702, 'n_words': 480}