Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
11.11% |
1 / 9 |
|
33.33% |
1 / 3 |
CRAP | |
0.00% |
0 / 1 |
| Bitcoin_Transaction_Factory | |
11.11% |
1 / 9 |
|
33.33% |
1 / 3 |
22.56 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| get_by_wp_post_id | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
12 | |||
| get_by_wp_post | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Mostly takes a WP_Post and returns a Bitcoin_Transaction |
| 4 | * |
| 5 | * @package brianhenryie/bh-wp-bitcoin-gateway |
| 6 | */ |
| 7 | |
| 8 | namespace BrianHenryIE\WP_Bitcoin_Gateway\API\Repositories\Factories; |
| 9 | |
| 10 | use BrianHenryIE\WP_Bitcoin_Gateway\API\Model\Payments\Bitcoin_Transaction; |
| 11 | use BrianHenryIE\WP_Bitcoin_Gateway\API\Model\Payments\Bitcoin_Transaction_WP_Post_Interface; |
| 12 | use BrianHenryIE\WP_Bitcoin_Gateway\API\Model\Payments\Transaction; |
| 13 | use BrianHenryIE\WP_Bitcoin_Gateway\JsonMapper\Exception\BuilderException; |
| 14 | use BrianHenryIE\WP_Bitcoin_Gateway\JsonMapper\Exception\ClassFactoryException; |
| 15 | use BrianHenryIE\WP_Bitcoin_Gateway\JsonMapper\JsonMapperInterface; |
| 16 | use InvalidArgumentException; |
| 17 | use WP_Post; |
| 18 | |
| 19 | /** |
| 20 | * @phpstan-type MoneySerializedArray array{amount:string,currency:string} |
| 21 | */ |
| 22 | class Bitcoin_Transaction_Factory { |
| 23 | |
| 24 | /** |
| 25 | * @param JsonMapperInterface $json_mapper To parse JSON to typed objects. |
| 26 | */ |
| 27 | public function __construct( |
| 28 | protected JsonMapperInterface $json_mapper, |
| 29 | ) { |
| 30 | } |
| 31 | |
| 32 | /** |
| 33 | * Get a Bitcoin transaction by WordPress post ID. |
| 34 | * |
| 35 | * @param int $post_id The WordPress post id this wallet is stored under. |
| 36 | * |
| 37 | * @throws InvalidArgumentException When the supplied post_id is not a post of this type. |
| 38 | * @throws BuilderException When the JSON mapper fails to build the transaction object from the post content. |
| 39 | * @throws ClassFactoryException When the JSON mapper cannot create required class instances during deserialization. |
| 40 | */ |
| 41 | public function get_by_wp_post_id( int $post_id ): Bitcoin_Transaction { |
| 42 | $post = get_post( $post_id ); |
| 43 | if ( ! ( $post instanceof WP_Post ) || Bitcoin_Transaction_WP_Post_Interface::POST_TYPE !== $post->post_type ) { |
| 44 | throw new InvalidArgumentException( 'post_id ' . $post_id . ' is not a ' . Bitcoin_Transaction_WP_Post_Interface::POST_TYPE . ' post object' ); |
| 45 | } |
| 46 | |
| 47 | return $this->get_by_wp_post( $post ); |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Takes a WP_Post and gets the values (primitives?) to create a Bitcoin_Transaction. |
| 52 | * |
| 53 | * TODO: inject JsonMapper. |
| 54 | * |
| 55 | * @param WP_Post $post The backing WP_Post for this Bitcoin_Transaction. |
| 56 | * |
| 57 | * @throws BuilderException When the JSON mapper fails to deserialize the transaction data from post_content. |
| 58 | * @throws ClassFactoryException When the JSON mapper cannot instantiate required classes like DateTimeInterface or Money during object creation. |
| 59 | */ |
| 60 | public function get_by_wp_post( WP_Post $post ): Bitcoin_Transaction { |
| 61 | |
| 62 | /** @var Transaction $transaction */ |
| 63 | $transaction = $this->json_mapper->mapToClassFromString( $post->post_content, Transaction::class ); |
| 64 | |
| 65 | // If this is an empty array, something is up. We never save a transaction unless it is relevant to an address of ours. |
| 66 | /** @var array<int,string> $addresses post_id:bitcoin_address */ |
| 67 | $addresses = array_filter( |
| 68 | (array) get_post_meta( $post->ID, Bitcoin_Transaction_WP_Post_Interface::BITCOIN_ADDRESSES_POST_IDS_META_KEY, true ) |
| 69 | ); |
| 70 | |
| 71 | return new Bitcoin_Transaction( |
| 72 | post_id: $post->ID, |
| 73 | transaction: $transaction, |
| 74 | bitcoin_addresses: $addresses, |
| 75 | ); |
| 76 | } |
| 77 | } |