<?php

namespace App\PackagesPublic\Capsule\Services;

use App\Packages\Capsule\Models\Capsule;
use App\Packages\Capsule\Models\CapsuleStats;

class ShareStatisticService
{

    /** @var Capsule */
    public $capsule;

    public function __construct(Capsule $capsule)
    {
        $this->capsule = $capsule;
    }

    public function addWebView()
    {
        $share_stats = $this->shareStats();
        $share_stats->increment('web_views');
    }

    /**
     * @return CapsuleStats
     */
    private function shareStats()
    {
        if ( $share_stats = $this->capsule->share_stats ) {
            return $share_stats;
        }

        $share_stats = new CapsuleStats();
        $share_stats->capsule_id = $this->capsule->id;
        $share_stats->save();

        return $share_stats;
    }

}
