PluginProbe
Stripe Payment Forms by WP Simple Pay – Accept Credit Card Payments + Subscriptions with Stripe / trunk
Stripe Payment Forms by WP Simple Pay – Accept Credit Card Payments + Subscriptions with Stripe vtrunk
4.17.3 trunk 2.2.0 2.3.0 2.3.1 2.3.2 2.3.3 2.4.0 2.4.1 2.5.0 2.5.1 2.5.2 2.5.3 2.6.0 2.6.1 2.6.2 2.6.3 4.10.0 4.11.1 4.12.2 4.14.1 4.14.2 4.14.3 4.15.0 4.16.0 All 59 releases
stripe / src / Repository / BerlinDbRepository.php

BerlinDbRepository.php in Stripe Payment Forms by WP Simple Pay – Accept Credit Card Payments + Subscriptions with Stripe trunk, at src/Repository/BerlinDbRepository.php

202 lines 4.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Repository: BerlinDB
4 *
5 * Normalizes some BerlinDB operations to provide consistency between operations.
6 * All singular operations return a shaped item, or null, and a query returns a
7 * list of shaped items.
8 *
9 * @package SimplePay
10 * @subpackage Core
11 * @copyright Copyright (c) 2022, Sandhills Development, LLC
12 * @license http://opensource.org/licenses/gpl-2.0.php GNU Public License
13 * @since 4.4.5
14 */
15
16 namespace SimplePay\Core\Repository;
17
18 use SimplePay\Vendor\BerlinDB\Database\Base as BerlinDbQuery;
19 use stdClass;
20
21 /**
22 * BerlinDbRepository class.
23 *
24 * @since 4.4.5
25 */
26 class BerlinDbRepository extends BerlinDbQuery implements RepositoryInterface {
27
28 /**
29 * Item shape.
30 *
31 * @since 4.4.5
32 * @var string Fully qualified class name.
33 */
34 protected $shape;
35
36 /**
37 * Item query.
38 *
39 * @since 4.4.5
40 * @var \SimplePay\Vendor\BerlinDB\Database\Query
41 */
42 protected $query;
43
44 /**
45 * BerlinDbRepository.
46 *
47 * @since 4.4.5
48 *
49 * @param string $shape Item shape in the form of a fully qualified class name.
50 * @param string $query BerlinDB Query class in the form of a fully qualified class name.
51 * https://github.com/berlindb/core/blob/master/src/Database/Query.php
52 */
53 public function __construct( $shape, $query ) {
54 $this->shape = $shape;
55 $this->query = new $query; // @phpstan-ignore-line
56 }
57
58 /**
59 * Shapes a single item to a model given a set of data.
60 *
61 * @since 4.4.5
62 *
63 * @param array<mixed> $data Model data.
64 * @return \SimplePay\Core\Model\ModelInterface
65 */
66 private function shape( $data ) {
67 return new $this->shape( (array) $data ); // @phpstan-ignore-line
68 }
69
70 /**
71 * {@inheritdoc}
72 */
73 public function get( $id ) {
74 $item = $this->query->get_item( $id );
75
76 if ( false === $item ) {
77 return null;
78 }
79
80 return $this->shape( (array) $item );
81 }
82
83 /**
84 * {@inheritdoc}
85 */
86 public function get_by( $column_name, $column_value ) {
87 $item = $this->query->get_item_by( $column_name, $column_value );
88
89 if ( false === $item ) {
90 return null;
91 }
92
93 return $this->shape( (array) $item );
94 }
95
96 /**
97 * {@inheritdoc}
98 */
99 public function add( $data ) {
100 $item = $this->query->add_item( $data );
101
102 if ( false === $item ) {
103 return null;
104 }
105
106 /** @var int $item_id */
107 $item_id = $item;
108
109 $item = $this->query->get_item( $item_id );
110
111 return $this->shape( (array) $item );
112 }
113
114 /**
115 * {@inheritdoc}
116 */
117 public function update( $id, $data ) {
118 $item = $this->query->update_item( $id, $data );
119 /** @var \SimplePay\Vendor\BerlinDB\Database\Base $this */
120 $last_result = $this->get_db()->last_result;
121
122 // BerlinDB does not perform an update if the data has not changed but
123 // it still returns false. If an update returns false and the latest
124 // database result's ID does not equal the item being updated then
125 // something truly went wrong.
126 if (
127 false === $item &&
128 ! empty( $last_result ) &&
129 (string) $last_result[0]->id !== (string) $id
130 ) {
131 return null;
132 }
133
134 /** @var \SimplePay\Core\Repository\BerlinDbRepository $this */
135 return $this->get( $id );
136 }
137
138 /**
139 * {@inheritdoc}
140 */
141 public function delete( $id ) {
142 $item = $this->query->get_item( $id );
143
144 if ( false === $item ) {
145 return null;
146 }
147
148 $delete = $this->query->delete_item( $id );
149
150 if ( false === $delete ) {
151 return null;
152 }
153
154 return $this->shape( (array) $item );
155 }
156
157 /**
158 * {@inheritdoc}
159 */
160 public function query( $args = array() ) {
161 if ( isset( $args['count'] ) ) {
162 unset( $args['count'] );
163
164 _doing_it_wrong(
165 __METHOD__,
166 __( 'Counting results via a query is not allowed. Use the ::count() method.', 'stripe' ),
167 '4.4.5'
168 );
169 }
170
171 /** @var array<stdClass> $items */
172 $items = $this->query->query( $args );
173
174 $items = array_map(
175 function( $item ) {
176 return $this->get( $item->id );
177 },
178 $items
179 );
180
181 return array_filter(
182 $items,
183 function( $item ) {
184 return $item instanceof $this->shape;
185 }
186 );
187 }
188
189 /**
190 * {@inheritdoc}
191 */
192 public function count( $args = array() ) {
193 $args['count'] = true;
194
195 /** @var int $count */
196 $count = $this->query->query( $args );
197
198 return $count;
199 }
200
201 }
202