Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 28 |
|
0.00% |
0 / 7 |
CRAP | |
0.00% |
0 / 1 |
| Data_Sync_Entry_Adapter | |
0.00% |
0 / 28 |
|
0.00% |
0 / 7 |
240 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| is | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| get | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
30 | |||
| set | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| merge | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
12 | |||
| delete | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
| get_parser | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace Automattic\Jetpack\WP_JS_Data_Sync; |
| 4 | |
| 5 | use Automattic\Jetpack\Schema\Parser; |
| 6 | use Automattic\Jetpack\Schema\Schema_Error; |
| 7 | use Automattic\Jetpack\Schema\Schema_Parser; |
| 8 | use Automattic\Jetpack\WP_JS_Data_Sync\Contracts\Data_Sync_Entry; |
| 9 | use Automattic\Jetpack\WP_JS_Data_Sync\Contracts\Entry_Can_Delete; |
| 10 | use Automattic\Jetpack\WP_JS_Data_Sync\Contracts\Entry_Can_Get; |
| 11 | use Automattic\Jetpack\WP_JS_Data_Sync\Contracts\Entry_Can_Merge; |
| 12 | use Automattic\Jetpack\WP_JS_Data_Sync\Contracts\Entry_Can_Set; |
| 13 | |
| 14 | /** |
| 15 | * Data Sync Entry Adapter: |
| 16 | * ======================== |
| 17 | * This class takes in any instance that subscribes to one or more "Entry_Can_*" interfaces |
| 18 | * and adapts it to give it a predictable interface. |
| 19 | * |
| 20 | * This makes it possible to have an Entry class that only subscribes to "Entry_Can_Get" |
| 21 | * yet still have all the other methods (set/merge/delete) available. |
| 22 | * |
| 23 | * Entry Adapter will infer whether an object is able to perform actions (get,set,merge,delete) |
| 24 | * based on whether the object is an instance of the corresponding interface (Entry_Can_*). |
| 25 | */ |
| 26 | final class Data_Sync_Entry_Adapter implements Data_Sync_Entry { |
| 27 | |
| 28 | /** |
| 29 | * @var Entry_Can_Get&Entry_Can_Set|Entry_Can_Get&Entry_Can_Merge|Entry_Can_Get&Entry_Can_Delete - The data sync entry. |
| 30 | */ |
| 31 | private $entry; |
| 32 | |
| 33 | /** |
| 34 | * @var Schema_Parser $parser - The schema for the data sync entry. |
| 35 | */ |
| 36 | private $parser; |
| 37 | |
| 38 | /** |
| 39 | * For more explanation, see the class docblock. |
| 40 | * |
| 41 | * @see Data_Sync_Entry_Adapter |
| 42 | * The constructor accepts any entry that subscribes to at least "Entry_Can_Get", but can also |
| 43 | * subscribe to any of the other Entry_Can_* interfaces. |
| 44 | * |
| 45 | * @param Entry_Can_Get $entry - The data sync entry. |
| 46 | * @param Parser $schema - The schema for the data sync entry. |
| 47 | */ |
| 48 | public function __construct( $entry, $schema ) { |
| 49 | $this->entry = $entry; |
| 50 | $this->parser = $schema; |
| 51 | } |
| 52 | |
| 53 | public function is( $interface_reference ) { |
| 54 | return $this->entry instanceof $interface_reference; |
| 55 | } |
| 56 | |
| 57 | public function get() { |
| 58 | |
| 59 | if ( $this->parser->has_fallback() ) { |
| 60 | $default = $this->parser->get_fallback(); |
| 61 | $value = $this->entry->get( $default ); |
| 62 | return $this->parser->parse( $value ); |
| 63 | } |
| 64 | |
| 65 | // If WordPress debug is enabled, don't hide exceptions. |
| 66 | if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) { |
| 67 | return $this->parser->parse( $this->entry->get() ); |
| 68 | } |
| 69 | |
| 70 | // If WordPress debug is disabled, attempt to recover by just returning the value |
| 71 | try { |
| 72 | return $this->parser->parse( $this->entry->get() ); |
| 73 | } catch ( Schema_Error $error ) { |
| 74 | return $this->entry->get(); |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | public function set( $value ) { |
| 79 | if ( $this->is( Entry_Can_Set::class ) ) { |
| 80 | $parsed_value = $this->parser->parse( $value ); |
| 81 | $this->entry->set( $parsed_value ); |
| 82 | } |
| 83 | return $this->get(); |
| 84 | } |
| 85 | |
| 86 | public function merge( $partial_value ) { |
| 87 | if ( $this->is( Entry_Can_Merge::class ) ) { |
| 88 | if ( $this->parser->has_fallback() ) { |
| 89 | $default = $this->parser->get_fallback(); |
| 90 | $existing_value = $this->entry->get( $default ); |
| 91 | } else { |
| 92 | $existing_value = $this->entry->get(); |
| 93 | } |
| 94 | $updated_value = $this->entry->merge( $existing_value, $partial_value ); |
| 95 | $this->set( $updated_value ); |
| 96 | } |
| 97 | return $this->get(); |
| 98 | } |
| 99 | |
| 100 | public function delete() { |
| 101 | if ( $this->is( Entry_Can_Delete::class ) ) { |
| 102 | $this->entry->delete(); |
| 103 | } |
| 104 | return $this->get(); |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * @return Parser |
| 109 | */ |
| 110 | public function get_parser() { |
| 111 | return $this->parser; |
| 112 | } |
| 113 | } |