<?php

class catalyzCacheCreateTask extends sfBaseTask {
    protected function configure() {
        $this->namespace = 'catalyz';
        $this->name = 'cache-create';
        $this->briefDescription = 'Create Catalyz pages cache';
        $this->detailedDescription = <<<EOF
The [catalyz:cache-create|INFO] task creates the Catalyz pages cache
EOF;

        $this->addArguments(array(new sfCommandArgument('node_id', sfCommandArgument::OPTIONAL, 'The node id'),
                new sfCommandArgument('culture', sfCommandArgument::OPTIONAL, 'The culture'),
                new sfCommandArgument('application', sfCommandArgument::OPTIONAL, 'The application name', 'frontend'),
                ));
        $this->addOption('env', null, sfCommandOption::PARAMETER_REQUIRED, 'The environment', 'dev');
        $this->addOption('connection', null, sfCommandOption::PARAMETER_REQUIRED, 'The connection name', 'propel');
    }

    protected function execute($arguments = array(), $options = array()) {
        // Database initialization
        $configuration = ProjectConfiguration::getApplicationConfiguration($arguments['application'], $options['env'], true);
        $databaseManager = new sfDatabaseManager($configuration);
        sfContext::createInstance($configuration);

        if ((!empty($arguments['node_id']) && empty($arguments['culture'])) || (empty($arguments['node_id']) && !empty($arguments['culture']))) {
            throw new sfCommandException('Please provide all parameters <node_id> and <culture> or none of them.');
        }

        CatalyzCache::create($arguments['node_id'], $arguments['culture']);
        $this->Log('Cache created');
    }
}