Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
6 / 6
CRAP
100.00% covered (success)
100.00%
28 / 28
AbstractOption
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
6 / 6
12
100.00% covered (success)
100.00%
28 / 28
 getValueFromWordPress
100.00% covered (success)
100.00%
1 / 1
2
100.00% covered (success)
100.00%
5 / 5
 isAutoload
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
1 / 1
 setAutoload
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
2 / 2
 deleteFromWP
100.00% covered (success)
100.00%
1 / 1
2
100.00% covered (success)
100.00%
5 / 5
 flush
100.00% covered (success)
100.00%
1 / 1
4
100.00% covered (success)
100.00%
11 / 11
 updateValue
100.00% covered (success)
100.00%
1 / 1
2
100.00% covered (success)
100.00%
4 / 4
<?php
declare(strict_types=1);
namespace Korobochkin\WPKit\Options;
use Korobochkin\WPKit\DataComponents\AbstractNode;
use Korobochkin\WPKit\DataComponents\Traits\DeleteTrait;
/**
 * Class AbstractOption
 * @package Korobochkin\WPKit\Options
 */
abstract class AbstractOption extends AbstractNode implements OptionInterface
{
    use DeleteTrait;
    /**
     * @var $autoload bool Flag which define how option should be loaded by WordPress.
     */
    protected $autoload = true;
    /**
     * @inheritdoc
     */
    public function getValueFromWordPress()
    {
        $name = $this->getName();
        if (!$name) {
            throw new \LogicException(
                'You must specify the name of option before calling any methods using name of option.'
            );
        }
        return get_option($name);
    }
    /**
     * @inheritdoc
     */
    public function isAutoload()
    {
        return $this->autoload;
    }
    /**
     * @inheritdoc
     */
    public function setAutoload($autoload)
    {
        $this->autoload = (bool) $autoload;
        return $this;
    }
    /**
     * @inheritdoc
     */
    public function deleteFromWP()
    {
        $name = $this->getName();
        if (!$name) {
            throw new \LogicException(
                'You must specify the name of option before calling any methods using name of option.'
            );
        }
        return delete_option($name);
    }
    /**
     * @inheritdoc
     */
    public function flush()
    {
        if ($this->getDataTransformer()) {
            $raw = $this->getDataTransformer()->transform($this->localValue);
        } else {
            $raw =& $this->localValue;
        }
        $name = $this->getName();
        if (!$name) {
            throw new \LogicException(
                'You must specify the name of option before calling any methods using name of option.'
            );
        }
        $result = update_option($name, $raw, $this->isAutoload());
        if ($result) {
            $this->setLocalValue(null);
        }
        return $result;
    }
    /**
     * @inheritdoc
     */
    public function updateValue($value, $autoload = null)
    {
        $this->setLocalValue($value);
        if (!is_null($autoload)) {
            $this->setAutoload($autoload);
        }
        return $this->flush();
    }
}