PluginProbe ʕ •ᴥ•ʔ
VikAppointments Services Booking Calendar / 1.2.21
VikAppointments Services Booking Calendar v1.2.21
1.2.21 1.2.20 trunk 1.2.17 1.2.18 1.2.19
vikappointments / site / helpers / libraries / order / factory.php
vikappointments / site / helpers / libraries / order Last commit date
classes 3 days ago export 3 days ago factory.php 3 days ago index.html 3 days ago wrapper.php 3 days ago
factory.php
166 lines
1 <?php
2 /**
3 * @package VikAppointments
4 * @subpackage core
5 * @author E4J s.r.l.
6 * @copyright Copyright (C) 2021 E4J s.r.l. All Rights Reserved.
7 * @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL
8 * @link https://vikwp.com
9 */
10
11 // No direct access
12 defined('ABSPATH') or die('No script kiddies please!');
13
14 /**
15 * Factory class used to retrieve the details of the order belonging
16 * to the specified group, such as appointments or packages.
17 *
18 * @since 1.7
19 */
20 abstract class VAPOrderFactory
21 {
22 /**
23 * A list of cached orders.
24 *
25 * @var array
26 */
27 protected static $cache = array();
28
29 /**
30 * Returns the order instance, containing a list of appointments.
31 *
32 * @param integer $id The order ID.
33 * @param mixed $langtag The language tag. If null, the default one will be used.
34 * @param array $options An array of options to be passed to the order instance.
35 *
36 * @return mixed The order instance.
37 */
38 public static function getAppointments($id, $langtag = null, array $options = array())
39 {
40 return static::get('appointment', $id, $langtag, $options);
41 }
42
43 /**
44 * Returns the package order instance.
45 *
46 * @param integer $id The order ID.
47 * @param mixed $langtag The language tag. If null, the default one will be used.
48 * @param array $options An array of options to be passed to the order instance.
49 *
50 * @return mixed The order instance.
51 */
52 public static function getPackages($id, $langtag = null, array $options = array())
53 {
54 return static::get('package', $id, $langtag, $options);
55 }
56
57 /**
58 * Returns the employee subscription order instance.
59 *
60 * @param integer $id The order ID.
61 * @param mixed $langtag The language tag. If null, the default one will be used.
62 * @param array $options An array of options to be passed to the order instance.
63 *
64 * @return mixed The order instance.
65 */
66 public static function getEmployeeSubscription($id, $langtag = null, array $options = array())
67 {
68 return static::get('empsubscr', $id, $langtag, $options);
69 }
70
71 /**
72 * Returns the customer subscription order instance.
73 *
74 * @param integer $id The order ID.
75 * @param mixed $langtag The language tag. If null, the default one will be used.
76 * @param array $options An array of options to be passed to the order instance.
77 *
78 * @return mixed The order instance.
79 */
80 public static function getCustomerSubscription($id, $langtag = null, array $options = array())
81 {
82 return static::get('subscr', $id, $langtag, $options);
83 }
84
85 /**
86 * Unset cache every time the order details change.
87 *
88 * @param string $group The group.
89 * @param integer $id The order ID.
90 *
91 * @return void
92 */
93 public static function changed($group, $id)
94 {
95 if (isset(static::$cache[$group][$id]))
96 {
97 unset(static::$cache[$group][$id]);
98 }
99 }
100
101 /**
102 * Returns the order instance.
103 *
104 * @param string $group The group.
105 * @param integer $id The order ID.
106 * @param mixed $langtag The language tag. If null, the default one will be used.
107 * @param array $options An array of options to be passed to the order instance.
108 *
109 * @return mixed The order instance.
110 *
111 * @throws Exception
112 */
113 protected static function get($group, $id, $langtag = null, array $options = array())
114 {
115 $key = $langtag ? $langtag : 'auto';
116
117 // make sure the group is set in the cache pool
118 if (!isset(static::$cache[$group]))
119 {
120 static::$cache[$group] = array();
121 }
122
123 // load handler class
124 if (!VAPLoader::import('libraries.order.classes.' . $group))
125 {
126 throw new Exception(sprintf('Order driver [%s] not found', $group), 404);
127 }
128
129 // create class name
130 $classname = 'VAPOrder' . ucfirst($group);
131
132 // make sure the class handler exists
133 if (!class_exists($classname))
134 {
135 throw new Exception(sprintf('Order class [%s] does not exist', $classname), 404);
136 }
137
138 // Check if the instance already exists in the cache pool.
139 // Skip cache in case the configuration contains the "ignore_cache" attribute.
140 if (!isset(static::$cache[$group][$id][$key]) || !empty($options['ignore_cache']))
141 {
142 // create a space for the given order to support multiple languages
143 if (!isset(static::$cache[$group][$id]))
144 {
145 static::$cache[$group][$id] = array();
146 }
147
148 // retrieve the order for the given language
149 $obj = new $classname($id, $langtag, $options);
150
151 if (!empty($options['ignore_cache']))
152 {
153 // return the object before caching it
154 return $obj;
155 }
156
157 // retrieve the order for the given language
158 static::$cache[$group][$id][$key] = $obj;
159 }
160
161 // return a clone of the instance to allow the management
162 // of its data without affecting the cached reference
163 return clone static::$cache[$group][$id][$key];
164 }
165 }
166