<?php

namespace apexl\Io\modules\subscription\callbacks;

use apexl\Io\includes\Routes;
use apexl\Io\modules\subscription\entities\subscriptionEntity;

class entityStoreCallback {

    public static function insert($data, $table, $extraStore, $extends)
    {
        if (isset($extends->foreignKey) && isset($extraStore['product_id']) && $extends->entity == 'apexl\Io\modules\subscription\entities\subscriptionEntity') {
            $subscription = new subscriptionEntity();
            foreach (['company_id','user_id','date_starts','date_expires','enabled','max_users','created','product_id','code'] as $field) {
                if (isset($extraStore[$field])) $subscription->$field = $extraStore[$field];
            }
            $subscription->store();
            $data[$extends->foreignKey] = $subscription->id;
        }

        return $data;
    }

    public static function update($data, $table, $extraStore, $extends)
    {
        if (isset($extends->foreignKey) && isset($extraStore['product_id']) && $extends->entity == 'apexl\Io\modules\subscription\entities\subscriptionEntity' && isset($data[$extends->foreignKey])) {
            $subscription = new subscriptionEntity();
            $subscription->load($data[$extends->foreignKey]);
            foreach (['company_id','user_id','date_starts','date_expires','enabled','max_users','created','product_id','code'] as $field) {
                if (isset($extraStore[$field])) $subscription->$field = $extraStore[$field];
            }
            $subscription->store();
            $data[$extends->foreignKey] = $subscription->id;
        }

        return $data;
    }
}