Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
78.57% covered (warning)
78.57%
66 / 84
62.50% covered (warning)
62.50%
10 / 16
CRAP
0.00% covered (danger)
0.00%
0 / 1
Licence
78.57% covered (warning)
78.57%
66 / 84
62.50% covered (warning)
62.50%
10 / 16
28.21
0.00% covered (danger)
0.00%
0 / 1
 get_licence_statuses
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
2
 get_licence_key
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 set_licence_key
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 get_status
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 set_status
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 get_expiry_date
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 set_expiry_date
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 get_last_updated
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 set_last_updated
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 __serialize
100.00% covered (success)
100.00%
12 / 12
100.00% covered (success)
100.00%
1 / 1
8
 __unserialize
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
 serialize
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 unserialize
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 jsonSerialize
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 is_active
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 get_licence_object_schema_properties
100.00% covered (success)
100.00%
46 / 46
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2/**
3 * POJO for the licence details themselves:
4 * * the licence key, status, expiry date, last updated date.
5 *
6 * TODO: use the last updated time so if there is no communication to the licence server, it can be known if it's an old problem or maybe transient.
7 *
8 * @package brianhenryie/bh-wp-plugin-updater
9 */
10
11namespace BrianHenryIE\WP_Plugin_Updater;
12
13use DateTimeImmutable;
14use DateTimeInterface;
15
16class Licence implements \Serializable, \JsonSerializable {
17
18    /**
19     * The licence key itself. Will be null until set.
20     */
21    protected ?string $licence_key = null;
22
23    /**
24     * The status. Enum TBD. TODO.
25     */
26    protected string $status = 'unknown'; // 'empty'
27
28    protected ?DateTimeInterface $expiry_date = null;
29
30    protected ?DateTimeInterface $last_updated = null;
31
32    /**
33     * The available license status types.
34     */
35    public static function get_licence_statuses(): array {
36        return array(
37            'valid'           => __( 'Valid', 'bh-wp-plugin-updater' ),
38            'deactivated'     => __( 'Deactivated', 'bh-wp-plugin-updater' ),
39            'max_activations' => __( 'Max Activations reached', 'bh-wp-plugin-updater' ),
40            'invalid'         => __( 'Invalid', 'bh-wp-plugin-updater' ),
41            'inactive'        => __( 'Inactive', 'bh-wp-plugin-updater' ),
42            'active'          => __( 'Active', 'bh-wp-plugin-updater' ),
43            'expiring'        => __( 'Expiring', 'bh-wp-plugin-updater' ),
44            'expired'         => __( 'Expired', 'bh-wp-plugin-updater' ),
45        );
46    }
47
48    /**
49     * Get the licence key itself.
50     */
51    public function get_licence_key() {
52        return $this->licence_key;
53    }
54
55    /**
56     * Set the licence key
57     *
58     * @param string $licence_key licence key.
59     */
60    public function set_licence_key( string $licence_key ): void {
61        $this->licence_key = $licence_key;
62    }
63
64    /**
65     * Get the licence status.
66     */
67    public function get_status(): string {
68        return $this->status;
69    }
70
71    /**
72     * Set the licence status
73     *
74     * @param string $status licence status.
75     */
76    public function set_status( string $status ): void {
77        $this->status = $status;
78    }
79
80    /**
81     * Get the licence expiry date.
82     */
83    public function get_expiry_date(): ?DateTimeInterface {
84        return $this->expiry_date;
85    }
86
87    /**
88     * Set the licence expires date.
89     *
90     * @param DateTimeInterface $expiry_date licence expiry date.
91     */
92    public function set_expiry_date( DateTimeInterface $expiry_date ): void {
93        $this->expiry_date = $expiry_date;
94    }
95
96    public function get_last_updated(): ?DateTimeInterface {
97        return $this->last_updated;
98    }
99
100    public function set_last_updated( ?DateTimeInterface $last_updated ): void {
101        $this->last_updated = $last_updated;
102    }
103
104    /**
105     * Serialize the object to an array.
106     *
107     * @used-by serialize()
108     */
109    public function __serialize(): array {
110
111        $arr         = get_object_vars( $this );
112        $ordered_arr = array();
113        foreach ( self::get_licence_object_schema_properties() as $property_name => $property_schema ) {
114            if ( isset( $arr[ $property_name ] ) && $arr[ $property_name ] instanceof DateTimeInterface ) {
115                $ordered_arr[ $property_name ] = $arr[ $property_name ]->format( \DateTimeInterface::ATOM );
116                continue;
117            }
118            if ( isset( $arr[ $property_name ] ) ) {
119                $ordered_arr[ $property_name ] = $arr[ $property_name ];
120                continue;
121            }
122            if ( isset( $property_schema['type'] ) && is_array( $property_schema['type'] ) && in_array( 'null', $property_schema['type'], true ) ) {
123                $ordered_arr[ $property_name ] = null;
124            }
125        }
126
127        return $ordered_arr;
128    }
129
130    /**
131     * Given an array of the object's properties, set them.
132     *
133     * @used-by unserialize()
134     */
135    public function __unserialize( array $data ): void {
136        $this->licence_key  = $data['licence_key'];
137        $this->status       = $data['status'] ?? $this->status;
138        $this->expiry_date  = new DateTimeImmutable( $data['expiry_date'] );
139        $this->last_updated = new DateTimeImmutable( $data['last_updated'] );
140    }
141
142
143    /**
144     * @see Serializable::serialize()
145     */
146    public function serialize() {
147        return $this->jsonSerialize();
148    }
149
150    /**
151     * @see Serializable::unserialize()
152     */
153    public function unserialize( string $data ) {
154        $this->__unserialize( json_decode( $data, true ) );
155    }
156
157    /**
158     * @see \JsonSerializable::jsonSerialize()
159     */
160    public function jsonSerialize() {
161        return $this->__serialize();
162    }
163
164    public function is_active() {
165        return 'active' === $this->status;
166
167        // array(
168        // 'active',
169        // 'expiring',
170        // 'expired',
171        // );
172    }
173
174    /**
175     * @return array<string,array{description:string,type:string|array<string>,format:string}>
176     */
177    public static function get_licence_object_schema_properties(): array {
178        return array(
179            'licence_key'   => array(
180                'description' => esc_html__( 'The licence key.', 'bh-wp-plugin-updater' ),
181                'type'        => 'string',
182                // 'minimum'          => 1, // TODO: Is there a set length the key will be?
183                // 'exclusiveMinimum' => true,
184                // 'maximum'          => 3,
185                // 'exclusiveMaximum' => true,
186            ),
187            'status'        => array(
188                'description' => esc_html__( 'The licence status.', 'bh-wp-plugin-updater' ),
189                'type'        => 'string',
190                // 'enum' => array(
191                // 'invalid',
192                // ),
193            ),
194            'last_updated'  => array(
195                'description' => esc_html__( 'The last time the license server was successfully contacted.', 'bh-wp-plugin-updater' ),
196                'type'        => array( 'string', 'null' ),
197                'format'      => 'date-time',
198            ),
199            'purchase_date' => array(
200                'description' => esc_html__( 'The date of original purchase.', 'bh-wp-plugin-updater' ),
201                'type'        => array( 'string', 'null' ),
202                'format'      => 'date-time',
203            ),
204            'order_link'    => array(
205                'description' => esc_html__( 'A link to the original order domain.com/my-account/orders/123.', 'bh-wp-plugin-updater' ),
206                'type'        => array( 'string', 'null' ),
207                'format'      => 'uri',
208            ),
209            'expiry_date'   => array(
210                'description' => esc_html__( 'The expiry date.', 'bh-wp-plugin-updater' ),
211                'type'        => array( 'string', 'null' ),
212                'format'      => 'date-time',
213            ),
214            'auto_renews'   => array(
215                'description' => esc_html__( 'Will the licence auto-renew?', 'bh-wp-plugin-updater' ),
216                'type'        => array( 'boolean', 'null' ),
217            ),
218            'renewal_link'  => array(
219                'description' => esc_html__( 'A link to domain.com to renew the licence.', 'bh-wp-plugin-updater' ),
220                'type'        => array( 'string', 'null' ),
221                'format'      => 'uri',
222            ),
223        );
224    }
225}