|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Coco\SourceWatcher\Core\Loaders; |
|
4
|
|
|
|
|
5
|
|
|
use Coco\SourceWatcher\Core\IO\Outputs\DatabaseOutput; |
|
6
|
|
|
use Coco\SourceWatcher\Core\Loader; |
|
7
|
|
|
use Coco\SourceWatcher\Core\Row; |
|
8
|
|
|
use Coco\SourceWatcher\Core\SourceWatcherException; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Class DatabaseLoader |
|
12
|
|
|
* |
|
13
|
|
|
* @package Coco\SourceWatcher\Core\Loaders |
|
14
|
|
|
*/ |
|
15
|
|
|
class DatabaseLoader extends Loader |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* @param Row $row |
|
19
|
|
|
* @throws SourceWatcherException |
|
20
|
|
|
*/ |
|
21
|
|
|
public function load ( Row $row ) |
|
22
|
|
|
{ |
|
23
|
|
|
$this->insert( $row ); |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @param Row $row |
|
28
|
|
|
* @throws SourceWatcherException |
|
29
|
|
|
*/ |
|
30
|
|
|
protected function insert ( Row $row ) : void |
|
31
|
|
|
{ |
|
32
|
|
|
if ( $this->output == null ) { |
|
33
|
|
|
throw new SourceWatcherException( "An output must be provided" ); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
if ( !( $this->output instanceof DatabaseOutput ) ) { |
|
37
|
|
|
throw new SourceWatcherException( sprintf( "The output must be an instance of %s", |
|
38
|
|
|
DatabaseOutput::class ) ); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
$output = $this->output->getOutput(); |
|
42
|
|
|
|
|
43
|
|
|
if ( $output == null || empty( $output ) || ( sizeof( $output ) == 1 && $output[0] == null ) ) { |
|
44
|
|
|
throw new SourceWatcherException( "No database connector found. Set a connector before trying to insert a row" ); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
foreach ( $output as $currentConnector ) { |
|
48
|
|
|
if ( $currentConnector != null ) { |
|
49
|
|
|
$currentConnector->insert( $row ); |
|
50
|
|
|
} |
|
51
|
|
|
} |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
public function getArrayRepresentation () : array |
|
55
|
|
|
{ |
|
56
|
|
|
$result = parent::getArrayRepresentation(); |
|
57
|
|
|
|
|
58
|
|
|
$result["output"] = []; |
|
59
|
|
|
|
|
60
|
|
|
$output = $this->output->getOutput(); |
|
|
|
|
|
|
61
|
|
|
|
|
62
|
|
|
foreach ( $output as $currentConnector ) { |
|
63
|
|
|
if ( $currentConnector != null ) { |
|
64
|
|
|
$result["output"][] = [ |
|
65
|
|
|
"class" => get_class( $currentConnector ), |
|
66
|
|
|
"parameters" => $currentConnector->getConnectionParameters() |
|
67
|
|
|
]; |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
return $result; |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.