Ejemplo n.º 1
0
 /** Add value to counter. If counter not exists set counter to value. Save to database.
  * @param Date      $day   - counter date
  * @param string    $name  - counter name
  * @param int|float $value - number to add
  * @throws ArgumentTypeException
  * @throws SystemException
  */
 public function addCounter(Date $day, $name, $value)
 {
     if (!is_string($name)) {
         throw new ArgumentTypeException('name', 'string');
     }
     if (!is_numeric($value)) {
         throw new ArgumentTypeException('value', 'numeric');
     }
     if (($id = $this->id) === null) {
         throw new SystemException('Cannot add counter without context!');
     }
     static $types;
     if (!$types) {
         $types = CounterManager::getTypes();
     }
     if (!($type = $types[$name])) {
         throw new SystemException('Undefined counter type!');
     }
     if (!$type['ACTIVE']) {
         return;
     }
     // save to database
     $primary = array('DAY' => $day, 'CONTEXT_ID' => $id, 'NAME' => $name);
     $data = array('VALUE' => new DB\SqlExpression('?# + ?f', 'VALUE', $value));
     $result = ContextCounterDayTable::update($primary, $data);
     if ($result->getAffectedRowsCount() === 0) {
         try {
             $result = ContextCounterDayTable::add($primary + array('VALUE' => $value));
         } catch (DB\SqlQueryException $e) {
             $result = ContextCounterDayTable::update($primary, $data);
         }
     }
     $result->isSuccess();
     // TODO isSuccess
 }