<?php

class catalyzCacheClearTask extends sfBaseTask {
    protected function configure() {
        $this->namespace = 'catalyz';
        $this->name = 'cache-clear';
        $this->briefDescription = 'Clear Catalyz pages cache';
        $this->detailedDescription = <<<EOF
The [catalyz:cache-clear|INFO] task clears 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);

        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::clear($arguments['node_id'], $arguments['culture']);
        $this->Log('Cache cleared');
    }
}