PHP code example of mothership-ec / cog-mothership-cp

1. Go to this page and download the library: Download mothership-ec/cog-mothership-cp library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

mothership-ec / cog-mothership-cp example snippets


$services->extend('statistics', function($statistics, $c) {
    // Simple value counter
	$statistics->add(new MyDataset($c['db.query'], $c['statistics.counter'], $c['statistics.range.date']));

	// Key based counter
	$statistics->add(new MyKeyDataset($c['db.query'], $c['statistics.counter.key'], $c['statistics.range.date']));
});

class MyDataset extends AbstractDataset
{
    public function getName()
    {
        return 'my.dataset';
    }

    public function getPeriodLength()
    {
        return static::DAILY;
    }

    public function rebuild()
    {
        // add rebuilding sql to transaction and commit if not overridden
    }
}

$dataset = $this->get('statistics')->get('my.dataset');

$dataset->counter->push($value);

$dataset->counter->set($key, $value);

$dataset->counter->get();
$dataset->counter->get($key);

$dataset->counter->increment();
$dataset->counter->decrement($step);

$dataset->counter->decrement($key);
$dataset->counter->increment($key, $step);

// From the beginning of time
$values = $dataset->range->getValues(0);

// From a week ago
$values = $dataset->range->getValues(
    $dataset->range->getWeekAgo()
);

// Previous month
$values = $dataset->range->getValues(
    $dataset->range->getMonthAgo(1),
    $dataset->range->getMonthAgo()
);

$data = $dataset->range->getKeyValues(0);

$monthAverage = $dataset->range->getAverage(
    $dataset->range->getMonthAgo()
);

$yearToDate = $dataset->range->getTotal(
    $dataset->range->getYearAgo()
);