Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 16
0.00% covered (danger)
0.00%
0 / 7
CRAP
0.00% covered (danger)
0.00%
0 / 1
Async_Option
0.00% covered (danger)
0.00%
0 / 16
0.00% covered (danger)
0.00%
0 / 7
90
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 get
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 set
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
12
 delete
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 key
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 has_errors
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 get_errors
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3namespace Automattic\Jetpack\Packages\Async_Option;
4
5use Automattic\Jetpack\Packages\Async_Option\Storage\Storage;
6
7class Async_Option {
8
9    /**
10     * @var string
11     */
12    private $key;
13
14    /**
15     * @var Storage
16     */
17    protected $storage;
18
19    /**
20     * @var Async_Option_Template
21     */
22    protected $option;
23
24    /**
25     * @param $namespace string
26     * @param $key       string
27     * @param $value     Async_Option_Template
28     */
29    public function __construct( $namespace, $key, $value ) {
30        $this->key     = $key;
31        $this->option  = $value;
32        $this->storage = $this->option->setup_storage( $namespace );
33    }
34
35    public function get() {
36        return $this->option->transform(
37            // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase, VariableAnalysis.CodeAnalysis.VariableAnalysis.UndefinedVariable -- False positive on the latter.
38            $this->storage->get( $this->key, ( $this->option )::$DEFAULT_VALUE )
39        );
40    }
41
42    public function set( $input ) {
43
44        $value = $this->option->parse( $input );
45
46        if ( true !== $this->option->validate( $value ) ) {
47            return $this->option->get_errors();
48        }
49
50        if ( ! empty( $this->storage ) ) {
51            return $this->storage->set( $this->key, $this->option->sanitize( $value ) );
52        }
53
54        return false;
55    }
56
57    public function delete() {
58        return $this->storage->delete( $this->key );
59    }
60
61    public function key() {
62        return $this->key;
63    }
64
65    public function has_errors() {
66        return $this->option->has_errors();
67    }
68
69    public function get_errors() {
70        return $this->option->get_errors();
71    }
72}