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 / invoice / factory.php
vikappointments / site / helpers / libraries / invoice Last commit date
classes 3 days ago constraints.php 3 days ago factory.php 3 days ago generator.php 3 days ago index.html 3 days ago invoice.php 3 days ago
factory.php
122 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 VAPLoader::import('libraries.invoice.invoice');
15
16 /**
17 * Invoices factory class.
18 *
19 * @since 1.6
20 */
21 class VAPInvoiceFactory
22 {
23 /**
24 * A list of instances.
25 *
26 * @var array
27 */
28 protected static $classes = array();
29
30 /**
31 * Returns a new instance of this object, only creating it
32 * if it doesn't already exist.
33 *
34 * @param array $order The order details.
35 * @param string $group The invoices group.
36 *
37 * @return VAPInvoice A new instance of the invoice object.
38 *
39 * @deprecated 1.8 Use getInvoice() instead.
40 */
41 public static function getInstance($order, $group = null)
42 {
43 return static::getInvoice($order, $group);
44 }
45
46 /**
47 * Returns a new instance of this object, only creating it
48 * if it doesn't already exist.
49 *
50 * @param array $order The order details.
51 * @param string $group The invoices group.
52 *
53 * @return VAPInvoice A new instance of the invoice object.
54 *
55 * @since 1.7
56 */
57 public static function getInvoice($order, $group = null)
58 {
59 if (!isset(static::$classes[$group]))
60 {
61 if (!VAPLoader::import('libraries.invoice.classes.' . $group))
62 {
63 throw new Exception('Invoice group [' . $group . '] not supported', 404);
64 }
65
66 $classname = 'VAPInvoice' . ucwords($group);
67
68 if (!class_exists($classname))
69 {
70 throw new Exception('Invoice handler [' . $classname . '] not found', 404);
71 }
72
73 static::$classes[$group] = $classname;
74 }
75
76 // get cached classname
77 $classname = static::$classes[$group];
78
79 // instantiate new object
80 $obj = new $classname($order);
81
82 if (!$obj instanceof VAPInvoice)
83 {
84 throw new Exception('The invoice handler [' . $classname . '] is not a valid instance', 500);
85 }
86
87 return $obj;
88 }
89
90 /**
91 * Returns a new generator instance.
92 *
93 * @param mixed $data An object containing the invoice arguments or
94 * the ID of the employee issuing the invoice.
95 *
96 * @param VAPInvoice $invoice The invoice instance to generate. If not
97 * specified, it will be possible to set it in a
98 * second time.
99 *
100 * @return VAPInvoiceGenerator A new generator instance.
101 *
102 * @since 1.7
103 */
104 public static function getGenerator($data = null, $invoice = null)
105 {
106 if (is_numeric($data))
107 {
108 VAPLoader::import('libraries.invoice.generators.employee');
109
110 /**
111 * @todo Implement employee generator once the employees will be
112 * able to issue their own invoices.
113 */
114 // return new VAPInvoiceGeneratorEmployee($invoice, $data);
115 }
116
117 // create default generator
118 VAPLoader::import('libraries.invoice.generator');
119 return new VAPInvoiceGenerator($invoice, $data);
120 }
121 }
122