Skip to content
Snippets Groups Projects
PhaseAbstract.php 3.90 KiB
<?php
/**
 * Copyright (c) 2012-2014, Agence Française Informatique (AFI). All rights reserved.
 *
 * BOKEH is free software; you can redistribute it and/or modify
 * it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE as published by
 * the Free Software Foundation.
 *
 * There are special exceptions to the terms and conditions of the AGPL as it
 * is applied to this software (see README file).
 *
 * BOKEH is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
 *
 * You should have received a copy of the GNU AFFERO GENERAL PUBLIC LICENSE
 * along with BOKEH; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA
 */


abstract class Class_Cosmogramme_Integration_PhaseAbstract {
  use Trait_TimeSource, Trait_StaticFileSystem, Trait_Translator, Trait_MemoryCleaner;

  protected $_label = '';
  protected $_phase, $_log, $_printer, $_chrono, $_is_time_out;


  public function __construct($phase, $log, $chrono) {
    $this->_phase = $phase;
    $this->_log = $log;
    $this->_chrono = $chrono;
    $this->_is_time_out = false;
  }


  public function setSqlAdapter($adapter) {
    $this->_sql_adapter = $adapter;
    return $this;
  }


  protected function getSqlAdapter() {
    if(!$this->_sql_adapter)
      $this->_sql_adapter = Zend_Db_Table::getDefaultAdapter();

    return $this->_sql_adapter;
  }


  protected abstract function _execute();
  protected abstract function _init($phase);


  /** @return array **/
  protected function _previousPhaseIds() {
    return [static::MY_ID - 1];
  }


  protected function _prepareNewPhase() {
    if (!$this->_phase->isIdIn($this->_previousPhaseIds()))
      return $this->_phase;

    $new_phase = (new Class_Cosmogramme_Integration_Phase(static::MY_ID))
      ->beSameCronAs($this->_phase)
      ->beSameCountAs($this->_phase);

    $this->_log->ecrire('<h4>' . $this->_label . '</h4>');
    $this->_init($new_phase);
    return $new_phase;
  }


  /** @return Class_Cosmogramme_Integration_Phase */
  public function run() {
    Class_CosmoVar::setValueOf('traitement_phase', $this->_label);
    $this->_phase = $this->_prepareNewPhase();

    if (!$this->_isMyTurn())
      return $this->_phase;

    $this->_printLabel();

    $this->_execute();

    return $this->_phase;
  }


  protected function _isMyTurn() {
    return $this->_phase->isId(static::MY_ID);
  }


  protected function _getData($name) {
    return $this->_phase->getData($name);
  }


  protected function _setData($name, $value) {
    $this->_phase->setData($name, $value);
    return $this;
  }


  protected function _incrementData($name) {
    $this->_phase->incrementData($name);
    return $this;
  }


  protected function _incrementCount($name) {
    $this->_phase->incrementCount($name);
    return $this;
  }


  protected function _printLabel() {
    if (!$this->_phase->isCron() && $this->_wasRunning())
      $this->_getPrinter()->nextPutAll('<h4>' . $this->_label . '</h4>');
  }


  protected function _wasRunning() {
    return false;
  }


  /** @category testing */
  public function setPrinter($printer) {
    $this->_printer = $printer;
    return $this;
  }


  protected function _getPrinter() {
    if (null !== $this->_printer)
      return $this->_printer;
    return new Class_Cosmogramme_Integration_PhasePrinter();
  }


  public function isTimeOut() {
    if (!$this->_chrono)
      return false;
    return !$this->_phase->isCron()
      && $this->_chrono->mainElapsed() > $this->_chrono->timeout();
  }


  protected function _resetDbConnection() {
    if (!$this->_db_reset)
      return $this;

    Storm_Model_Abstract::unsetLoaders();
    Zend_Db_Table::getDefaultAdapter()->closeConnection();
    setupDatabase(loadConfig());
    gc_collect_cycles();
    return $this;
  }
}