<?php

namespace App\Repositories\Core\User;

use App\Models\Core\CartStorage;
use Darryldecode\Cart\CartCollection;

class ShopCartStorage
{
    public function has($key)
    {
        return CartStorage::find($key);
    }

    public function get($key)
    {
        if (! $this->has($key)) {
            return [];
        }

        return new CartCollection(CartStorage::find($key)->cart_data);
    }

    public function put($key, $value)
    {
        $row = CartStorage::find($key);
        if ($row) {
            $row->cart_data = $value;
            $row->save();
        } else {
            CartStorage::create([
                'id' => $key,
                'cart_data' => $value,
            ]);
        }
    }
}
