Skip to content

Commit f2725cb

Browse files
committed
refactor: improve DataTable instance creation logic by introducing helper methods for builders and engines
1 parent fb3ee05 commit f2725cb

File tree

1 file changed

+119
-24
lines changed

1 file changed

+119
-24
lines changed

src/DataTables.php

Lines changed: 119 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -42,42 +42,137 @@ public static function of($source)
4242
*/
4343
public static function make($source)
4444
{
45+
$args = func_get_args();
4546
$engines = (array) config('datatables.engines');
4647
$builders = (array) config('datatables.builders');
4748

48-
$args = func_get_args();
49+
$instance = self::tryCreateFromBuilders($source, $builders, $engines, $args);
50+
if ($instance !== null) {
51+
return $instance;
52+
}
53+
54+
$instance = self::tryCreateFromEngines($source, $engines, $args);
55+
if ($instance !== null) {
56+
return $instance;
57+
}
58+
59+
throw new Exception('No available engine for '.$source::class);
60+
}
61+
62+
/**
63+
* Try to create a DataTable instance from builders configuration.
64+
*
65+
* @param object $source
66+
* @param array $builders
67+
* @param array $engines
68+
* @param array $args
69+
* @return DataTableAbstract|null
70+
*/
71+
private static function tryCreateFromBuilders($source, array $builders, array $engines, array $args): ?DataTableAbstract
72+
{
4973
foreach ($builders as $class => $engine) {
50-
if (is_string($class) && class_exists($class) && $source instanceof $class) {
51-
$engineClass = is_string($engine) && isset($engines[$engine]) ? $engines[$engine] : null;
52-
if ($engineClass === null) {
53-
continue;
54-
}
55-
$callback = [$engineClass, 'create'];
56-
57-
if (is_callable($callback)) {
58-
/** @var \Yajra\DataTables\DataTableAbstract $instance */
59-
$instance = call_user_func_array($callback, $args);
60-
61-
return $instance;
62-
}
74+
if (! self::isValidBuilderClass($source, $class)) {
75+
continue;
76+
}
77+
78+
$engineClass = self::getEngineClass($engine, $engines);
79+
if ($engineClass === null) {
80+
continue;
81+
}
82+
83+
$instance = self::createInstance($engineClass, $args);
84+
if ($instance !== null) {
85+
return $instance;
6386
}
6487
}
6588

66-
foreach ($engines as $engine) {
67-
$canCreate = [$engine, 'canCreate'];
68-
if (is_callable($canCreate) && call_user_func_array($canCreate, $args)) {
69-
$create = [$engine, 'create'];
89+
return null;
90+
}
7091

71-
if (is_callable($create)) {
72-
/** @var \Yajra\DataTables\DataTableAbstract $instance */
73-
$instance = call_user_func_array($create, $args);
92+
/**
93+
* Try to create a DataTable instance from engines configuration.
94+
*
95+
* @param object $source
96+
* @param array $engines
97+
* @param array $args
98+
* @return DataTableAbstract|null
99+
*/
100+
private static function tryCreateFromEngines($source, array $engines, array $args): ?DataTableAbstract
101+
{
102+
foreach ($engines as $engine) {
103+
if (! self::canCreateInstance($engine, $args)) {
104+
continue;
105+
}
74106

75-
return $instance;
76-
}
107+
$instance = self::createInstance($engine, $args);
108+
if ($instance !== null) {
109+
return $instance;
77110
}
78111
}
79112

80-
throw new Exception('No available engine for '.$source::class);
113+
return null;
114+
}
115+
116+
/**
117+
* Check if the source is a valid instance of the builder class.
118+
*
119+
* @param object $source
120+
* @param string $class
121+
* @return bool
122+
*/
123+
private static function isValidBuilderClass($source, $class): bool
124+
{
125+
return is_string($class) && class_exists($class) && $source instanceof $class;
126+
}
127+
128+
/**
129+
* Get the engine class from the engine name.
130+
*
131+
* @param mixed $engine
132+
* @param array $engines
133+
* @return string|null
134+
*/
135+
private static function getEngineClass($engine, array $engines): ?string
136+
{
137+
if (! is_string($engine) || ! isset($engines[$engine])) {
138+
return null;
139+
}
140+
141+
return $engines[$engine];
142+
}
143+
144+
/**
145+
* Check if an engine can create an instance with the given arguments.
146+
*
147+
* @param string $engine
148+
* @param array $args
149+
* @return bool
150+
*/
151+
private static function canCreateInstance($engine, array $args): bool
152+
{
153+
$canCreate = [$engine, 'canCreate'];
154+
155+
return is_callable($canCreate) && call_user_func_array($canCreate, $args);
156+
}
157+
158+
/**
159+
* Create a DataTable instance from the engine class.
160+
*
161+
* @param string $engineClass
162+
* @param array $args
163+
* @return DataTableAbstract|null
164+
*/
165+
private static function createInstance($engineClass, array $args): ?DataTableAbstract
166+
{
167+
$callback = [$engineClass, 'create'];
168+
if (! is_callable($callback)) {
169+
return null;
170+
}
171+
172+
/** @var \Yajra\DataTables\DataTableAbstract $instance */
173+
$instance = call_user_func_array($callback, $args);
174+
175+
return $instance;
81176
}
82177

83178
/**

0 commit comments

Comments
 (0)