<commit_before><?php namespace Orchestra\Model\Memory;

use Illuminate\Container\Container;
use Orchestra\Memory\Abstractable\Handler;
use Orchestra\Memory\MemoryHandlerInterface;

class UserMetaRepository extends Handler implements MemoryHandlerInterface
{
    /**
     * Storage name.
     *
     * @var string
     */
    protected $storage = 'user';

    /**
     * Setup a new memory handler.
     *
     * @param  string                           $name
     * @param  array                            $config
     * @param  \Illuminate\Container\Container  $repository
     */
    public function __construct($name, array $config, Container $repository)
    {
        $this->repository = $repository;

        parent::__construct($name, $config);
    }

    /**
     * Initiate the instance.
     *
     * @return array
     */
    public function initiate()
    {
        return array();
    }

    /**
     * Get value from database.
     *
     * @param  string   $key
     * @return mixed
     */
    public function retrieve($key)
    {
        list($name, $userId) = explode('/user-', $key);

        $userMeta = $this->getModel()->search($name, $userId)->first();

        if (! is_null($userMeta)) {
            if (! $value = @unserialize($userMeta->value)) {
                $value = $userMeta->value;
            }

            $this->addKey($key, array(
                'id'    => $userMeta->id,
                'value' => $value,
            ));

            return $value;
        }

        return null;
    }

    /**
     * Add a finish event.
     *
     * @param  array   $items
     * @return boolean
     */
    public function finish(array $items = array())
    {
        foreach ($items as $key => $value) {
            $this->save($key, $value);
        }

        return true;
    }

    /**
     * Save user meta to memory.
     *
     * @param  mixed    $key
     * @param  mixed    $value
     * @return void
     */
    protected function save($key, $value)
    {
        $isNew = $this->isNewKey($key);

        list($name, $userId) = explode('/user-', $key);

        // We should be able to ignore this if user id is empty or checksum
        // return the same value (no change occured).
        if ($this->check($key, $value) or empty($userId)) {
            return ;
        }

        $meta = $this->getModel()->search($name, $userId)->first();

        // Deleting a configuration is determined by ':to-be-deleted:'. It
        // would be extremely weird if that is used for other purposed.
        if (is_null($value) or $value === ':to-be-deleted:') {
            ! is_null($meta) and $meta->delete();
            return ;
        }

        // If the content is a new configuration, let push it as a insert
        // instead of an update to Eloquent.
        if (true === $isNew and is_null($meta)) {
            $meta = $this->getModel();

            $meta->name    = $name;<commit_msg>Small refactor to saving process.<commit_after><?php namespace Orchestra\Model\Memory;

use Illuminate\Container\Container;
use Orchestra\Memory\Abstractable\Handler;
use Orchestra\Memory\MemoryHandlerInterface;

class UserMetaRepository extends Handler implements MemoryHandlerInterface
{
    /**
     * Storage name.
     *
     * @var string
     */
    protected $storage = 'user';

    /**
     * Setup a new memory handler.
     *
     * @param  string                           $name
     * @param  array                            $config
     * @param  \Illuminate\Container\Container  $repository
     */
    public function __construct($name, array $config, Container $repository)
    {
        $this->repository = $repository;

        parent::__construct($name, $config);
    }

    /**
     * Initiate the instance.
     *
     * @return array
     */
    public function initiate()
    {
        return array();
    }

    /**
     * Get value from database.
     *
     * @param  string   $key
     * @return mixed
     */
    public function retrieve($key)
    {
        list($name, $userId) = explode('/user-', $key);

        $userMeta = $this->getModel()->search($name, $userId)->first();

        if (! is_null($userMeta)) {
            if (! $value = @unserialize($userMeta->value)) {
                $value = $userMeta->value;
            }

            $this->addKey($key, array(
                'id'    => $userMeta->id,
                'value' => $value,
            ));

            return $value;
        }

        return null;
    }

    /**
     * Add a finish event.
     *
     * @param  array   $items
     * @return boolean
     */
    public function finish(array $items = array())
    {
        foreach ($items as $key => $value) {
            $this->save($key, $value);
        }

        return true;
    }

    /**
     * Save user meta to memory.
     *
     * @param  mixed    $key
     * @param  mixed    $value
     * @return void
     */
    protected function save($key, $value)
    {
        $isNew = $this->isNewKey($key);

        list($name, $userId) = explode('/user-', $key);

        // We should be able to ignore this if user id is empty or checksum
        // return the same value (no change occured).
        if ($this->check($key, $value) or empty($userId)) {
            return ;
        }

        return $this->saving($name, $userId, $value, $isNew);
    }

    /**
     * Process saving the value to memory.
     *
     * @param  string   $name
     * @param  mixed    $userId
     * @param  mixed    $value
     * @param  boolean  $isNew
     * @return void
     */
    protected function saving($name, $userId, $value = null, $isNew = true)
    {
        $meta = $this->getModel()->search($name, $userId)->first();

        // Deleting a configuration is determined by ':to-be-deleted:'. It
        // would be extremely weird if that is used for other purposed.
        if (is_null($value) or $value === ':to-be-deleted:') {
            ! is_null($meta) and $meta->delete();
            return ;
        }

        // If the content is a new configuration, let push it as a insert
        // instead of an update to Eloquent.
        if (true === $isNew and is_null($meta)) {
            $meta = $this->getModel();

            $meta->name    = $name;
{'fraction_non_alphanumeric': 0.13513513513513514, 'fraction_numerical': 0.0, 'mean_word_length': 1.6980155306298532, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 44, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3948349', 'n_tokens_mistral': 1796, 'n_tokens_neox': 1767, 'n_words': 486}