public function countByType($viewableType): int
    {
        if ($viewableType instanceof ViewableContract) {
            $viewableType = $viewableType->getMorphClass();
        }

        $cacheKey = Key::createForType($viewableType, $this->period ?? Period::create(), $this->unique);

        // Return cached views count if it exists
        if ($this->shouldCache) {
            $cachedViewsCount = $this->cache->get($cacheKey);

            if ($cachedViewsCount !== null) {
                return (int) $cachedViewsCount;
            }
        }

        $query = app(ViewContract::class)->where('viewable_type', $viewableType);

        if ($period = $this->period) {
            $query->withinPeriod($period);
        }

        if ($this->unique) {
            $viewsCount = $query->uniqueVisitor()->count('visitor');
        } else {
            $viewsCount = $query->count();
        }

        if ($this->shouldCache) {
            $this->cache->put($cacheKey, $viewsCount, $this->cacheLifetime);
        }

        return $viewsCount;
    }
public function countByType($viewableType): int
    {
        if ($viewableType instanceof ViewableContract) {
            $viewableType = $viewableType->getMorphClass();
        }

        $cacheKey = Key::createForType($viewableType, $this->period ?? Period::create(), $this->unique);

        // Return cached views count if it exists
        if ($this->shouldCache) {
            $cachedViewsCount = $this->cache->get($cacheKey);

            if ($cachedViewsCount !== null) {
                return (int) $cachedViewsCount;
            }
        }

        $query = app(ViewContract::class)->where('viewable_type', $viewableType);

        if ($period = $this->period) {
            $query->withinPeriod($period);
        }

        if ($this->unique) {
            $viewsCount = $query->uniqueVisitor()->count('visitor');
        } else {
            $viewsCount = $query->count();
        }

        if ($this->shouldCache) {
            $this->cache->put($cacheKey, $viewsCount, $this->cacheLifetime);
        }

        return $viewsCount;
    }
public function countByType ( $ viewableType ) : int { if ( $ viewableType instanceof ViewableContract ) { $ viewableType = $ viewableType -> getMorphClass ( ) ; } $ cacheKey = Key :: createForType ( $ viewableType , $ this -> period ?? Period :: create ( ) , $ this -> unique ) ; // Return cached views count if it exists if ( $ this -> shouldCache ) { $ cachedViewsCount = $ this -> cache -> get ( $ cacheKey ) ; if ( $ cachedViewsCount !== null ) { return ( int ) $ cachedViewsCount ; } } $ query = app ( ViewContract :: class ) -> where ( 'viewable_type' , $ viewableType ) ; if ( $ period = $ this -> period ) { $ query -> withinPeriod ( $ period ) ; } if ( $ this -> unique ) { $ viewsCount = $ query -> uniqueVisitor ( ) -> count ( 'visitor' ) ; } else { $ viewsCount = $ query -> count ( ) ; } if ( $ this -> shouldCache ) { $ this -> cache -> put ( $ cacheKey , $ viewsCount , $ this -> cacheLifetime ) ; } return $ viewsCount ; }
Count the views for a viewable type.

@param  string|  $viewableType
@return int
Count the views for a viewable type .