Piwik\Plugins\CustomVariables\CustomVariables::getNumUsableCustomVariables PHP Method

getNumUsableCustomVariables() public static method

"Can be used" is identifed by the minimum number of available custom variables across all relevant tables. Eg if there are 6 custom variables installed in log_visit but only 5 in log_conversion, we consider only 5 custom variables as usable.
public static getNumUsableCustomVariables ( ) : integer
return integer
    public static function getNumUsableCustomVariables()
    {
        $cache = Cache::getCacheGeneral();
        $cacheKey = self::MAX_NUM_CUSTOMVARS_CACHEKEY;
        if (!array_key_exists($cacheKey, $cache)) {
            $minCustomVar = null;
            foreach (Model::getScopes() as $scope) {
                $model = new Model($scope);
                $highestIndex = $model->getHighestCustomVarIndex();
                if (!isset($minCustomVar)) {
                    $minCustomVar = $highestIndex;
                }
                if ($highestIndex < $minCustomVar) {
                    $minCustomVar = $highestIndex;
                }
            }
            if (!isset($minCustomVar)) {
                $minCustomVar = 0;
            }
            $cache[$cacheKey] = $minCustomVar;
            Cache::setCacheGeneral($cache);
        }
        return $cache[$cacheKey];
    }

Usage Example

コード例 #1
0
 public function test_getNumUsableCustomVariables_ShouldReturnMinVariables_IfOneTableHasLessEntriesThanOthers()
 {
     $this->assertEquals(5, CustomVariables::getNumUsableCustomVariables());
     $scopes = Model::getScopes();
     // removing custom vars step by step... as soon as one custom var is removed,
     // it should return the min count of available variables
     for ($i = 4; $i != -1; $i--) {
         foreach ($scopes as $scope) {
             $this->dropCustomVar($scope);
             $this->assertSame($i, CustomVariables::getNumUsableCustomVariables());
         }
     }
     $this->assertEquals(0, CustomVariables::getNumUsableCustomVariables());
     // add custom var, only once all custom vars are written it should write return a higher custom var number
     for ($i = 1; $i != 7; $i++) {
         foreach ($scopes as $index => $scope) {
             $isLastIndex = $index === count($scopes) - 1;
             $this->addCustomVar($scope);
             if ($isLastIndex) {
                 $this->assertSame($i, CustomVariables::getNumUsableCustomVariables());
                 // all scopes have been added, it should consider all custom var counts
             } else {
                 $this->assertSame($i - 1, CustomVariables::getNumUsableCustomVariables());
                 // at least one scope is not added and should therefore return the old custom var count until all
                 // tables have been updated
             }
         }
     }
     $this->assertEquals(6, CustomVariables::getNumUsableCustomVariables());
 }
All Usage Examples Of Piwik\Plugins\CustomVariables\CustomVariables::getNumUsableCustomVariables